Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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
如何使single.php中的第二个循环工作?_Php_Wordpress_Syntax_Wordpress Theming - Fatal编程技术网

如何使single.php中的第二个循环工作?

如何使single.php中的第二个循环工作?,php,wordpress,syntax,wordpress-theming,Php,Wordpress,Syntax,Wordpress Theming,-简单博客 -二十一儿童主题 我需要:single.php中的第二个循环,显示所选帖子和下面的所有其他帖子 到目前为止,我在single.php中的内容(结果是一个空白页): 这应该可以做到: <?php get_header(); ?> <div id="primary" class="site-content"> <div id="content" role="main"> <?php while (

-简单博客 -二十一儿童主题

我需要:single.php中的第二个循环,显示所选帖子和下面的所有其他帖子

到目前为止,我在single.php中的内容(结果是一个空白页):


这应该可以做到:

<?php get_header(); ?>
    <div id="primary" class="site-content">
        <div id="content" role="main">

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


         <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <?php the_content(); ?>
        </div>

                <?php comments_template( '', true ); ?>

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

        <?php wp_reset_postdata(); // reset the post data so we can run another query ?>

     <?php 

     $args_second = array(
            'posts_per_page' => -1,
        );

     // The Second Query
     $second_query = new WP_Query( $args_second );

     // The Loop
     if ( $second_query->have_posts() ):
        while ( $second_query->have_posts() ):
            $second_query->the_post(); ?>

        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <?php the_content(); ?>
        </div>

            <?php endwhile; ?>
            <?php endif; ?>
        <?php wp_reset_postdata(); // Restore original Post ?>
        </div><!-- #content -->

    </div><!-- #primary -->

注:

  • 您需要在单个循环中使用_title()和_content()正确显示标题和内容
  • 要显示其他帖子,您需要查询它们,通过查看上面的代码,您将很快理解它们
  • 我把样式留给你

  • 它经过测试并正常工作。

    因为OP需要所有的帖子,所以
    posts\u per\u page
    参数应该是
    -1
    。是的,这是有意义的。我在自己的安装中进行测试,如果我不停止循环,它将加载数百篇文章。谢谢。
    <?php get_header(); ?>
        <div id="primary" class="site-content">
            <div id="content" role="main">
    
                <?php while ( have_posts() ) : the_post(); ?>
    
    
             <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
                <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                <?php the_content(); ?>
            </div>
    
                    <?php comments_template( '', true ); ?>
    
                <?php endwhile; // end of the loop. ?>
    
            <?php wp_reset_postdata(); // reset the post data so we can run another query ?>
    
         <?php 
    
         $args_second = array(
                'posts_per_page' => -1,
            );
    
         // The Second Query
         $second_query = new WP_Query( $args_second );
    
         // The Loop
         if ( $second_query->have_posts() ):
            while ( $second_query->have_posts() ):
                $second_query->the_post(); ?>
    
            <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
                <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                <?php the_content(); ?>
            </div>
    
                <?php endwhile; ?>
                <?php endif; ?>
            <?php wp_reset_postdata(); // Restore original Post ?>
            </div><!-- #content -->
    
        </div><!-- #primary -->