2013-03-28 72 views
1

我想显示从特定类别,这将链接到一个功能,所以我可以在Wordpress页面插入短代码的最后5个职位。我拥有的代码如下,它尽我所需(尽管我想添加精选图片),但它不显示来自特定类别的帖子。显示从特定类别(WordPress的)最后5个职位

我试过很多东西,但找不到工作修复程序。

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(); 

任何帮助将不胜感激。

回答

1

查看结果:query posts parameters;你绝对应该使用“猫”而不是类别。 另外,你是否以“endwhile”结束了你的“while”? 你现在的完整代码是什么样的?

+0

我正在使用下面的代码作为答案添加的代码;它的工作原理,但也似乎显示“留下一个答复”框,它不应该因为我已经把简码的页面禁用了评论。 –

2

可以使用代码略低于

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

使用这个WordPress默认情况下将使用最后5后这个代码后像...

2

使用此
$ 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; 
?> 
相关问题