mercredi 28 septembre 2016

WP - displaying posts in a category easier way to code

Hi I would like to find an easier way to code my block below. I am doing if statements on categories based on post type, then spitting out posts including titles/featured image /content. Wondering if there is an easier way with a for loop - looping through an array or similar?

Currently it is:

        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....
        }

Aucun commentaire:

Enregistrer un commentaire