Php 将自定义文章类型分类显示为存档页
您好,我有一个自定义的帖子类型,名为Php 将自定义文章类型分类显示为存档页,php,wordpress,Php,Wordpress,您好,我有一个自定义的帖子类型,名为results。我还使用分类法为特定的帖子类型创建了类别。我不确定我是否正确设置了它,但我的代码是有效的,所以我坚持使用它。如果您看到更好的方法或任何错误,请让我知道 我可以创建一个自定义帖子并设置一个类别。接下来,我想创建一个categories页面,它的作用类似于regular archive.php,但只针对自定义文章类型的类别 因此,假设我有一个用于结果的自定义帖子,并且我将其类别设置为车祸,我希望有一种方法可以像archive.php用于普通帖子一样
results
。我还使用分类法为特定的帖子类型创建了类别。我不确定我是否正确设置了它,但我的代码是有效的,所以我坚持使用它。如果您看到更好的方法或任何错误,请让我知道
我可以创建一个自定义帖子并设置一个类别。接下来,我想创建一个categories页面,它的作用类似于regular archive.php,但只针对自定义文章类型的类别
因此,假设我有一个用于结果的自定义帖子
,并且我将其类别设置为车祸
,我希望有一种方法可以像archive.php用于普通帖子一样显示它们
我试图访问这样的url,但我被发送到404页面,即使我有一个archive-results.php
www.myurl.com/results/categories/car contractions
下面是我用来设置自定义帖子类型和分类法的代码。抱歉,如果它很长,但我觉得有必要包括一切
// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies' => array( 'results', 'result-category' ),
)
);
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );
我是否缺少阻止此url工作的内容
www.myurl.com/results/categories/car contractions
提前谢谢
// Create custom post type
function create_posttype() {
register_post_type( 'Results',
array(
'labels' => array(
'name' => __( 'Results' ),
'singular_name' => __( 'Results' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'results'),
'taxonomies' => array( 'results', 'result-category' ),
)
);
flush_rewrite_rules();
}
add_action( 'init', 'create_posttype' );
//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
'results-categories',
'results',
array(
'label' => __( 'Result Categories' ),
'rewrite' => array( 'slug' => 'result-category' ),
'hierarchical' => true,
'has_archive' => true
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );
我刚刚做了这些更改,请您将其复制到您的代码中,看看它是否运行良好首先显示您需要使用taxonomy.php的类别。。其中所有自定义帖子类型的类别都可以控制,然后使用flush_rewrite_rules();创建自定义帖子类型时。。我想这就是你的url转到404页面的原因。当然,我创建了taxonomy.php,现在我该如何查看页面?那个url仍然发送到404,我试过你的代码,它已经工作了,只需使用flush_rewrite_rules();注册后,根据您的设置查找的url是:www.myurl.com/result-category/car-contractions,因为这就是您设置重写的方式。它将由一个名为taxonomy-results-categories.php的文件控制,或者返回到taxonomy.php,然后返回index.php。所以我应该能够转到这个url?/results/categories/car contracts
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'cmc-description'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );