Firstly, PHP isn’t my strong point, but here we go.
I have a function in my functions.php that grabs the featured image and sets it as the background. This function is then called in the header.php
function set_post_background() {
if(query_posts(array ('category_name' => 'results')));
if (have_posts()) : while (have_posts()) : the_post();
global $post;
$bgimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
if (!empty($bgimage)) {
return '<style type="text/css">body {background:#fff url('.$bgimage[0].') no-repeat top center;}</style>';
}
endwhile; endif;
wp_reset_query();
}
Last night, I attempted to modify the function, and wrap it in a query_posts()
I managed to kind of get it working. It will now only fetch the featured image of a post and set it as the background if it is in the category called “results”.
But something in this code is wrong, as now none of my page content appears. Disable the function, content comes back.
What have I done wrong?
[edit] I think it’s the way in which I am querying the category name. because page.php similar queries which get the_content()
I think the function is overriding that query, and therefor not displaying the pages content.
The problem is you’re using
query_posts
which will alter the results of the default main query. It means the default contents of the page will be altered by contents fromresults
category. Try this:Under any circumstances, do not use
query_posts()
function to query posts. From reference:Please replace following code for your code and it should work fine.
And also you can set default image on “else” statement
Try it!…