为什么我的POST循环会打破页面?

时间:2017-04-26 作者:Daniel

因此,我需要在我的WordPress站点中添加自定义帖子类型的标题和内容。我将帖子循环添加到此归档工作中。php文件:

<?php
/**
 * Work template file.
 *
 * This is the most generic template file in a WordPress theme and one of the
 * two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package   CMMS
 * @author    Creative MMS
* 
 * 
 * @since     1.0.0
 */

get_header(); ?>


    <div class="cmms-house">

        <div class="content-area cmms-clr">

            <main class="site-main cmms-clr">

                <?php
                // Display page header
                //get_template_part( \'partials/archives/header\' ); ?>

                <?php
                // Check if posts exist
                if ( have_posts() ) : ?>

                    <div class="work-entry cmms-clr">   
                        <div class="work-col work-left">

                            <?php
                                // Get query
                                global $wp_query;

                                // Count
                                $count = 1;

                                // Loop through posts
                                while ( have_posts() ) : the_post(); 

                                //Vars
                                $tileImageRow = get_field(\'hovered_image_large\');



                                ?>

                                <div id="<?= $count; ?>" class="item <?php if ($count == 1) : ?>active<?php endif; ?>" style="background-image: url(\'<?= $tileImageRow[\'url\']; ?>\');">




                                </div>


                            <?php
                                // End loop
                                $count++; endwhile;
                            ?>
                            <?php wp_reset_postdata(); ?>

                        </div><!-- .left -->

                        <div class="work-col work-right">
                            <ul>
                            <?php
                                // Get query
                                global $wp_query;

                                //Counter
                                $counter = 1;

                                // Loop through posts
                                while ( have_posts() ) : the_post(); ?>

                                    <li class="list-right" data-trigger="#<?= $counter; ?>"><?= get_the_title(); ?></li>

                            <?php
                                // End loop
                                $counter++; endwhile;
                            ?>
                            <?php wp_reset_postdata(); ?>
                            </ul>
                        </div><!-- .right -->

                    </div><!-- .work-entry -->  

                    <?php
                    // Include pagination template part
                    cmms_include_template( \'partials/global/pagination.php\' ); ?>

                <?php
                // Display no posts found message
                else : ?>

                    <?php get_template_part( \'partials/entry/none\' ); ?>

                <?php endif; ?>

            </main><!-- .main -->

        </div><!-- .content-area -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>
添加此代码时:

<?php if (have_posts()) : ?>
  <?php while(have_posts()): the_post(); ?>
    <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
  <?php endwhile; ?>
<?php endif; ?>
在id为的div之间,分页,这与我想要显示自定义帖子类型的标题和内容的意图相反。为什么?

1 个回复
最合适的回答,由SO网友:Aishan 整理而成

通过在第一个循环中存储标题/内容,然后在div之间的外部显示,可以使用单个循环来实现这一点。这样,您就不需要重复相同的内容,从而最终减少了加载时间。

<?php
/**
 * Work template file.
 *
 * This is the most generic template file in a WordPress theme and one of the
 * two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package   CMMS
 * @author    Creative MMS
*
 *
 * @since     1.0.0
 */
get_header(); ?>
    <div class="cmms-house">
        <div class="content-area cmms-clr">
            <main class="site-main cmms-clr">
                <?php
                // Display page header
                //get_template_part( \'partials/archives/header\' ); ?>
                <?php
                // Check if posts exist
                if ( have_posts() ) : ?>
                    <div class="work-entry cmms-clr">
                        <div class="work-col work-left">
                            <?php
                                // Get query
                                global $wp_query;
                                // Count
                                $li = \'\';
                                $count = 1;
                                // Loop through posts
                                while ( have_posts() ) : the_post();
                                //Vars
                                $tileImageRow = get_field(\'hovered_image_large\');
                                $li .= \'<li class="list-right" data-trigger="#<\'.$count.\'">\'.get_the_title().\'</li>\';
                                ?>
                                <div id="<?= $count; ?>" class="item <?php if ($count == 1) : ?>active<?php endif; ?>" style="background-image: url(\'<?= $tileImageRow[\'url\']; ?>\');">
                                </div>
                            <?php
                                // End loop
                                $count++; endwhile;
                            ?>
                            <?php wp_reset_postdata(); ?>
                        </div><!-- .left -->
                        <div class="work-col work-right">
                            <ul>
                                <?php echo $li; ?>
                            </ul>
                        </div><!-- .right -->
                    </div><!-- .work-entry -->
                    <?php
                    // Include pagination template part
                    cmms_include_template( \'partials/global/pagination.php\' ); ?>
                <?php
                // Display no posts found message
                else : ?>
                    <?php get_template_part( \'partials/entry/none\' ); ?>
                <?php endif; ?>
            </main><!-- .main -->
        </div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
希望你能理解!!

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp