【问题标题】:Display all the posts except selected post by category wise按类别显示除所选帖子之外的所有帖子
【发布时间】:2016-06-04 05:24:17
【问题描述】:

我已显示“主页”类别帖子主页。点击“阅读全文” 第一篇文章。 它正在显示第一篇文章的所有详细信息。

现在我想用“Readmore”显示“第一篇文章”下面的所有帖子。

这是我的代码,它显示了一些其他类别的帖子。

<?php
  $categories = get_the_category();
  $catID = the_category_ID($echo=false); //Current selected category ID
  $catPost = get_posts('cat=$catID&posts_per_page=3');
  foreach ($catPost as $post) : setup_postdata($post);
?>
  <h1><a><?php the_title(); ?></a></h1>
  <?php the_excerpt(); ?> 
  <p class="postinfo">Written by: <?php the_author_posts_link(); ?>
  Posted on: <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>
  Categories: <?php the_category(', '); ?></p>
  <hr />
<?php  endforeach;?>

帖子页面

【问题讨论】:

  • 您的意思是按类别或当前类别显示所有帖子。
  • 没有。只想显示选定的类别帖子。并非所有类别
  • 是的。当前类别帖子
  • 使用 get_posts('category_name=CATEGORYNAME&posts_per_page=5');或使用 wp_query 像 $getpost = new WP_Query('category_name=my_second_category', 'posts_per_page'=>3); ?>
  • 我如何动态使用它?

标签: wordpress


【解决方案1】:

试试这个方法..

<?php
    global $post;
    if(is_category() || is_single()){
    foreach(get_the_category() as $category)
    {
    $current = $category->cat_ID;
    $current_name = $category->cat_name;

    //query_posts("cat=". $current);

    $myposts = get_posts(array('category__in' => array($current)));

    //$myposts = get_posts('numberposts=50&category='.$current); 

    //query_posts(array('category__in' => array(11)));
        }
    }

    foreach($myposts as $post) :
    setup_postdata($post); 

    ?>
    <li>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>

    <?php endforeach; ?>

【讨论】:

  • 是的,这就是我想要的,但想显示除所选帖子之外的所有帖子。
  • 我有 4 个帖子。我选择了 Post-2,并希望显示 post-1,3 和 4。
  • 你在帖子参数中使用过'numberposts'吗?
  • 这个答案非常有用。我如何显示除选定的帖子之外的所有帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 2019-10-25
相关资源
最近更新 更多