个性化阅读
专注于IT技术分析

WordPress自定义帖子类型分类模板

点击下载

我有一个称为”产品”的自定义帖子类型, 它的分类法为”产品类别”, 其类别为类别1, 类别2等。子类别又为类别1a, 类别2a等。当我单击时, 我想要的是类别1, 应列出子类别1a, 类别2a等。单击类别2a时, 应列出与该类别关联的产品。如何使用wordpress完成此操作?

<?php $taxonomy_name = 'al_product_cat'; 
$term_childs = get_term_children( $wp_query->get_queried_object_id(), $taxonomy_name ); //print_r($term_childs);
foreach($term_childs as $child){ 
    $tm = get_term_by( 'id', $child, $taxonomy_name ); ?>
    <div class="tax_content">
        <div class="feat_thumb"></div>
        <div class="feat_content">
            <h2><a href="<?php echo get_term_link( $child, $taxonomy_name ); ?>"><?php echo $tm->name; ?></a></h2> 
            <p><?php echo $tm->description; ?> </p>
            <div class="brand_logos">
            <?php $terms = get_the_terms( $wp_query->get_queried_object_id(), 'brand' ); 
            foreach($terms as $term){
            ?>
                <img src="<?php echo z_taxonomy_image_url($term->term_id); ?>" />
           <?php } ?>
        </div>
    </div>
    <div class="clear"></div>
 </div>
<?php } ?>

#1


你可以为此使用WordPress模板。

始终将WP_Query()用于自定义帖子类型和分类。

现在以你的主题创建一个文件, 如taxonomy-al_product_cat.php, 然后在该文件中编写一些代码。

该文件适用于父母, 子女及其子女的类别。

例如在taxonomy-al_product_cat.php中

<?php
    get_header();

    $al_cat_slug = get_queried_object()->slug;
    $al_cat_name = get_queried_object()->name;
?>
    <h2><?php echo $al_cat_name; ?></h2>
<?php
    $al_tax_post_args = array(
        'post_type' => 'Your Post Type', // Your Post type Name that You Registered
        'posts_per_page' => 999, 'order' => 'ASC', 'tax_query' => array(
            array(
                'taxonomy' => 'al_product_cat', 'field' => 'slug', 'terms' => $al_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();
?>
            <a href="<?php the_permalink(); ?>">
                 <?php the_title(); ?>
            </a>
<?php
       endwhile;
    endif;
get_footer();
?>

你可以从这些链接中阅读关于tax_query()和get_queried_object()的信息。

希望这会帮助你。

赞(0)
未经允许不得转载:srcmini » WordPress自定义帖子类型分类模板

评论 抢沙发

评论前必须登录!