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

Aru*_*hin 5 wordpress wordpress-theming

我有一个名为“产品”的自定义帖子类型,它有一个分类“产品类别”,其中有类别 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 } ?>
Run Code Online (Sandbox Code Playgroud)

dee*_*eem 5

您可以使用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();
?>
Run Code Online (Sandbox Code Playgroud)

您可以从这些链接中了解有关tax_query()get_queried_object()的信息。

希望对你有帮助。