Calling posts in header
This weekend I was trying to add what I thought to be a simple feature to my site, and it ended up taking me a lot longer than expected. Because of this I wanted to go ahead document and post about what I did and how I accomplished it.
Problem
I needed to add the post picture stored in a custom field to my header only on the post when its called.
Information
If you guys are already familiar with wordpress in any sort of way then you will know that the single.php will handle any calls of your posts. but it also handles categories or groups of posts that are called.
Hypothesis
My hypothesis was that i could run an if statement that would test and see if it was using single.php and if it was in the specific category i want the picture applied to (in my case the only categorized with post pictures is my blog).
my statement was something like this
<php if (is_single() && in_category(‘blog’))
echo ‘content ‘;
} ?>
Solution
Although my initial hypothesis was nearly correct or at least the logic was correct it wasn’t working for some reason, the wordpress template tag for in_category was pulling no results at any time. And I believe this was because this statement was completely out of the loop being in the header and all it didn’t quite know what post it was suppose to call to be in a category or not. Any ways after trying to take a nap later that day the simple solution came to me and ended up as such.
<?php if (is_single()) {
if (!is_category()) {
echo ‘content’;
}
} ?>
Instead of testing if it’s in a category I only needed to test to see if it was displaying a category page or not, This would eliminate my border on category pages, but keep them on all my post pages, the next and last step would be to create a default image for the categories where there is no post image. Ill post on this later.
Tags: Blog, Code, Css, Development, Php, Template Tags, Tips, Tutorials, Wordpress








