我创建了一个自定义类型,包含类别和标记:
function servicios_taxonomy() {
register_taxonomy(
'servicios_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'servicios', //post type name
array(
'hierarchical' => true,
'label' => 'Categorías servicios', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'servicios', // This controls the base slug that will display before each term
'with_front' => false // Display the category base before
)
)
);
}
add_action( 'init', 'servicios_taxonomy');
function create_posttype() {
$args = array(
'labels' => array(
'name' => __( 'Servicios' ),
'singular_name' => __( 'Servicio' )
),
'rewrite' => array('slug' => 'servicios'),
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'thumbnail'),
'taxonomies' => array('servicios_categories', 'post_tag'),
'has_archive' => true,
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page'
);
register_post_type( 'servicios',$args);
}
add_action( 'init', 'create_posttype' );
我可以访问网址http://example.com/servicios
中的所有自定义帖子,但在菜单编辑器中添加带有 servicios 类别的菜单条目时,我会以http://example.com/category-slug/servicios
的形式获取网址,这会导致a 抱歉,您的搜索未返回任何结果页面。
当我点击此自定义帖子类型视图中的标记链接http://example.com/tag/custom-post-tag/
时,会发生同样的情况。
如何按类别或标记列出自定义帖子类型?
谢谢
答案 0 :(得分:0)
您可以通过在functions.php
文件中包含以下代码来修改类别和存档页面的Wordpress查询,以包含常规帖子和自定义帖子类型:
function my_post_queries( $query ) {
if (!is_admin() && $query->is_main_query()){
// alter the query for the archive and category pages
if( is_category() || is_archive() ){
$query->set('post_type', array('servicios', 'post') );
$query->set('post_status', 'publish');
$query->set('orderby', 'date');
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
(这可以包括更多设置/条件/参数,但基本上看起来如何)