第2页上的自定义帖子类型和自定义分类404

时间:2017-12-14 作者:Matthew Ellis

我的问题和Custom Taxonomy archive returns 404, 我尝试实施解决方案,但仍然发现问题。。。

这是我的CPT

    <?php
    if( ! function_exists( \'videos_create_post_type\' ) ) :
        function videos_create_post_type() {
            $labels = array(
                \'name\' => \'Videos\',
                \'singular_name\' => \'Video\',
                \'add_new\' => \'Add video\',
                \'all_items\' => \'All videos\',
                \'add_new_item\' => \'Add video\',
                \'edit_item\' => \'Edit video\',
                \'new_item\' => \'New video\',
                \'view_item\' => \'View video\',
                \'search_items\' => \'Search videos\',
                \'not_found\' => \'No videos found\',
                \'not_found_in_trash\' => \'No videos found in trash\',
                \'parent_item_colon\' => \'Parent video\'
            );
            $args = array(
                \'labels\' => $labels,
                \'public\' => true,
                \'has_archive\' => \'videos\',
                \'publicly_queryable\' => true,
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => \'videos/%videos_category%\', \'with_front\' => false ),
                \'capability_type\' => \'post\',
                \'hierarchical\' => false,
                \'supports\' => array(
                    \'title\',
                    \'editor\',
                    \'thumbnail\',
                    \'revisions\'
                ),
                \'menu_position\' => 5,
                \'menu_icon\'     => \'dashicons-video-alt3\',
                \'exclude_from_search\' => false
            );
            register_post_type( \'videos\', $args );
            //flush_rewrite_rules();

            register_taxonomy( \'videos_category\', // register custom taxonomy - category
                \'videos\',
                array(
                    \'hierarchical\' => true,
                    \'public\' => true,
                    \'show_admin_column\' => true,
                    \'show_in_nav_menus\' => true,
                    \'rewrite\' => array( \'slug\' => \'videos\', \'with_front\' => false ),
                    \'labels\' => array(
                        \'name\' => \'Video categories\',
                        \'singular_name\' => \'Video category\',
                    )
                )
            );
        }
        add_action( \'init\', \'videos_create_post_type\' );

        function wpa_show_permalinks( $post_link, $post ){
            $args = array(
                \'orderby\'   => \'ID\',
                \'order\'     => \'ASC\'
            );
            if ( is_object( $post ) && $post->post_type == \'videos\' ){
                $terms = wp_get_object_terms( $post->ID, \'videos_category\', $args );
                if( $terms ){
                    return str_replace( \'%videos_category%\' , $terms[0]->slug , $post_link );
                }
            }
            return $post_link;
        }

    endif; // end of function_exists()
?>
我的档案模板上的循环道具设置如下

                <?php
                    global $post;
                    $custom_taxterms = wp_get_object_terms( $post->ID, \'videos_category\', array(\'fields\' => \'ids\') );
                    $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
                    $args = array(
                        \'posts_per_page\' => 1,
                        \'is_paged\' => true,
                        \'post_type\' => \'videos\',
                        \'paged\' => $paged,
                        \'tax_query\' => array(
                            array(
                                \'taxonomy\' => \'videos_category\',
                                \'field\' => \'id\',
                                \'terms\' => $custom_taxterms
                            )
                        ),
                    );
                    $custom_query = new WP_Query( $args );
                ?>
在while循环中,我刚刚通过

                @while ($custom_query->have_posts()) @php($custom_query->the_post())
                    @include(\'partials.content-\'.get_post_type())
                @endwhile
我正在使用刀片模板,以防你对@while等感到疑惑。

非常感谢您的帮助:)谢谢!

1 个回复
最合适的回答,由SO网友:Matthew Ellis 整理而成

因此,在多次尝试和错误之后,我发现我必须创建一个特定的重写规则才能进入我的函数。php,一个处理post类型\'videos\' 以及类别(分类法)\'videos_category\':

add_filter(\'init\', function() {
    add_rewrite_rule( 
        \'^videos/([^/]*)/([^/]*)/(\\d*)?\',
        \'index.php?videos_category=$matches[1]&p=$matches[2]&paged=$matches[3]\',
        \'top\' 
     ); 
});
我还发现这个插件对查看我的重写规则非常有帮助:https://wordpress.org/plugins/rewrite-rules-inspector/

结束