簡體   English   中英

在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 ); 
}

所有內容都可以從自定義帖子類型的單篇文章中獲得,並且在新循環中WP_Query( array('posts_type'=> 'recetas')內容也可以。但問題出現在類別模板中(category-recetas.php )用於獲取帶有默認wordpress循環<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>的帖子類型文章<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 。它簡單不起作用,沒有一個帖子來自類別“ recetas”。

我嘗試注冊custom-taxonomy“recetas”,嘗試使用category-id.php ,嘗試archive-slug.php ,嘗試重新保存永久鏈接,但沒有任何作用......

從wordpress.stackexchange 解決由於@Max尤金-他的回答解決了該問題。 鏈接

來自@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;
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM