WP - 以更简单的方式显示类别中的帖子

时间:2016-09-29 01:05:35

标签: php wordpress if-statement

您好我想找到一种更简单的方法来编码下面的块。我正在做基于帖子类型的类别的陈述,然后吐出包括标题/特色图像/内容的帖子。想知道是否有更简单的方法使用for循环 - 循环遍历数组或类似的东西?

目前是:

        if(in_category('hoses-posts')){
            $args = array( 'post_type' => 'hoses_posts' , 'category_name' => 'hoses-posts' , 'order' => 'ASC', 'posts_per_page'  => 30);
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <?php get_template_part('templates/loop-product');?>
            <?php
            endwhile;

        } elseif(in_category('hoses-isobaric')){

            $args = array( 'post_type' => 'hoses_posts' , 'category_name' => 'hoses-isobaric' , 'order' => 'ASC','posts_per_page'  => 30);
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <?php get_template_part('templates/loop-product');?>
            <?php
            endwhile;

        } elseif(in_category('hoses-braid')){

            $args = array( 'post_type' => 'hoses_posts' , 'category_name' => 'hoses-braid' , 'order' => 'ASC','posts_per_page'  => 30);
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <?php get_template_part('templates/loop-product');?>
            <?php
            endwhile;

        } elseif(in_category('hoses-spiral')){

            $args = array( 'post_type' => 'hoses_posts' , 'category_name' => 'hoses-spiral' , 'order' => 'ASC','posts_per_page'  => 30);
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
            <?php get_template_part('templates/loop-product');?>
            <?php
            endwhile;

        } elseif(in_category('hoses-speciality')){
          and so on....
        }

更新

我从这里抓取了这段代码:https://gist.github.com/DevinWalker/6fb2783c05b46a2ba251。不确定这是否是正确的方法呢?

              $post_type = 'hoses-posts';

                // Get all the taxonomies for this post type
                $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );                   
                foreach( $taxonomies as $taxonomy ) :                    
                    // Gets every "category" (term) in this taxonomy to get the respective posts
                    $terms = get_terms( $taxonomy );                     
                    foreach( $terms as $term ) : ?>
                    <?php
                        $args = array('post_type' => $post_type, 'order' => 'ASC','posts_per_page'  => -1 ,'tax_query' => array(array('taxonomy' => $taxonomy,'field' => 'slug','terms' => $term->slug, ) ) );
                        $posts = new WP_Query($args);

                        if( $posts->have_posts() ): ?> 

                          <?php echo $term->name; ?>

                        <?php while( $posts->have_posts() ) : $posts->the_post(); ?>

                            <?php get_template_part('templates/loop-product');?>                                       

                        <?php endwhile; endif; ?>

                    <?php endforeach;

                endforeach; ?>

2 个答案:

答案 0 :(得分:0)

尝试以这种方式优化代码,

<?php
function get_template( $category_name ) {
  $args = array( 'post_type' => 'hoses_posts' , 'category_name' => $category_name , 'order' => 'ASC', 'posts_per_page'  => 30);
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
    get_template_part('templates/loop-product');
  endwhile;   
}

if ( in_category('hoses-posts') ) {
    get_template( 'hoses-posts' );
} elseif ( in_category('hoses-isobaric') ) {
    get_template( 'hoses-isobaric' );
} elseif ( in_category('hoses-braid') ) {
    get_template( 'hoses-braid' );
} elseif ( in_category('hoses-spiral') ) {
    get_template( 'hoses-spiral' );
} elseif ( in_category('hoses-speciality') ) {
  and so on....
}

答案 1 :(得分:0)

我的解决方案

$args = array( 
    'post_type' => 'hoses_posts' , 
    'category_name' => in_category('hoses-posts'), 
    'order' => 'ASC', 
    'posts_per_page'  => 30
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    get_template_part('templates/loop-product');
endwhile;
wp_reset_postdata();

不要忘记通过函数wp_reset_postdata()

关闭WP_query