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

标签 php wordpress

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

我正在尝试迁移一个使用插件(CCTM(没有更多的开发事件))的 WordPress 网站来注册自定义帖子类型和字段“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 ); 
}

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

我尝试注册自定义分类法“recetas”,尝试 category-id.php , 尝试 archive-slug.php ,尝试重新保存永久链接,但没有任何效果......

最佳答案

已解决 感谢来自 wordpress.stackexchange 的@M​​ax Yudin - 他的回答解决了这个问题。 LINK

来自@Max的回答

Category is the built-in taxonomy for posts only, not custom post types. So you have to call the pre_get_posts hook.

This hook is called after the query variable object is created, but before the actual query is run. Place the following code to the functions.php or a custom plugin. Not tested though.

<?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;
        }
}

关于php - WordPress 迁移后 category-slug.php 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39917743/

相关文章:

php - 在 Ubuntu 上的 php 5.6 上安装 php-zip

php - 避免在编辑时两次显示下拉值

php - 如何在PHP中的特定字/字符限制后添加“…”?

php - WordPress 不支持 .htaccess

wordpress - Azure 上的全新 WordPress 安装无法更新到 3.7

小部件中的 Wordpress 媒体 uploader

php - 重定向功能 Php

php - Imagemagick 不在 php 中工作,但在终端中工作

php - Laravel连接MSSqL服务器可以与cli一起使用,但不能在浏览器中使用

javascript - 如何使用 wp_enqueue_script WordPress 将脚本添加为模块