简体   繁体   中英

Wordpress - How to display featured image from specific category as background?

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 from results category. Try this:

function set_post_background() {
    $query = new \WP_Query(['category_name' => 'results']);
    if ($query->have_posts()) {
        while ($query->have_posts()) : $query->the_post();
        $bgimage = wp_get_attachment_image_src(get_post_thumbnail_id($query->post->ID), 'full');
        if (!empty($bgimage)) {
            return '<style type="text/css">body{background:#fff url('.$bgimage[0].') no-repeat top center}</style>';
        }
        endwhile;
    }
    wp_reset_query();
}

Under any circumstances, do not use query_posts() function to query posts. From reference:

Note : This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Please replace following code for your code and it should work fine.

function set_post_background() {
$cat_ids=array();
$img='';
$cat_ids=wp_get_post_categories();

foreach($cat_ids as $cat_id){
    $img=wpds_tax_pic_url($cat_id);
}
if (!empty($img)) {
  return '<style type="text/css">body {background:#fff url('.$img.') no-repeat top center;}</style>';
}}

And also you can set default image on "else" statement

Try it!...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM