admin管理员组文章数量:1414621
I have the following below which I have used to create a custom post type and a custom taxonomy.
Within the products section I've created the categories "monitors" & "consumables".
I have then created the template taxonomy-monitors.php, is that correctly named for the monitors category? Also what is the url I need to visit to see only the monitors category using that template?
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'capability_type' => 'post',
'supports' => array('title','editor','comments'),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ),
)
);
}
function news_init() {
// create a new taxonomy
register_taxonomy(
'products',
'products',
array(
'label' => __( 'Product Categories' ),
'sort' => true,
'hierarchical' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'products-category' )
)
);
}
add_action( 'init', 'news_init' );
UPDATE
I have the following below which I have used to create a custom post type and a custom taxonomy.
Within the products section I've created the categories "monitors" & "consumables".
I have then created the template taxonomy-monitors.php, is that correctly named for the monitors category? Also what is the url I need to visit to see only the monitors category using that template?
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'capability_type' => 'post',
'supports' => array('title','editor','comments'),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ),
)
);
}
function news_init() {
// create a new taxonomy
register_taxonomy(
'products',
'products',
array(
'label' => __( 'Product Categories' ),
'sort' => true,
'hierarchical' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'products-category' )
)
);
}
add_action( 'init', 'news_init' );
UPDATE
Share Improve this question edited Aug 15, 2012 at 14:17 Rob asked Aug 15, 2012 at 10:00 RobRob 1,41613 gold badges41 silver badges68 bronze badges3 Answers
Reset to default 15Templates
See the Template Hiearchy for a more detailed break down of how WordPress chooses the template.
For a taxonomy term slug
('monitors' your example) in the taxonomy taxonomy
(e.g. 'products') WordPress will try to use the following templates (in this order)
taxonomy-{taxonomy}-{slug}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php
For your 'monitors' taxonomy term page, WordPress will use
taxonomy-products-monitors.php
if it exists. If it doesn't, then for that taxonomy it will fallback to
taxonomy-products.php
and so on.
Permalinks
The following url should point to the 'monitors' products page:
www.example?products=monitors
You have also specified an url rewrite, so assuming the rewrite rules have been flushed and there isn't a clash, the following should also work
www.example/products-category/monitors
Reference : https://stackoverflow/questions/33888951/wordpress-custom-post-type-taxonomy-template
<?php
get_header();
do_action('genesis_before_content_sidebar_wrap'); ?>
<div id="content-sidebar-wrap">
<?php do_action('genesis_before_content'); ?>
<div class="wrap">
<main class="content">
<?php
$case_study_cat_slug = get_queried_object()->slug;
$case_study_cat_name = get_queried_object()->name;
?>
<h2><?php echo $case_study_cat_name; ?></h2>
<?php
$al_tax_post_args = array(
'post_type' => 'success_stories', // Your Post type Name that You Registered
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'success_stories_category',
'field' => 'slug',
'terms' => $case_study_cat_slug
)
)
);
$al_tax_post_qry = new WP_Query($al_tax_post_args);
if($al_tax_post_qry->have_posts()) :
while($al_tax_post_qry->have_posts()) :
$al_tax_post_qry->the_post();
echo '<div class="post-excerpt">';
?>
<h2 class="entry-title" itemprop="headline"><a href="<?php the_permalink(); ?>" class="entry-title-link"><?php the_title(); ?></a></h2>
<div class="entry-content"> <?php echo excerpt(35); ?> </div>
</div>
<?php
endwhile;
endif;
?>
</main>
</div>
</div>
<?php
do_action('genesis_after_content_sidebar_wrap');
get_footer();
For this, add the following code in the functions.php (located in the theme folder):
add_action( 'init', 'create_cw_hierarchical_taxonomy', 0 );
//create a custom taxonomy name
function create_cw_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => __( 'Parent Topic' ),
'parent_item_colon' => __( 'Parent Topic:' ),
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'menu_name' => __( 'Topics' ),
);
// taxonomy register
register_taxonomy('topics',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
I have found it here and here i found how to create Non-hierarchical Taxonomy https://www.wpblog/create-custom-taxonomies-in-wordpress/
本文标签: How to create a custom template for a custom taxonomy
版权声明:本文标题:How to create a custom template for a custom taxonomy? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177749a2646323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论