如何将nofollow属性添加到WordPress类别列表


How to add nofollow attribute to wordpress category list?

好吧,此代码获取类别列表并将其显示在分配帖子的主题中。我想将nofollow标签添加到此列表中。我在网上冲浪,找不到解决方案。我找到的唯一解决方案是修改wordpress核心文件。但我不想修改核心文件。

<footer class="entry-meta">
        <?php
            /* translators: used between list items, there is a space after the comma */
            $category_list = get_the_category_list( __( ', ', 'basically' ) );

            $meta_text = __( 'Category: %1$s', 'basically' );
            printf(
                $meta_text,
                $category_list,
                get_permalink(),
                the_title_attribute( 'echo=0' )
            );
        ?>

还有其他方法吗?

<?php
foreach( (get_the_category() ) as $category ) {
    $category_link[] = '<a href="' . get_category_link( $category->cat_ID ) . '"'
                     . ' title="' . $category->cat_name . '" rel="nofollow">'
                     . $category->cat_name . '</a>';
}
printf( __( 'Category: %1$s', 'basically' ), implode( ', ', $category_link ) );
?>

它将在 the_loop() 内工作,要在循环外使用它,您必须在 get_the_category() 上提供帖子 ID,因此它应该get_the_category( $post->ID )

也试试这个:

<footer class="entry-meta">
        <?php
            /* translators: used between list items, there is a space after the comma */
            $cat_list = get_the_category_list( __( ', ', 'basically' ) );
            $category_list = str_replace('rel="category tag"','rel="category tag nofollow"',$cat_list);    
            $meta_text = __( 'Category: %1$s', 'basically' );
            printf(
                $meta_text,
                $category_list,
                get_permalink(),
                the_title_attribute( 'echo=0' )
            );
        ?>