【问题标题】:WP - get_template_part based on matching taxonomy term(s)WP - 基于匹配分类术语的 get_template_part
【发布时间】: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 之前检查文件是否存在?

谢谢

【问题讨论】:

    标签: wordpress custom-taxonomy


    【解决方案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 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多