我正在尝试根据分配给帖子的自定义分类术语显示一些摘要(通过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
)
)
我当时正在考虑遍历一组分配的术语并加载适当的代码片段。片段的名称与分类术语的“子弹”相同。
$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之前先检查文件是否存在?
谢谢
答案 0 :(得分:1)
您正在尝试将对象值作为数组访问,因此它不会回显该值。正确使用以下代码进行回显。
foreach($term_list as $key => $term) {
$term_slug = $term->slug; // does not out put anything.
get_template_part( 'modules/'.$term_slug.'.php' );
}
有关更多帮助,请参见以下链接:Click Here 谢谢