仅显示一个类别的wordpress帖子

时间:2015-10-27 12:48:59

标签: php wordpress content-management-system categories posts

我正在尝试向显示缩略图的页面显示某个类别的帖子,然后链接到完整的帖子。这将成为一个案例研究部分。

我有链接显示并带我通过,但它显示所有帖子而不是我想要的特定类别。

有什么想法吗?我不熟悉PHP,只是使用wordpress

我的代码:

<?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 个答案:

答案 0 :(得分:0)

<?php 
 $catPost = get_posts(get_cat_ID("NameOfTheCategory")); //change this
   foreach ($catPost as $post) : setup_postdata($post); ?>
       <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
             <p><?php the_content(); ?></p>
       </div>
<?php  endforeach;?>

答案 1 :(得分:0)

没有特定的类别集,只有要显示的帖子数量,在本例中为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();
} 


    ?>

这设置了特定类别和帖子数量(使用类别slug将其拉入)

CATEGORY_NAME =服务&安培; posts_per_page = 10