简体   繁体   English

Wordpress - 如何将特定类别的特色图像显示为背景?

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

Firstly, PHP isn't my strong point, but here we go. 首先,PHP不是我的强项,但我们走了。

I have a function in my functions.php that grabs the featured image and sets it as the background. 我在我的functions.php中有一个功能,可以抓取特色图像并将其设置为背景。 This function is then called in the header.php 然后在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. 昨晚,我试图修改该函数,并将其包装在query_posts()我设法让它正常工作。 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. 因为page.php类似的查询得到了the_content()我认为该函数覆盖了该查询,因而没有显示页面内容。

The problem is you're using query_posts which will alter the results of the default main query. 问题是你使用的是query_posts会改变默认主查询的结果。 It means the default contents of the page will be altered by contents from results category. 这意味着页面的默认内容将被results类别中的内容更改。 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. 在任何情况下, 都不要使用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. query_posts()是一种过于简单化和有问题的方法来修改页面的主要查询,方法是用新的查询实例替换它。 It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). 它是低效的(重新运行SQL查询)并且在某些情况下会彻底失败(特别是在处理帖子分页时)。

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 您还可以在“else”语句中设置默认图像

Try it!... 试试吧!...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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