Tweaking and Manipulating WordPress
This is a guest post by Muhammad Siyab.
WordPress is a very flexible and versatile blogging platform, and can be adapted for use as almost anything (CMS. Tumblelog, etc). In this post, I’ll be telling you how to make your WordPress blog more ‘magazine style’. You will learn how to include post-thumbnails, how to display specific posts on the homepage, automatically update a page which contains posts from a certain category, and more.
Using if/else to check for pages
You can use conditional tags such as if/else to check which page the user is currently on and what content to display to them. The basic structure goes like this:
<?php if( it is a particular page ) { ?>
—-DO THIS—–
<?php } else { ?> /* If the page is NOT the specified one */
—-DO THIS—–
<?php } ?>
Using conditional tags will make your sidebar dynamic and alive. It is good to have a dynamic sidebar that displays information according to the particular page.
Let’s put this into action. Suppose, you want to display your work details and testimonials to the reader, if they are on the About page of your blog. Else, (if they are not on the about page), you want to display your most recent posts. The code to do the job would look like:
<?php if (is_page(’About Me’) ) { ?>
–YOUR WORK DETAILS AND TESTIMONIALS HERE –
<?php } else { ?>
–MOST RECENT POSTS CODE HERE –
<?php } ?>
What the above piece of code does is that it checks first what page the current page is. If the current page has the exact title ‘About Me’, then your work and testimonial details are shown. If the page is not the about page, your most recent posts are shown. Note that the title of the page(’About Me’) in this code and the about page must match exactly, otherwise the code will not work.
You can view all the applicable conditionals on the WordPress Codex.
Automatically Update Pages
Suppose you have a page in which you show whole posts or link to posts in a certain category. Each time you write a new post, you manually enter the information into the page. This is tedious and time consuming. You can, instead have those pages automatically updated for you! The code to use is:
<?php
$posts = get_posts('numberposts=number&category=cat-id&order=order‘);
foreach($posts as $post) :
?>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
<?php endforeach; ?>
Now, if you want to display only a specific number of posts, say only 5, change number to 5. To display all posts, either remove the whole of numberposts=number or set it to a very high value. You also have to change cat-id to the id of the category in which the posts belong. To arrange the posts in descending order(newest first), change order to DESC. To arrange posts in ascending order, (oldest first), change order to ASC.
Also, this code only displays the titles of the post. If you want to display the whole content as well, add <?php the_content(); ?>(For the whole content) or <?php the_excerpt(); ?> (For excerpts) below the <?php the_title(); ?>
WARNING - WordPress doesn’t allow you to add PHP code in your pages or posts in the text-editor. You’ll have to use a PHP executing plugin such as Exec-PHP to do it, or use a page template (the more complicated option).
Custom Fields to Display Post Thumbnails
Custom fields are one of the best features on WordPress, but sadly, not everyone exploits them.
Using custom fields, you can easily prop up thumbnail images next to your blog post. It’s very easy to do so. Insert the following code in your index.php file, wherever you like (preferably next to the title)
<?php
$thumb = get_post_meta($post->ID, "thumb", true);
if ($thumb != "") { ?>
<img src="<?php bloginfo('url')?>/wp-content/uploads/<?php $values = get_post_custom_values("thumb"); echo $values[0]; ?>” alt=”<?php the_title(); ?>” class=”thumb” style=”height:100px; width:100px;” />
<?php } else {} ?>
Now to decipher this code.
$thumb = get_post_meta($post->ID, “thumb”, true); - The code sets up a variable called $thumb and gets the value of the custom field named ‘thumb’ from the current post.
if ($thumb != “”) - Sometimes, you don’t want to include post thumbnails. Perfectly okay. This line checks to see whether the ‘thumb’ key has a value or not. If if does have a value(that is you have specified an image), it continues on with the rest of the code. If it doesn’t have a value, the script stops right there and shows nothing.
The next two lines of the code set up an image tag, and also defines the path of the image file. Note: If your uploads folder is different from the default(’wp-content/uploads/’), change wp-content/uploads/. The $values=… echo $values[0] part prints out the value of the custom field named ‘thumb’.
Also, the class=”thumb” part allows you to manipulate the position and styling of your thumbnail image using CSS.
You can change the height and width attributes to change the size of your thumbnail images, if you want.
The last line of code, (<?php } else {} ?>) finishes off the script by declaring nothing to do in case there is no thumbnail image.
Now, to include post thumbnails, you have to specify custom fields. In the WordPress Post editor area, scroll down to where it says ‘Custom Fields’. Like so:

