【发布时间】:2018-08-18 10:31:08
【问题描述】:
我正在尝试根据分配给帖子的自定义分类术语显示一些 sn-ps(通过 get_template_part() 加载)。
到目前为止,我可以将分类术语分配给帖子
$term_list = wp_get_post_terms($post->ID, 'sidebar_snippets', array("fields" => "all"));
print_r($term_list);
这会产生一个这样的对象数组:
(
[0] => WP_Term Object
(
[term_id] => 11
[name] => Future Events
[slug] => future_events
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => sidebar_snippets
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)
我正在考虑迭代一组分配的术语并加载适当的 sn-ps。 sn-ps 的名称与分类术语的 'slug' 相同。
$term_list = wp_get_post_terms($post->ID, 'sidebar_modules', array("fields" => "all"));
print_r($term_list); // works fine - outputs three terms (like above)
foreach($term_list as $term) {
echo $term['slug']; // does not out put anything.
get_template_part( 'modules/' . $term['slug] . '.php' );
}
我有两个问题。它甚至不输出 $term[slug]。其次,我将如何添加一些验证,例如。在尝试 get_template_part 之前检查文件是否存在?
谢谢
【问题讨论】: