Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过类别外循环发布Wordpress相关帖子_Wordpress_Foreach - Fatal编程技术网

通过类别外循环发布Wordpress相关帖子

通过类别外循环发布Wordpress相关帖子,wordpress,foreach,Wordpress,Foreach,我在网上找到了这段代码,并做了我需要它做的事情,即在单个模板中通过循环外的类别给我一个相关帖子的列表 <?php $postid = $post->ID; foreach((get_the_category()) as $category) { echo "<h3>Related Posts in ".$category->cat_name." </h3>"; $postlist = get_posts('

我在网上找到了这段代码,并做了我需要它做的事情,即在单个模板中通过循环外的类别给我一个相关帖子的列表

<?php
    $postid = $post->ID;
    foreach((get_the_category()) as $category) {
        echo "<h3>Related Posts in ".$category->cat_name." </h3>";
        $postlist = get_posts('category='.$category->cat_name);
            foreach ($postlist as $post) :
                $catpostid = $post->ID;
                    if (in_category($category->cat_name) && ($catpostid != $postid)) { ?>
                        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php }
            endforeach;
      }
?>

  • 有人能帮我添加一个post count参数吗?我已经试着修改了好几个小时了,我一直在破坏它。我所需要的只是控制显示多少帖子的能力。很抱歉,但在PHP中编写任何代码时,我都是个十足的笨蛋


    谢谢。

    您可以通过numberposts参数获取帖子()以及您的类别。。。我在$args=行中列出了一些想法,从你的类别名称开始,只允许10篇文章,按标题升序排列。当然,完全未经测试,但请随意使用它

    <?php
      $postid = $post->ID;
      foreach((get_the_category()) as $category) {
       echo "<h3>Related Posts in ".$category->cat_name." </h3>";
       $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title', 'category' => $category->cat_name );
       $postlist = get_posts( $args );
       foreach ($postlist as $post) :
       $catpostid = $post->ID;
       if (in_category($category->cat_name) && ($catpostid != $postid)) { ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       <?php }
       endforeach;
      } ?>
    
    
    

  • 要获得获取帖子所需的参数的完整列表,还可以查看Wordpress文档:尝试了您提供的代码,没有中断或出现任何错误。但奇怪的是,当我把数字从10降到4时,它只显示了1个相关的帖子。在这个测试类别中有3个帖子,所以它应该显示两个相关的帖子。正如我所说的,上面4篇和下面的文章只显示了1篇相关的文章,但是5-10篇,我假设10篇以上的任何数字都显示了2篇文章。有什么想法吗?