我在主题选项中查询自定义分类法的输出时遇到一些问题。
我已经在下面列出了我认为必要的东西。
非常感谢您的帮助。
// 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\' );
最合适的回答,由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);
}