显示最近5篇文章从特定类别(Wordpress)


Show last 5 posts from specific category (Wordpress)

我试图显示从一个特定类别的最后5个帖子,这将被链接到一个功能,所以我可以在一个Wordpress页面插入短代码。我所拥有的代码如下,它做了我所需要的一切(虽然我也想添加特色图像),除了它不显示来自特定类别的帖子。

我尝试了很多方法,但都没有找到一个有效的修复。

function Last5posts()
{
    $args = array( "showposts" => 5, "category" => 3 ); 
    $content = "";   
    query_posts($args);
    if ( have_posts() ) : 
        while ( have_posts() ) :
            the_post();
            $link = get_permalink();
            $title = get_the_title();
            $date = get_the_date();                              
            $content .= "<div class='latest-posts'>";
            $content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>'n";
            $content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
            $content .= "</div>";
        endwhile;
        wp_reset_query(); 
     endif;
     return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );   

我已经尝试用下面的代码替换第3行和第4行,但是它抛出了一个错误"语法错误,'}'在第31行"。

$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post(); 

你可以像下面这样使用代码

query_posts( 'cat=3&posts_per_page=5' );

使用这个wordpress默认会在这段代码之后使用最后5篇文章

使用
$catnames[1]表示您想使用与该帖子相关的哪个类别

<?php $catnames = get_the_category();  
$postcatid=$catnames[1]->term_id;
$catquery = new WP_Query( 'cat='.$postcatid.'&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ;?>" rel="bookmark"><?php the_title(); ?></a>    </h3>
</li>
</ul>
 <?php endwhile; 
?>

查看这个:query posts parameters;你绝对应该使用"cat"而不是category。还有,你的"while"是不是以"endwhile;"结尾?现在完整的代码是什么样子的?