Domanda

Hi I'm using these 9 wp_query in index.php I want to know that is there any another way to do the same thing but without using lot of queries?

<?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; ?>

and also there's 3 queries like this

<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>
È stato utile?

Soluzione

I don't have a full detailed response for you, but here's a few things I'd suggest:

1) Use template parts for each of those segments. That will help clean up the template so it's easier to work with in the future. ( see here: https://konstantin.blog/2013/get_template_part/ )

2) You could then create a helper function for the entire query, basically get EVERYTHING, then have login in the helper function to filter the results. So that within each new template part you'd have something like this:

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

See here: https://tommcfarlin.com/wordpress-helper-functions-for-templates/

Those two things alone will get you going in the right direction for a much cleaner file structure.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top