Wordpress 自定义帖子类型和类别
几天来,我一直在尝试创建一个带有类别的自定义帖子类型。到目前为止,我已经完成了这项工作,我可以轻松地添加内容,并将其分配到一个类别。我的代码如下 我不明白的是,似乎无法创建一个归档页面来显示某个类别的帖子。Wordpress 自定义帖子类型和类别,wordpress,custom-post-type,Wordpress,Custom Post Type,几天来,我一直在尝试创建一个带有类别的自定义帖子类型。到目前为止,我已经完成了这项工作,我可以轻松地添加内容,并将其分配到一个类别。我的代码如下 我不明白的是,似乎无法创建一个归档页面来显示某个类别的帖子。 例如:我的帖子类型称为广告。我的类别是摄影师 页面是否可以动态计算出您所在的类别,并显示属于该类别的所有自定义帖子 function my_custom_post_advert() { $labels = array( 'name' =&g
例如:我的帖子类型称为广告。我的类别是摄影师 页面是否可以动态计算出您所在的类别,并显示属于该类别的所有自定义帖子
function my_custom_post_advert() {
$labels = array(
'name' => _x( 'Adverts', 'post type general name' ),
'singular_name' => _x( 'Advert', 'post type singular name' ),
'add_new' => _x( 'Add New', 'advert' ),
'add_new_item' => __( 'Add New Advert' ),
'edit_item' => __( 'Edit Advert' ),
'new_item' => __( 'New Advert' ),
'all_items' => __( 'All Adverts' ),
'view_item' => __( 'View Advert' ),
'search_items' => __( 'Search Adverts' ),
'not_found' => __( 'No adverts found' ),
'not_found_in_trash' => __( 'No adverts found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Adverts'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our adverts and advert specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'category' ),
'has_archive' => true,
);
register_post_type( 'advert', $args );
}
add_action( 'init', 'my_custom_post_advert' );
function my_taxonomies_advert() {
$labels = array(
'name' => _x( 'Advert Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Advert Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Advert Categories' ),
'all_items' => __( 'All Advert Categories' ),
'parent_item' => __( 'Parent Advert Category' ),
'parent_item_colon' => __( 'Parent Advert Category:' ),
'edit_item' => __( 'Edit Advert Category' ),
'update_item' => __( 'Update Advert Category' ),
'add_new_item' => __( 'Add New Advert Category' ),
'new_item_name' => __( 'New Advert Category' ),
'menu_name' => __( 'Advert Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'advert_category', 'advert', $args );
}
add_action( 'init', 'my_taxonomies_advert', 0 );
为了省去很多麻烦,我过去用过的是——它就像一个符咒: 通过添加内容类型、自定义字段和分类法,可以自定义WordPress管理员。您将能够手工制作WordPress管理员,并将其转换为您自己的内容管理系统 我用这个: 该插件将支持自定义帖子类型的存档(也包括年度、月度和每日),以及提要、自定义标题和分页
您应该能够导航到/广告。另外,
has_archive
应该为您创建一个归档页面。解决方案基本上包含在StackOverflow上其他地方的答案中
总之,构建一个自定义查询,但在$args数组中替换:
'cat_name'=>“摄影师”
使用分类查询,如下所示:
'tax\u query'=>数组(
排列(
“分类法”=>“广告类别”,
'字段'=>'段塞',
“术语”=>“摄影师”
)
)
当然,您还应该在$args中包含
'post-type'=>'advert'
。希望这有帮助 您基本上需要创建一个包含自定义wp_查询的页面模板,以便WordPress可以确定您属于哪个类别
一旦你创建了你的页面模板,你就可以在你的WordPress管理员中创建一个页面。。。选择新页面模板作为模板
如果您希望类别是动态的,您可以始终将页面模板设置为接受$\u GET参数,以确定显示广告的类别。像这样:
http://example.com/adverts-listing/?mycat=photographers
或
等等
下面是一个页面模板的示例(当然,这会因您使用的主题而异…此示例是为使用Twenty14主题而构建的):
因此,我还需要自定义带有类别的帖子类型
下面的代码非常简单明了。按字面意思复制并粘贴。然后根据你的需要进行调整。希望这对未来的人们有所帮助
它基本上将普通的wordpress类别与您的自定义帖子类型链接起来。使您的客户在wordpress管理部分工作时非常容易。它还通过标签进行单独的分类。因此您可以选择通过所有文章类型或特定于文章的分类法进行分类
这段代码很容易解释。请投票支持我的答案,我需要建立我的代表。谢谢
必须将代码复制到functions.php文件中
add_action( 'init', 'create_post_types' );
function create_post_types() {
// Custom Post 1
register_post_type( 'companies',
array(
'labels' => array(
'name' => __( 'Companies' ),
'singular_name' => __( 'Company' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'companies' );
// Post Specific Taxonomy
register_taxonomy( 'company_category', 'companies' );
// Custom Post 2
register_post_type( 'events',
array(
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'events' );
// Post Specific Taxonomy
register_taxonomy( 'events_category', 'events' );
// Custom Post 3
register_post_type( 'deals',
array(
'labels' => array(
'name' => __( 'Deals' ),
'singular_name' => __( 'Deal' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'deals' );
// Post Specific Taxonomy
register_taxonomy( 'deals_category', 'deals' );
// Custom Post 4
register_post_type( 'banners',
array(
'labels' => array(
'name' => __( 'Banners' ),
'singular_name' => __( 'Banner' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'banners' );
// Post Specific Taxonomy
register_taxonomy( 'banners_category', 'banners' );
}
有4种自定义帖子类型,就像我说的那样,代码很容易解释为什么这不是正确的答案?我错过什么了吗?我不知道这页上一半的答案在说什么!
<?php
/**
* Template Name: Advert Listing
*
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Set the args array for the query to happen
$args = array(
'post_type' => 'adverts',
'post_status' => 'publish',
'posts_per_page' => 10
);
// Dynamically set the mycat argument from a $_GET parameter
if( isset($_GET['mycat']) ) {
$args['tax_query'] => array(
array(
'taxonomy' => 'advert_category',
'field' => 'slug',
'terms' => $_GET['mycat']
)
);
}
// Issue the query
$q = null;
$q = new WP_Query($args);
// Start the loop
if( $q->have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php _e( 'Advert Listing:', 'twentyfourteen' ); ?></h1>
</header>
<?php
while ($q->have_posts()) : $q->the_post();
?>
<article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> adverts type-adverts status-publish hentry">
<header class="entry-header">
<a href="<?php echo get_permalink(get_the_ID()); ?>"><h3 class="entry-title"><?php the_title(); ?></h3></a>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
</article>
<?php
endwhile;
// Previous/next post navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div><!-- #content -->
</section><!-- #primary -->
<?php
get_sidebar( 'content' );
get_sidebar();
get_footer();
add_action( 'init', 'create_post_types' );
function create_post_types() {
// Custom Post 1
register_post_type( 'companies',
array(
'labels' => array(
'name' => __( 'Companies' ),
'singular_name' => __( 'Company' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'companies' );
// Post Specific Taxonomy
register_taxonomy( 'company_category', 'companies' );
// Custom Post 2
register_post_type( 'events',
array(
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'events' );
// Post Specific Taxonomy
register_taxonomy( 'events_category', 'events' );
// Custom Post 3
register_post_type( 'deals',
array(
'labels' => array(
'name' => __( 'Deals' ),
'singular_name' => __( 'Deal' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'deals' );
// Post Specific Taxonomy
register_taxonomy( 'deals_category', 'deals' );
// Custom Post 4
register_post_type( 'banners',
array(
'labels' => array(
'name' => __( 'Banners' ),
'singular_name' => __( 'Banner' )
),
'public' => true,
'has_archive' => true,
)
);
// Default Wordpress Category Taxonomy
register_taxonomy_for_object_type( 'category', 'banners' );
// Post Specific Taxonomy
register_taxonomy( 'banners_category', 'banners' );
}