Wordpress get_the_category() 正在返回我不想要的东西
Wordpress get_the_category() is returning stuff I don't want
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,而不是那里的类别现实。我可以通过插入 arrays[1] category int get_posts's $arg.
来解决
我不想要解决方法,而是可靠的解决方案,如果它们被创建,它也适用于不同的类别,只是正常的存档,在我进入类别 slug 的地方,我可以简单地通过本机分页浏览它等等上,不是我最终必须重写整个 cms 的解决方法。
因此,在 Wordpress 中,您可以将多个类别与给定的 post.
相关联
所以您遇到的是正常行为,您只需 select 只为您想要的 post 设置一个类别即可避免这种情况。
所以这里有一些您可以做的事情
从主 post 查询中排除类别。
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
遍历类别和select当前上下文的重要类别
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>';
}
}
}
?>
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,而不是那里的类别现实。我可以通过插入 arrays[1] category int get_posts's $arg.
来解决我不想要解决方法,而是可靠的解决方案,如果它们被创建,它也适用于不同的类别,只是正常的存档,在我进入类别 slug 的地方,我可以简单地通过本机分页浏览它等等上,不是我最终必须重写整个 cms 的解决方法。
因此,在 Wordpress 中,您可以将多个类别与给定的 post.
相关联所以您遇到的是正常行为,您只需 select 只为您想要的 post 设置一个类别即可避免这种情况。
所以这里有一些您可以做的事情
从主 post 查询中排除类别。
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
遍历类别和select当前上下文的重要类别
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>';
}
}
}
?>