【问题标题】:category-slug.php not working after WordPress migrationWordPress 迁移后 category-slug.php 无法正常工作
【发布时间】:2017-06-10 01:02:23
【问题描述】:

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

我正在尝试迁移一个 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 ); 
}

自定义帖子类型的单篇文章的所有内容都可以,并且在新循环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 网站上获得更好的成功。
  • 尝试重新保存您的永久链接。
  • 我尝试了但没有任何反应

标签: php wordpress


【解决方案1】:

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

@Max 的回答

类别是仅为帖子的内置分类,而不是自定义帖子类型。所以你必须调用 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;
        }
}

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多