>
我在function.php中创建了一个自定义post类型。
我为自定义帖子类型添加了类别功能。
我为前任创造的。 员额1和员额2。
我已经创建了2个类别1,2个类别。
并且在wp面板中设置post 1有类别1,post 2有类别2,但是post没有显示在类别页面上,我尝试编辑category.php,但是什么也没有发生。
你对如何解决这个有什么想法吗? 我应该向category.php添加哪个函数?
functions.php
/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'news',
// CPT Options
array(
'labels' => array(
'name' => __( 'news' ),
'singular_name' => __( 'News' )
),
'taxonomies' => array('category'), //add this....
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'news'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
/* Edit custom post type */
function cw_post_type_news() {
$supports = array(
'title', // post title
'editor', // post content
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'revisions', // post revisions
'post-formats', // post formats
);
$labels = array(
'name' => _x('Produkty', 'plural'),
'singular_name' => _x('Produkt', 'singular'),
'menu_name' => _x('Produkty', 'admin menu'),
'name_admin_bar' => _x('produkty', 'admin bar'),
'add_new' => _x('Dodaj', 'add new'),
'add_new_item' => __('Dodaj produkt'),
'new_item' => __('Nowy '),
'edit_item' => __('Edytuj '),
'view_item' => __('Zobacz'),
'all_items' => __('Wszystkie produkty'),
'search_items' => __('Wyszukaj'),
'not_found' => __('Brak'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('news', $args);
}
add_action('init', 'cw_post_type_news');
您需要为您的自定义post类型创建自定义分类法,因为如果category.php调用post type=post,那么您的post类型就不会进入查询。
自定义分类法示例:
function taxonomies_examle() {
$labels = array(
'name' => _x( 'Example', 'Example' ),
'singular_name' => _x( 'Example', 'Example' ),
'search_items' => __( 'Search Example' ),
'all_items' => __( 'All Example' ),
'parent_item' => __( 'Parent Example' ),
'parent_item_colon' => __( 'Parent Example:' ),
'edit_item' => __( 'Edit Example' ),
'update_item' => __( 'Update Example' ),
'add_new_item' => __( 'Add New Example' ),
'new_item_name' => __( 'New Example' ),
'menu_name' => __( 'Example' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true, // it's not public, it shouldn't have it's own permalink, and so on
'publicly_queriable' => true, // you should be able to query it
'show_ui' => true, // you should be able to edit it in wp-admin
'exclude_from_search' => false, // you should exclude it from search results
'show_in_nav_menus' => true, // you shouldn't be able to add it to menus
'has_archive' => true, // it shouldn't have archive page
'rewrite' => true, // it shouldn't have rewrite rules
'supports' => array('title', 'thumbnail'),
);
register_taxonomy( 'examle', 'your-custom-post-type', $args );
}
add_action( 'init', 'taxonomies_examle', 0);
然后,您可以创建taxonomy-example.php,并且可以从类别中查询您的帖子。
从自定义post类型显示post的好方法是,您需要使用模板文件taxonomy-{taxonomy}。php或taxonomy-{taxonomy}-{term}。php。
但实际上,使用category.php或taxonomy.php可能真的不是您想要做的。 您尝试过使用archive-{post-type}。php来显示自定义Post类型的Post的存档索引列表吗?
如果您使用的是SEO友好的permalinks,那么您的CPT的URL很可能是这样的:
http://example.com/movies
如果您没有使用SEO友好的permalinks,那么您的自定义帖子类型URL将是这样的:
http://example.com/?post_type=电影
不要忘记将example.com替换为您自己的域名,将movies替换为您自定义的帖子类型名称。
但如果需要在默认类别页面中显示post,则需要这样做:
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'post', 'news' ) );
return $query;
}