How to display specific content on the post page only?

After completing the custom field to display unique content work I found out that there was a problem. The problem was not with the functionality which I had just coded rather it was with how it was working.

Problem was that the $post->ID also existed on the archive and category pages. Due to this the system was displaying the content of the custom field on those pages as well which defeated the purpose which the client wanted.

I was aware of the function is_post() which used to check whether the page was a post of not. However, the said function has now been deprecated as of WordPress 2.8.3. My version of WordPress was 2.9.1 so the function was never going to work on this version. I headed off to the WordPress Conditional Tags page to check which function fulfills my requirements.

I soon found my answer. is_single() function provided the functionality I wanted. So, I made the following changes to the code written earlier

if (is_single()) {
        $custom_fields = get_post_custom($post->ID);
        $my_custom_field = $custom_fields['topcontent'];
        $display_title = $my_custom_field[0];
        echo $display_title;

and it worked like a charm. Now the content displayed only on the post page and no other page. The problem was solved and I heaved a sigh of relief.

Hope the above helped.

Leave a Comment