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') ) { ?>
<?php } else { ?>
<?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:

$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)

$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 He even has a starter’s guide for new bloggers.

Browse all articles on the WordPress category

22 Responses to “Tweaking and Manipulating WordPress”

  • Net Age | Google SEO

    WordPress is an open source juggernaut that is just getting better and better all the time with the community actively developing new themes, plugins and addons, as well as sharing information such as this. I am a latecomer to the whole WordPress thing, but have learnt so much in so little time thanks to everybody out there sharing their knowledge and experience.

  • ravi

    Thank you. This is really helpful. I’m only on my second week with wordpress and am trying hard to learn it well.

  • Matt

    Thanks for the code. We’ve added this in and the thumbnail is displayed perfectly, but there is also an undisplayed image coming up in the post too!! All the code has been removed from the post text section and we’re still seeing an image.

    What’s going wrong?

  • Steve

    I dont’ use custom fields at all on my wordpress blogs but might look at using your code for the tag lines.

  • Marcus

    Many thanks. It is a good tip. Will try it in future posts…

  • Asuka

    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.

  • Freelance Web Design

    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.

  • Gregory Pittman

    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.

  • Pachecus

    Super useful article… thanks for share these tips

  • katya

    Thank you Muhammad Siyab (commenter 10)

    I am sure I would be using custom fields in the future.

  • Ultimate Blogging Experiment

    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.

  • Muhammad Siyab
  • katya

    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.


  • Ben Moreno
  • Ben Moreno

    Interesting tips. I always wondered how to use those custom fields. Thanks for making it clear.

  • shawal

    Really useful tips…

    Going to apply it soon and you can visit to see..

  • ebta

    Thank you.. nice tips

  • Writer Dad

    Thank you. This is really helpful. I’m only on my second week with wordpress and am trying hard to learn it well.

  • Muhammad Siyab

    @Bilingual Blogger – thanks for pointing out;
    @Daniel – thanks for changing it 😀

  • Daniel Scocco

    Ok I added a “tweaking” there also to make it clearer.

  • Bilingual Blogger

    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.

Comments are closed.