In the key area, enter the name of the field, that is, ‘thumb‘. In the Value dialog, enter the address of the the image. Take care not to break the path to the image.
And you’re done. You just have to take the extra step of defining the thumbnail custom field, but it’s worth the effort
Bonus Tip
You can use custom fields not only for post thumbnails, but for anything. For example I use them to display taglines(One liners to introduce the post), for my latest post. The code goes like this:
<?php $values = get_post_custom_values("tagline"); echo $values[0]; ?>
Easy as a pie.
Muhammad Siyab blogs at PuttingBlogsFirst.com. He even has a starter’s guide for new bloggers.



Subscribe to our RSS Feed





The headline to this post is rather misleading. “Manipulate” has a kind of negative connotation, of something underhanded being done to control or change something. When I saw this headline I thought the blog entry was going to be about nefarious things people were doing on the web to WordPress blogs.
Ok I added a “tweaking” there also to make it clearer.
@Bilingual Blogger - thanks for pointing out;
@Daniel - thanks for changing it
Thank you. This is really helpful. I’m only on my second week with wordpress and am trying hard to learn it well.
Thank you.. nice tips
Really useful tips…
Going to apply it soon and you can visit to see..
Interesting tips. I always wondered how to use those custom fields. Thanks for making it clear.
I recently ran a SEO optimization report on my site and found some errors with the code that could be hurting my rank. It is telling me that I have some outdated code
img align
a target
object align
Can you show me what this is refering to? Here is the link to the report http://raven-seo-tools.com/seo.....ervoir.com
It is also saying that some style code is incorrect.
What are inline styles?
thank you for a very useful post.
I, however, prefer to simply modify the css file and define the image dimensions; after which I copy paste the image html and the content in the excerpt column.
This gives us freedom to define our excerpts owing to our website style.
However, this will be very useful if in the future we decide to go for it.
Are there other things custom fields can do? Would love to implement them.
Regards.
@katya - Loads.
Just take a look at this Performancing article for some of the ways you can use them
:
http://performancing.com/wordp.....tom-fields
that is very interesting. I’ve never really think about how other people do not know how to tweak and manipulate WordPress. It has just been so easy for me but these steps can really help someone that doesn’t really understand.
Thank you Muhammad Siyab (commenter 10)
I am sure I would be using custom fields in the future.
Super useful article… thanks for share these tips
While I certainly appreciate the time it took to write this article, only the third section is remotely useful to a blogger. By far, the best practice is actually to create an About page rather than to mess with PHP. Why even suggest such a thing? As for displaying a list of most recent posts (the “else” in the first section) and the automatically updated pages, any WordPress theme worth its salt is going to have these already implemented. In fact, even the worst themes have these implemented. If the user is truly new (the obvious target audience of this article), they’re not going to be messing with the deep-level code of WordPress files. They’re going to be downloading a pre-existing theme and won’t have to worry about any of this.
The third section could be quite useful.
Great post. PHP and other scripting languages (I prefer PHP) is a great way to add that extra push to your website to accomplish what you’re looking for. Being that most web servers are LAMP driven I think everyone should learn at least a little.
Thanks this is great and easy to follow. I knew how to add an image to my folder and a custom field. I couldn’t find help on calling my thumbnail on my front page. This worked the first time. Cheers.
Many thanks. It is a good tip. Will try it in future posts…