【问题标题】:Wordpress get_the_category() is returning stuff I don't wantWordpress get_the_category() 正在返回我不想要的东西
【发布时间】:2017-09-13 09:02:01
【问题描述】:

archive.php 文件中的 Wordpress 的 get_the_category() 在显示类别 slug url 时返回两个类别的数组,一个不同的,与我使用 slug 的“aktuality-blog”类别无关。

这里是var_dump( get_the_category() );

array(2) {
  [0]=>
  object(WP_Term)#4190 (16) {
    ["term_id"]=>
    int(2)
    ["name"]=>
    string(9) "Aktuality"
    ["slug"]=>
    string(9) "aktuality"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(2)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(18)
    ["filter"]=>
    string(3) "raw"
    ["cat_ID"]=>
    int(2)
    ["category_count"]=>
    int(18)
    ["category_description"]=>
    string(0) ""
    ["cat_name"]=>
    string(9) "Aktuality"
    ["category_nicename"]=>
    string(9) "aktuality"
    ["category_parent"]=>
    int(0)
  }
  [1]=>
  object(WP_Term)#4188 (16) {
    ["term_id"]=>
    int(30)
    ["name"]=>
    string(4) "Blog"
    ["slug"]=>
    string(14) "aktuality-blog"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(30)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
    ["cat_ID"]=>
    int(30)
    ["category_count"]=>
    int(1)
    ["category_description"]=>
    string(0) ""
    ["cat_name"]=>
    string(4) "Blog"
    ["category_nicename"]=>
    string(14) "aktuality-blog"
    ["category_parent"]=>
    int(0)
  }
}

我希望只有数组 1 是数组 0,而不是那里的类别实际情况。我可以通过插入数组 [1] 类别 int get_posts 的 $arg 来解决问题。

我不想要变通方法,而是可靠的解决方案,如果创建它们,它也适用于不同的类别,只是普通的存档,在我进入类别 slug 的地方,我可以简单地通过原生分页浏览它等等,不是解决方法,我最终将不得不重写整个 cms。

【问题讨论】:

  • 你在使用默认的 WP 主题吗?
  • 不,正在编写自定义... 功能完全没有变化。

标签: php wordpress categories


【解决方案1】:

因此,在 Wordpress 中,您可以将多个类别与给定的帖子关联。

因此,您遇到的是正常行为,您只需为所需的帖子选择一个类别即可避免这种情况。

所以这里有一些你可以做的事情

从主要帖子查询中排除一个类别。

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

遍历类别并为当前上下文选择重要的类别

foreach(get_the_category() as $term){
    if($term->slug == 'aktuality'){ 
        // do something
    }
}

可能是这个?

安装 Yoast 插件以允许主类别或使用独立于插件的分叉代码。

https://joshuawinn.com/using-yoasts-primary-category-in-wordpress-theme/

分叉代码(没有亲自测试过)

<?php 
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category();
$useCatLink = true;
// If post has a category assigned.
if ($category){
    $category_display = '';
    $category_link = '';
    if ( class_exists('WPSEO_Primary_Term') )
    {
        // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
        $wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
        $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
        $term = get_term( $wpseo_primary_term );
        if (is_wp_error($term)) { 
            // Default to first category (not Yoast) if an error is returned
            $category_display = $category[0]->name;
            $category_link = get_category_link( $category[0]->term_id );
        } else { 
            // Yoast Primary category
            $category_display = $term->name;
            $category_link = get_category_link( $term->term_id );
        }
    } 
    else {
        // Default, display the first category in WP's list of assigned categories
        $category_display = $category[0]->name;
        $category_link = get_category_link( $category[0]->term_id );
    }
    // Display category
    if ( !empty($category_display) ){
        if ( $useCatLink == true && !empty($category_link) ){
        echo '<span class="post-category">';
        echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
        echo '</span>';
        } else {
        echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>';
        }
    }

}
?>

【讨论】:

  • 我可以有一个独立于帖子的类别进行搜索吗?
  • @maztt 类别项目是一个公正的术语,然后您将其与帖子相关联。所以理论上你应该能够做一个类别术语,并在该类别页面上调用钩子pre_get_posts 来更改查询。
猜你喜欢
  • 2021-04-07
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多