WordPress迁移后category-slug.php不工作


category-slug.php not working after WordPress migration

我有一个问题,我试图解决几个小时前在网上搜索,但现在不能。欢迎有任何想法或线索…

我正在尝试迁移一个WordPress网站,使用插件(CCTM(没有更多的开发活动))注册自定义帖子类型和字段"recetas",使用原生WordPress类别"recetas"在他们的帖子。

在新版本中,我在functions.php上手动注册自定义帖子类型,并通过WordPress的原生XML导入工具导入内容。

add_action( 'init', 'codex_book_init' );
function codex_book_init() {
    $labels = array(
        'name'               => _x( 'Recetas'),
        'singular_name'      => _x( 'Receta'),
        'menu_name'          => _x( 'Recetas'),
        'name_admin_bar'     => _x( 'Recetas'),
        'add_new'            => _x( 'Agregar Nueva'),
        'add_new_item'       => __( 'Agregar Nueva Receta'),
        'new_item'           => __( 'Nueva Receta'),
        'edit_item'          => __( 'Editar Receta'),
        'view_item'          => __( 'Ver Receta'),
        'all_items'          => __( 'Todas las Recetas'),
        'search_items'       => __( 'Buscar Receta'),
        'parent_item_colon'  => __( 'Receta Padre:'),
        'not_found'          => __( 'Sin Recetas encontradas.'),
        'not_found_in_trash' => __( 'Sin Recetas encontradas en papelera.')
    );
    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Recetas'),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'recetas'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 5,
        'menu_icon'          => 'dashicons-admin-post',
        'taxonomies'         => array( 'category' ),
        'supports'           => array( 'title', 'thumbnail', 'excerpt', 'editor', 'comments')
    );
    register_post_type( 'recetas', $args ); 
}

在自定义帖子类型的单个文章中,所有内容都获得ok,并且在新循环WP_Query( array('posts_type'=> 'recetas')中,内容也获得ok。但是问题出现在类别模板(category-recetas.php)中,用于获取默认wordpress循环<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>的帖子类型文章。它根本不起作用,没有一个帖子来自"recetas"类别。

我尝试注册自定义分类法"recetas",尝试category-id.php,尝试archive-slug.php,尝试保存永久链接,但没有任何作用…

已解决感谢@Max Yudin从wordpress。Stackexchange -他的回答解决了这个问题。链接

@Max的回答

Category是仅用于文章的内置分类法,而不是自定义文章类型。所以你必须调用pre_get_posts钩子。

这个钩子在创建查询变量对象之后,但在实际查询运行之前被调用。将以下代码放入functions.php或自定义插件中。

<?php
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 = array('nav_menu_item', 'post', 'recetas'); // don't forget nav_menu_item to allow menus to work!
        }
        $query->set('post_type', $post_type);
        return $query;
        }
}