主题选项中自定义分类的QUERY_POST有问题

时间:2013-02-12 作者:Dean McCann

我在主题选项中查询自定义分类法的输出时遇到一些问题。

我已经在下面列出了我认为必要的东西。

非常感谢您的帮助。

// Pull all the custom taxonomies into an array

$options_taxonomies = array();
$taxonomies_terms_obj = get_terms(\'portfolio_category\');
foreach ($taxonomies_terms_obj as $taxonomy) {
    $options_taxonomies[$taxonomy->term_id] = $taxonomy->name;
}

// Select a Featured Homepage Category

$options[] = array(
    \'name\' => __(\'Featured Homepage Category\', \'options_framework_theme\'),
    \'desc\' => __(\'Choose a category to feature on your homepage.\', \'options_framework_theme\'),
    \'id\' => \'homepage_feature\',
    \'type\' => \'select\',
    \'options\' => $options_taxonomies);
}

//The query

if ( function_exists( \'of_get_option\' ) ) :
    query_posts( \'cat=\' . $homepage_feature = of_get_option( \'homepage_feature\' ) . \'&posts_per_page=3\' ); 

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

我看到三件事。

if ( function_exists( \'of_get_option\' ) ) :
    query_posts( \'cat=\' . $homepage_feature = of_get_option( \'homepage_feature\' ) . \'&posts_per_page=3\' ); 
您有语法错误。没有endif;. 也许这是个拼写错误,但也许不是。如果你想写一行if, 移除冒号cat= is for categories, 不general taxonomies. 参见法典使用WP_Query, 不query_posts假设其余代码正常工作,并且假设我读对了,您的查询应该是:

if ( function_exists( \'of_get_option\' ) ) {
  $args = array(
    \'posts_per_page\' => 3,
    \'tax_query\' => array(
         array(
            \'taxonomy\' => \'your-taxonomy-name\',
            \'field\' => \'id\', // or slug; I don\'t know which you have
            \'terms\' => of_get_option( \'homepage_feature\' )
         )
     )
  );
  $my_qry = new WP_Query($args); 
}

结束

相关推荐