繁体   English   中英

wordpress显示所有帖子,而不是分类

[英]wordpress displays all posts instead of categorised

我试图将不同类别的帖子显示在不同的页面上,以便将它们用于“服务”和“案例研究”页面。

我设置了两个页面,它们显示了每个帖子的链接,但是显示的是所有帖子,而不仅仅是特定类别。

除了帖子类别ID,我在每个页面上使用的代码都是相同的。

如何在单独的页面上显示单独的类别?

服务页面:

              <?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=4");
  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;
  }
}
?> 

案例研究页面:

        <?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=5");
  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;
  }
}
?> 

回答

没有设置特定的类别,只有要显示的帖子数量,在这种情况下为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();
} 


    ?>

这将设置特定的类别和帖子数量(使用类别标签将其插入)

category_name=service&posts_per_page=10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM