Wordpress:禁用get_the_category_list函数的链接?

Joh*_*911 4 php wordpress

嘿我的主题使用此功能来显示帖子的类别,但它也创建了我想要摆脱的链接.我更喜欢PHP而不是JavaScript解决方案.

这是代码:

<p class="postmeta">
    <?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
        <?php
            /* translators: used between list items, there is a space after the comma */
            $categories_list = get_the_category_list( __( ', ', 'gridster' ) );
            if ( $categories_list && gridster_categorized_blog() ) :
        ?>
            <?php /*printf( __( '%1$s', 'gridster' ), $categories_list );*/ echo $categories_list; ?>
        <?php endif; // End if categories ?>


    <?php endif; // End if 'post' == get_post_type() ?>
</p>
Run Code Online (Sandbox Code Playgroud)

链接到codex参考

我该如何取消这些链接?

或者从DOM中删除所有链接(但这可能会创建更多工作,因为实际文本位于<a>标记之间

HTML

 <p class="postmeta"> 
    <a target="_blank" href="http://whatever.com/category/default/" title="View all posts in Default" rel="category tag">Default</a>
</p>
Run Code Online (Sandbox Code Playgroud)

谢谢!!

小智 8

如果您只想返回一个文本字符串并使用本机get_the_categories()函数,您只需将其包装在PHP strip_tags()函数中即可删除返回的所有HTML:

echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));
Run Code Online (Sandbox Code Playgroud)

  • 供其他人复制/粘贴的简化版本:`echo strip_tags(get_the_category_list(', '));` (3认同)

agr*_*grm 3

您可以尝试修改它以满足您的需求:

<?php
foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
}
?>
Run Code Online (Sandbox Code Playgroud)

(来自http://premium.wpmudev.org/blog/how-to-get-a-wordpress-category-name-without-the-link/