Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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
Php 需要WordPress站点的分类帖子列表,但循环有问题_Php_Wordpress_Loops_Categories - Fatal编程技术网

Php 需要WordPress站点的分类帖子列表,但循环有问题

Php 需要WordPress站点的分类帖子列表,但循环有问题,php,wordpress,loops,categories,Php,Wordpress,Loops,Categories,我正在写一个wordpress博客(z的主题),我想在一个菜单中显示每个类别的所有文章,该菜单将在所有页面上都可用 我正在使用下面的代码 <?php $cat_args = array( 'orderby' => 'name', 'order' => 'ASC', 'child_of' => 0 ); $categories = get_categories(

我正在写一个wordpress博客(z的主题),我想在一个菜单中显示每个类别的所有文章,该菜单将在所有页面上都可用

我正在使用下面的代码

<?php
        $cat_args = array(
          'orderby' => 'name',
          'order' => 'ASC',
          'child_of' => 0
        );

        $categories =   get_categories($cat_args); 

        foreach($categories as $category) { 
            echo '<h4 class="category" id="' . $category->name.'" >' . $category->name.'</h4>';

             $post_args = array(
              'numberposts' => 5,
              'category' => $category->term_id 
            );

            $posts = get_posts($post_args);

            foreach($posts as $post) {
            ?>
            <p class="category-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php 
            } 
            }
            ?>

这段代码将完全按照我的要求显示和格式化列表

问题是

1.)在索引页上,如果出现在循环之前,此代码将使我的粘性帖子消失

2.)在单篇文章页面上,如果代码位于循环之后,则循环之后不会显示任何内容

这很好,因为我可以使用条件来显示代码,或者根据页面的不同来显示代码,但是

3.)这段代码还使单个post循环只显示最近的post的内容,但仅当它位于循环之前时才显示,否则我将回到问题2

我已尝试使用wp_reset_postdata()跟踪代码;和wp_reset_query();没有用,我也不知道如何正确地显示所有的内容,在codex中使用多循环教程

这也是我的single.php循环

<?php while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( 'content', 'single' ); ?>

            <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() ) :
                    comments_template();
                endif;
            ?>

            <?php endwhile; // end of the loop. ?>

这是我的index.php循环

<?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <?php
                    /* Include the Post-Format-specific template for the content.
                     * If you want to override this in a child theme, then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );
                ?>

            <?php endwhile; ?>

            <?php wdh_paging_nav(); ?>

        <?php else : ?>

            <?php get_template_part( 'content', 'none' ); ?>

        <?php endif; ?>

请帮帮我


<?php 
   $args = array(
    'posts_per_page'   => -1,
    'offset'           => 0,
    'category'         => 8, // specify category id here
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true ); 

    $myposts = get_posts( $args ); 

    foreach( $myposts as $post )
    {
      setup_postdata( $post );
      ?>
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      <?php
    }
    wp_reset_postdata();
?>

谢谢!更新后的代码在我文章的底部。