使用其他选项,而不是使用9 wp_Query

时间:2017-01-21 作者:arata

嗨,我在用这些9 wp\\U查询输入index.php我想知道有没有其他方法可以做同样的事情,但不需要使用大量的查询?

<?php $bigindex = new WP_Query(\'posts_per_page=1&post_status=publish\'); ?>
<?php if (have_posts()) : while ($bigindex->have_posts()) : $bigindex->the_post(); ?>
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<header class="article-header header-vertical-position-mid <?php the_field(\'posttype\'); ?> latest-article" style="background-image: url(<?php the_field(\'320px\'); ?>);">
      <div class="container">
        <div class="article-header-elements">
          <h1 class="article-title">
            <a href="<?php the_permalink();?>" title="<?php the_title(); ?>">
              <span class="article-highlight">
                <?php the_title(); ?>
              </span>
              <small class="article-subtitle">
                <span class="article-highlight">
                  <?php the_field(\'shorttext\'); ?>
                </span>
              </small>
            </a>
          </h1>
          <p class="article-meta">
            <span class="article-highlight">
                <i class="fa fa-<?php the_field(\'randico\'); ?>"></i>
              Last Post •
              <time datetime="<?php the_time(\'F j, Y\'); ?> "><?php the_time(\'F j, Y\'); ?> </time>
            </span>
          </p>
        </div>
      </div>
    </header>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

    <div class="company-news">
      <a href="#" title="news">
        <h2>
          News | <time datetime="2017-01-03T00:00:00Z">2017 . 01 . 03</time>
          <small>Something goes here</small>
        </h2>
      </a>
    </div>
    <div class="additional-posts">
      <div class="article-thumbnail-container">
          <div class="article-thumbnail-row">
              <?php $meindex = new WP_Query(\'posts_per_page=4&post_status=publish&offset=1\'); ?>
              <?php if (have_posts()) : while ($meindex->have_posts()) : $meindex->the_post(); ?>
              <?php if( $post->ID == $do_not_duplicate ) continue; ?>
              <div class="article-thumbnail <?php the_field(\'posttype\'); ?> article-thumbnail-bottom-border" style="background-image: url(<?php the_field(\'320px\'); ?>);">
                  <a href="<?php the_permalink();?>" title="<?php the_title(); ?>">
                      <div>
                          <span class="article-thumbnail-title">
                              <i class="fa fa-<?php the_field(\'randico\'); ?>"></i><?php the_title(); ?>
                          </span>
                      </div>
                  </a>
              </div>
              <?php endwhile; endif; ?>
              <?php wp_reset_query(); ?>
          </div>
        </div>
      <div class="container">
          <div class="more-articles">
          <h2>
            <span class="highlight">Recent Posts...</span>
          </h2> 
          <ol class="article-index-list">
              <?php $meindex = new WP_Query(\'posts_per_page=20&post_status=publish&offset=5\'); ?>
              <?php if (have_posts()) : while ($meindex->have_posts()) : $meindex->the_post(); ?>
              <?php if( $post->ID == $do_not_duplicate ) continue; ?>

              <li class="article-index-item <?php the_field(\'posttype\'); ?>">
                  <a href="<?php the_permalink();?>" title="<?php the_title(); ?>">
                      <div class="article-index-item-header" style="background-image: url(<?php the_field(\'320px\'); ?>);"></div>
                      <div class="article-index-item-text">
                          <div class="article-index-item-category">
                              <span>
                                  <?php the_field(\'caticoname\'); ?>
                              </span>
                          </div>
                          <h3>
                              <?php the_title(); ?>
                              <small><?php the_field(\'shorttext\'); ?></small>
                          </h3>
                      </div>
                  </a>
              </li>

<?php endwhile; endif; ?>
还有3个这样的查询

<li class="dropdown interviews nav-item">
            <a aria-expanded="false" aria-haspopup="true" class="nav-link" data-target="#" data-toggle="dropdown" href="/interviews/" id="dInterviews">
             <i class="fa fa-users"></i>Interviews
            </a>
            <div aria-labelledby="dInterviews" class="dropdown-menu">
              <div class="article-thumbnail-container">
                <div class="article-thumbnail-row">
                    <?php $jpindex = new WP_Query(\'posts_per_page=3&post_status=publish&cat=5\'); ?>
                    <?php if (have_posts()) : while ($jpindex->have_posts()) : $jpindex->the_post(); ?>
                    <?php if( $post->ID == $do_not_duplicate ) continue; ?>
                    <div class="article-thumbnail interviews" style="background-image: url(<?php the_field(\'320px\'); ?>)">
                        <a href="<?php the_permalink();?>" title="<?php the_title(); ?>">
                            <div> 
                                <span class="article-thumbnail-title"><?php the_title(); ?></span> 
                            </div> 
                        </a> 
                    </div>
                    <?php endwhile; endif; ?>
                    <?php wp_reset_query(); ?>
                  <div class="article-thumbnail article-thumbnail-archive interviews">
                    <a href="/interviews/" title="Interviews archive">
                      <div>
                        <i class="fa fa-users"></i>
                        <br>
                        More..
                      </div>
                    </a>
                  </div>
                </div>
              </div>
            </div>
          </li>

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

我没有给你一个完整详细的答复,但我有几点建议:

1) 为这些段中的每个段使用模板零件。这将有助于清理模板,以便将来更容易使用。(参见此处:https://konstantin.blog/2013/get_template_part/ )

2) 然后,您可以为整个查询创建一个助手函数,基本上获取所有内容,然后登录助手函数以过滤结果。因此,在每个新模板部分中,您都会有如下内容:

prefix_index_query($posts_per_page = 3, $post_status = \'publish\')

请参见此处:https://tommcfarlin.com/wordpress-helper-functions-for-templates/

仅这两件事就可以让您朝着正确的方向前进,以获得更干净的文件结构。

相关推荐

如何在class-wp-query.php中跟踪核心函数is_page()&is_Single上的通知警告

在我的生产站点上,我的这些产品已经超支了:PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3728 PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3730 PHP No