制作标签存档显示帖子按帖子格式排序

时间:2015-07-18 作者:HannesH

我想为标签设置一个归档页面,其中帖子根据帖子格式分开显示,先将帖子格式放在一边,然后从帖子格式放在一边,归档除已显示帖子外的所有帖子。

所以,首先我想要4篇带有post-format-aside格式的帖子,然后我想要一个归档,但没有post-format-aside格式的帖子。正如我尝试使用WP查询所做的那样,它感觉不是一个好的结构。。。。这是我迄今为止设置的代码:

<div class="row">
<?php $posts = new WP_Query( array(
\'posts_per_page\' => 4,
\'post_type\' => \'post\',
\'tax_query\' => array(
    array(                
        \'taxonomy\' => \'post_format\',
        \'field\' => \'slug\',
        \'terms\' => array( 
            \'post-format-aside\'
        ),
    )
)
) );

if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>
            <div class="col-sm-3 col-xs-6">
                <?php   get_template_part( \'content\', get_post_format() ); ?>
                </div>

    <?php endwhile; ?>

    <?php endif; wp_reset_query(); ?>
 </div>

<?php
 $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$posts = ( array(
   \'post_type\' => \'post\',
\'posts_per_page\' => -1,
\'paged\' => $paged,
\'tax_query\' => array(
    array(                
        \'taxonomy\' => \'post_format\',
        \'field\' => \'slug\',
        \'terms\' => array( 
            \'post-format-link\'
        ),
    )
)
) );
$query = new WP_query($posts);
if ( $query->have_posts() ) : ?>
   <?php rewind_posts(); ?>
  <?php  while ( $query->have_posts() ) : $query->the_post();
        if($i%4==0) {
            <div class="row">
        <?php } ?>

            <div class="col-sm-3 col-xs-6">
                <?php   get_template_part( \'content\', get_post_format() ); ?>
                </div>

                <?php $i++;
                    if($i%4==0) { // if counter is multiple of 3, put an closing div ?>
                    </div>
                <?php } ?>

    <?php endwhile; wp_reset_query(); ?>
                <?php
                    if($i%4!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
                    </div>
                <?php } ?>
    <div class="blog">
        <div class="row">
            <div class="col-md-12">
                <?php posts_nav_link(); ?>
            </div>
        </div>
    </div>

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

你说得对。这不是一个好的结构。

您正在页面上运行三个查询—您正在创建的两个查询加上被完全忽略的主查询(加上辅助查询)$wp_query, 这可能会导致意外和不可预测的结果以及分页困难$posts您要做的是更改主查询以排除该帖子格式:

function exclude_asides_wpse_194888($qry) {
  if (is_tag()) {
    $taxq = array(
        array(                
            \'taxonomy\' => \'post_format\',
            \'field\' => \'slug\',
            \'terms\' => array( 
                \'post-format-aside\'
              ),
            \'operator\' => \'!=\'
        )
    )
    $qry->set(\'tax_query\',$taxq);
  }
}
add_action(\'pre_get_posts\',\'exclude_asides_wpse_194888\');
然后运行“asides”查询:

$asides = new WP_Query( 
  array(
    \'posts_per_page\' => 4,
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        array(                
            \'taxonomy\' => \'post_format\',
            \'field\' => \'slug\',
            \'terms\' => array( 
                \'post-format-aside\'
              ),
        )
    )
  ) 
);

if ( $asides->have_posts() ) {
  while ( $asides->have_posts() ) {
    $asides->the_post(); ?>
    <div class="col-sm-3 col-xs-6">
        <?php get_template_part( \'content\', get_post_format() ); ?>
    </div><?php 
  }
}
然后让主查询恢复正常:

if ( have_posts() ) {
  while ( have_posts() ) {
    $query->the_post();
      if($i%4==0) { ?>
        <div class="row"><?php 
      } ?>

      <div class="col-sm-3 col-xs-6"><?php   
        get_template_part( \'content\', get_post_format() ); ?>
      </div><?php 
      $i++;
      if($i%4==0) { // if counter is multiple of 3, put an closing div ?>
        </div><?php 
      } 
  }
  if($i%4!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
    </div><?php 
  } ?>
  <div class="blog">
    <div class="row">
      <div class="col-md-12"><?php 
        posts_nav_link(); ?>
      </div>
    </div>
  </div>
}

结束

相关推荐

Get tags for current user

如何获取用户标签列表,我使用以下内容获取所有标签的列表:<?php $tags = get_tags(); ?> <?php if (isset($tags)): ?> <ul> <?php foreach ($tags as $tag): ?> <li><?php echo $tag->name; ?></li>&#