如何为自定义分类创建自定义模板?

时间:2012-08-15 作者:Rob

下面是我用来创建自定义帖子类型和自定义分类法的内容。

在products部分中,我创建了类别“monitors”&;“消耗品”。

然后,我创建了模板分类监视器。php,监视器类别的名称正确吗?另外,我需要访问哪个url才能看到使用该模板的监视器类别?

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\' );
更新enter image description here

3 个回复
SO网友:Stephen Harris

模板参见Template Hiearchy 详细了解WordPress如何选择模板。

对于分类术语slug (“monitors”您的示例)在分类法中taxonomy (如“产品”)WordPress将尝试使用以下模板(按此顺序)

taxonomy-{taxonomy}-{slug}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php
对于“监视器”分类术语页,WordPress将使用

taxonomy-products-monitors.php
if 它存在。如果没有,那么对于这种分类法,它将回退到

taxonomy-products.php
等等。

Permalinks以下url应指向“监视器”产品页面:

 www.example.com?products=monitors
您还指定了url重写,因此假设重写规则已刷新并且没有冲突,以下操作也应该可以

 www.example.com/products-category/monitors

SO网友:Rathna

Reference : https://stackoverflow.com/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();
SO网友:Owais Alam

为此,请在函数中添加以下代码。php(位于主题文件夹中):

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\' ),
));
}
我在这里找到了它,在这里我找到了如何创建非层次分类法https://www.wpblog.com/create-custom-taxonomies-in-wordpress/

结束

相关推荐