RRowan RRowan - 2 months ago 11
PHP Question

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


Update:

I have grabbed this piece of code from here:https://gist.github.com/DevinWalker/6fb2783c05b46a2ba251. unsure if this is correct way to go about it ?

$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; ?>

Fil Fil
Answer

Try optimizing your code in this way,

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