我想将特定类别的最新帖子的摘录嵌入到WP小部件中,其背景是特色图像,我很想知道;如果有人知道它是怎么做的,你能解释一下吗?
为了帮助你们更好地理解,这就是我想象HTML的样子:
<div style="background-image: url(featured image link);">Content from latest post in category X</div>
答案 0 :(得分:0)
您的小部件代码中需要这样的内容:
<?php
$x_cat_id = get_cat_ID('X');
$widget_post = get_posts(array('category'=>$x_cat_id, 'numberposts'=>1)); //get last post from category X
$post_thumbnail_id = get_post_thumbnail_id($widget_post->ID); //get thumbnail id to retrieve all the data we'll need
?>
<div style="background-image: url(<?php echo wp_get_attachment_url($post_thumbnail_id); ?>);"><?php print $widget_post->post_excerpt; ?></div>