我的问题和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等感到疑惑。
非常感谢您的帮助:)谢谢!