如何仅显示帖子的父类别。它包含两个子类别,我想消除它。
示例:myblog。com/parent\\u类别/post\\u名称
这只是我想要的。
如何仅显示帖子的父类别。它包含两个子类别,我想消除它。
示例:myblog。com/parent\\u类别/post\\u名称
这只是我想要的。
您可以在此处使用post_link
过滤器挂钩。
在函数返回已处理的URL之前,此筛选器将应用于帖子的永久链接URLget_permalink
. 现在,我们正在修改它以满足我们的要求,即只显示子类别的父类别,然后后跟帖子名称。
if ( ! is_admin() ){
add_filter( \'post_link\', \'custom_permalink\', 10, 3 );
}
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
$parent_category = get_category($category[0]->parent);
if ( !empty($category) && $category[0]->parent == "271" ) {
$permalink = trailingslashit( site_url(\'/\'.$parent_category->slug.\'/\'.$post->post_name.\'/\' ) );
}
return $permalink;
}
我的数据库中有很多类别,但当我打电话时wp_list_categories() 它没有列出任何类别,只是输出Categories No categories 此外,我正在尝试使用get_categories() 但还是一样的问题,它的输出就像没有类别一样