WordPress的:对于get_the_category_list功能禁用链接?

问题描述 投票:2回答:5

嘿,我的主题使用该功能显示后具有的类别,但是也带来了,我想摆脱的联系。我更喜欢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>

Link to the codex reference

我怎样才能作废这些链接?

或者从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>

谢谢!!

php wordpress
5个回答
8
投票

如果你只是想要一个文本字符串返回,并使用原生get_the_categories()功能,你可以简单地把它包装PHP's strip_tags() function删除返回所有的HTML:

echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));

3
投票

你可以尝试修改这个满足您的需求:

<?php
foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
}
?>

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


0
投票

一种选择是copy the original function和适应您的需求。修改删除<a>标签,但没有测试,比较原始的,适应如果需要的话,并把它添加到主题的functions.php

function category_list_so_22771587( $separator = '' ) {
    global $wp_rewrite;
    $categories = get_the_category( $post_id );
    $thelist = '';
    $i = 0;
    foreach ( $categories as $category ) {
        if ( 0 < $i )
            $thelist .= $separator;
        switch ( strtolower( $parents ) ) {
            case 'multiple':
                if ( $category->parent )
                    $thelist .= get_category_parents( $category->parent, true, $separator );
                $thelist .= $category->name;
                break;
            case 'single':
                if ( $category->parent )
                    $thelist .= get_category_parents( $category->parent, false, $separator );
                $thelist .= $category->name;
                break;
            case '':
            default:
                $thelist .= $category->name;
        }
        ++$i;
    }
    return $thelist;
}

并修改模板:

$categories_list = category_list_so_22771587( __( ', ', 'gridster' ) );

0
投票

您可以检索类别,提取每个项目的“名称”栏,并用逗号破灭每个值:

 <?= implode(', ', (array_column(get_the_category(), 'name')))?>

或者你可以检索与链接类别列表,删除标签:

 <?= strip_tags(get_the_category_list(', ')) ; ?>

0
投票

你只想隐藏链接?如果是使用CSS:

.postmeta a {display: none}

编辑,也可以“禁用”,在CSS这些链接:

.postmeta a {
  pointer-events: none;
  cursor: default;
}
© www.soinside.com 2019 - 2024. All rights reserved.