如何在这个定制循环中使用这个$TAX_SELECTION变量?

时间:2012-12-24 作者:janoChen

我从这个问题中提取了这段代码(它显示了如何让用户通过选择类别和标记来选择要显示的帖子):

Filter index page plugin

function.php

add_action( \'pre_get_posts\', \'do_user_post_selection\' );

function do_user_post_selection( $query ) {

    if( !is_home() || !is_user_logged_in() )
        return;

    if( isset( $query->query_vars[\'post_type\'] ) && ( \'nav_menu_item\' == $query->query_vars[\'post_type\'] ) )
        return; 
    if( 1 < did_action(\'wp\') )
        return;
    global $current_user;

    $tax_selection = get_user_option( \'taxonomy_selection\', $current_user->ID );

    if( empty( $tax_selection ) )
        return;

    if( isset( $tax_selection[\'categories\'] ) && ( is_array( $tax_selection[\'categories\'] ) ) && ( !empty( $tax_selection[\'categories\'] ) ) ) 
        $query->set( \'category__in\', $tax_selection[\'categories\'] );
    if( isset( $tax_selection[\'post_tag\'] ) && ( is_array( $tax_selection[\'post_tag\'] ) ) && ( !empty( $tax_selection[\'post_tag\'] ) ) ) 
        $query->set( \'tag__in\', $tax_selection[\'post_tag\'] );

    return;
}
我的首页模板中有以下内容:

front-page.php

      <?php // Create and run custom loop
          $custom_posts = new WP_Query();
          $custom_posts->query(\'post_type=topic&posts_per_page=5\');
          while ($custom_posts->have_posts()) : $custom_posts->the_post();
      ?>  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div class="entry-left">
              <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyeleven\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            </div><!-- .entry-header -->
          </article>
        <?php endwhile; ?>
      </div>
如何使用中的值$tax_selection 在我的自定义循环中?

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

我想你需要让它全球化。它可能看起来像:

add_action( \'pre_get_posts\', \'do_user_post_selection\' );
function do_user_post_selection( $query ) {

    if( !is_home() || !is_user_logged_in() )
        return;

    if( isset( $query->query_vars[\'post_type\'] ) && ( \'nav_menu_item\' == $query->query_vars[\'post_type\'] ) )
        return; 
    if( 1 < did_action(\'wp\') )
        return;
    global $current_user, $tax_selection;

    $tax_selection = get_user_option( \'taxonomy_selection\', $current_user->ID );

    if( empty( $tax_selection ) )
        return;

    if( isset( $tax_selection[\'categories\'] ) && ( is_array( $tax_selection[\'categories\'] ) ) && ( !empty( $tax_selection[\'categories\'] ) ) ) 
        $query->set( \'category__in\', $tax_selection[\'categories\'] );
    if( isset( $tax_selection[\'post_tag\'] ) && ( is_array( $tax_selection[\'post_tag\'] ) ) && ( !empty( $tax_selection[\'post_tag\'] ) ) ) 
        $query->set( \'tag__in\', $tax_selection[\'post_tag\'] );

    return;
}

front-page.php

  <?php // Create and run custom loop
      global $tax_selection; // use your variable
      $custom_posts = new WP_Query();
      $custom_posts->query( array( 
          \'post_type\' => \'topic\', 
          \'posts_per_page\' => 5, 
          \'category__in\' => $tax_selection[\'categories\'],
      ) );
      while ($custom_posts->have_posts()) : $custom_posts->the_post();
  ?>  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <div class="entry-left">
          <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyeleven\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
        </div><!-- .entry-header -->
      </article>
    <?php endwhile; ?>
  </div>

结束

相关推荐

Getting posts by taxonomy

我想根据现有的分类术语从CPT获取一组帖子。我想我应该使用WP的get\\u posts()函数:$结果=获取文章(数组$选项);当我指定我感兴趣的CPT时,此函数工作正常:$results=get\\u posts(数组(\'post\\u type\'=>\'my\\u cpt\\u name\');但后来我用我的UOM自定义post类型尝试了这一点,并尝试使用公制进行筛选,公制是一种称为“UOM\\U system”的分类法:$results=get\\u posts(数组(\'post\\u