【问题标题】:Add custom taxonomy terms as classes将自定义分类术语添加为类
【发布时间】:2021-04-27 07:59:12
【问题描述】:

我无法将后分类术语显示为类。我知道我过去做过,但我似乎找不到它,也不记得怎么做。

我有一个自定义分类“thema”,现在我想将相应的主题术语作为类添加到存档页面上的每个帖子中。

我可以列出帖子的所有术语,但是当我想将其作为类输出时,页面会从帖子循环开始的位置停止加载。

这是我目前所拥有的:

(编辑:更改了一些代码以在错误发生时显示)

while ( $query->have_posts() ) {
        $query->the_post();
        $f = get_fields();

        $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());

        $terms = wp_get_post_terms( get_the_ID(), 'thema');


        echo "<div class='post ". foreach ($terms as $t) { echo $t->slug, ' '; } ."'>";
            echo "<div class='postWrapper'>";
                echo "<div class='content'>";
                    echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";

                    echo the_excerpt_max_charlength($charlength = 130);
                    echo "<a href='".$link."' title='Ga naar ".get_the_title()."' class='link'>Lees meer</a>";
                echo "</div>";
            echo "</div>";
        echo "</div>";
    }

【问题讨论】:

  • 不要只说“分页符”,这不是正确的问题描述。
  • 我没有看到你甚至试图将术语输出为任何元素的 classes?您只是在 post 元素 HTML 之外回显它们……?
  • 另外,使用标签 slugs 而不是名称,在不创建无效类的可能性方面可能会“更安全”。
  • 这就是我卡住的地方。当我尝试将术语输出为带有后级的 div 上的类时,帖子没有加载。
  • 告诉我们您甚至没有实际向我们展示的代码产生的错误有什么意义?向我们展示您当时究竟尝试了什么,而不是执行其他操作的代码。到目前为止,您做了什么来尝试和调试它?

标签: php jquery wordpress custom-taxonomy


【解决方案1】:

你可以试试这个方法:

$classes = '';
$terms = wp_get_post_terms( get_the_ID(), 'thema');

foreach($terms as $t) {
    $classes .= $t->slug . ' ';
}

echo "<div class='post ". $classes ."'>";

应该可以的。希望能帮到你。

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 2015-06-29
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2021-11-29
    • 2020-01-04
    • 2019-06-09
    相关资源
    最近更新 更多