gpt4 book ai didi

wordpress - 使用单个类别显示所有帖子和自定义帖子类型

转载 作者:行者123 更新时间:2023-12-04 14:56:03 24 4
gpt4 key购买 nike

我用 CPT UI 创建了一个帖子类型:

add_action( 'init', 'cptui_register_my_cpts_matratze' );
function cptui_register_my_cpts_matratze() {
$labels = array(
"name" => __( 'Matratzen', '' ),
"singular_name" => __( 'Matratze', '' ),
);

$args = array(
"label" => __( 'Matratzen', '' ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "matratze", "with_front" => true ),
"query_var" => true,

"supports" => array( "title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "author", "page-attributes", "post-formats" ),
"taxonomies" => array( "category", "post_tag" ),
);
register_post_type( "matratze", $args );

// End of cptui_register_my_cpts_matratze()
}

但是,当我想通过前端中的链接访问类别时,我没有收到任何帖子。

例如,当你点击我什么也得不到:

Category

该帖子已发布,类别为 DaMi :

Post

是我的 CPT UI Post Type配置错误?任何建议我做错了什么?

最佳答案

查看here

默认情况下,您的 WordPress 网站上的类别页面将仅显示默认的“帖子”帖子类型,因此您需要在 Wordpress 通过添加 pre_get_posts 过滤器查询帖子之前添加您的 CPT。

在此处添加代码:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'post', 'matratze'); // don't forget nav_menu_item to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}

关于wordpress - 使用单个类别显示所有帖子和自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39717837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com