在wordpress博客上全局隐藏(不删除)类别名称

时间:2015-09-30 11:35:34

标签: php wordpress categories

我试图在我的整个wordpress博客上隐藏一个特定的类别名称(例如,名为"精选")。请注意我不想排除属于该特定类别的帖子,我只想阻止实际的类别名称显示任何内容,它应该是不可见的。

我找到了一个代码片段,我把它放在我的functions.php中,实际上它完全符合我的要求,但它只是隐藏了后部分的类别名称,它没有任何对类别名称仍在显示的第三方插件的影响。你可以请帮助,以使这个特定的插件工作吗?

function.php中用于隐藏类别名称的代码:

function the_category_filter($thelist,$separator=' ') {
if(!defined('WP_ADMIN')) {

    $exclude = array('featured');
    $cats = explode($separator,$thelist);
    $newlist = array();
    foreach($cats as $cat) {
        $catname = trim(strip_tags($cat));
        if(!in_array($catname,$exclude))
            $newlist[] = $cat;
    }
    return implode($separator,$newlist);
} else
    return $thelist;
}
add_filter('the_category','the_category_filter',10,2);

Plungin的功能如下:

private function generate_post_categories( $post ) {

    // Setting up category list string.
    $post_cat_string = '';

    // Setting up category list.
    $post_cat_list = '';

    // Fetching post category prefix  with WPML support.
    $post_category_prefix = ($this->icl_t) ? icl_t('ORP Post Category Prefix', 'post-category-prefix–' . $this->widget_id, $this->widget_args['post_category_prefix'] ) : $this->widget_args['post_category_prefix'];

    // Checking for post category PREFIX.
    if ( !empty( $this->widget_args['post_category_prefix'] ) ) {

        // Building post category PREFIX HTML.
        $post_cat_string .= esc_html( $post_category_prefix );
    }

    // Retrieving categories array.
    $orp_categories = get_the_category( $post->ID );

    // Checking if "post category link" option is on.
    if ( 'yes' == $this->widget_args['post_category_link'] ) {

        // Looping through categories array.
        foreach( $orp_categories as $orp_cat ) {

            // Fetching the current category link.
            $orp_category_link = get_category_link( $orp_cat->cat_ID );

            // Building HTML link atts.
            $linkatts = array(
                'href'  => $orp_category_link,
                'title' => $orp_cat->cat_name
            );

            // Building category link HTML.
            $post_cat_list .= $this->orp_create_tag( 'a', $orp_cat->cat_name, $linkatts ) . esc_html( $this->widget_args['post_category_separator'] );
        }

    } else {

        // Looping through categories array.
        foreach( $orp_categories as $orp_cat ) {

            // Filling categories list.
            $post_cat_list .= $orp_cat->cat_name . esc_html( $this->widget_args['post_category_separator'] );
        }
    }

    // Right trimming the last category separator on the category list.
    $post_cat_string .= rtrim( $post_cat_list, esc_html( $this->widget_args['post_category_separator'] ) );

    // Returning the post category HTML.
    return $this->orp_create_tag( 'div', $post_cat_string, array( 'class' => 'orp-post-category' ) );

1 个答案:

答案 0 :(得分:0)

您可以尝试更改此部分:

// Looping through categories array.
foreach( $orp_categories as $orp_cat ) {

致:

// Looping through categories array.
foreach( $orp_categories as $orp_cat ) {
  if($orp_cat->cat_name == 'Featured') {
    continue;
  }

如果类别名称等于“精选”,则应跳过循环。