$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to set featured image as background for a specific category?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - How to set featured image as background for a specific category?

matteradmin8PV0评论

I am currently trying to set the most recent post's featured image to be the background of my hero image. This is the code I'm using:

<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>

Problem is, it shows the featured image of the most recent most on the entire site. I'm trying to make it use the most recent featured image from a specific category (ID is 42). Could I do something like this:

<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID;cat="45"), 'full' );?>

I am currently trying to set the most recent post's featured image to be the background of my hero image. This is the code I'm using:

<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>

Problem is, it shows the featured image of the most recent most on the entire site. I'm trying to make it use the most recent featured image from a specific category (ID is 42). Could I do something like this:

<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID;cat="45"), 'full' );?>
Share Improve this question edited Nov 21, 2018 at 19:56 Owen asked Nov 21, 2018 at 16:58 OwenOwen 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I am not sure where you are trying to do this but give something like this a try.

<?php 
    $category_id = 45;
    query_posts('showposts=1&cat='.$category_id);
    if (have_posts()) : while (have_posts()) : the_post(); 
        $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    endwhile; endif;
    wp_reset_query(); 
?>
Post a comment

comment list (0)

  1. No comments so far