duanliao6077 2015-10-27 04:48
浏览 75
已采纳

仅显示一个类别的wordpress帖子

I am trying to display posts of a certain category to a page showing a thumbnail then linking through to the full posts. This is going to be fore a case study section.

I have the links showing up and carrying me through however it displays ALL posts instead of the specific category like I want.

Any ideas? I'm not familiar with PHP and only just stated using wordpress

my code:

<?php // PAGE LINK/TITLE

if (is_page()) {
  $cat=get_cat_ID($post->post_title); //use page title to get a category ID
  $posts = get_posts ("cat=$cat&showposts=10");
  if ($posts) {
    foreach ($posts as $post):
      setup_postdata($post); 

 if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail();
} 

?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page

?>

<?php endforeach;
  }
}
?>

展开全部

  • 写回答

2条回答 默认 最新

  • duancaishun4812 2015-10-28 02:11
    关注

    There is no specific category set, only the amount of posts to be displayed, in this case 10.

     <?php // PAGE LINK/TITLE
    
    
    if (is_page()) {
      $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    
      $posts = get_posts ("category_name=service&posts_per_page=10"); //CHANGE CODE AND ADD THIS LINE***************************
    
      if ($posts) {
        foreach ($posts as $post):
          setup_postdata($post); 
    
                      if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
    } 
    
    
        ?>
    

    This sets the specific category and the number of posts (use category slug to pull it in)

    category_name=service&posts_per_page=10

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部