List posts from taxonomy term

375 views Asked by At

I need to display posts assigned to taxonomy term. I can do it manually with no problem but I would like to have a loop to automaticall display posts from each taxonomy term so when user adds new term the posts from that term are displayed on the site. You can see the live example on the following url;

    176.9.5.243/~divesdes/shop

I am using Woocommerce to build an online store. Here is the code I am using to disply posts manually.

    <?php query_posts('post_type=product&product_cat=striped-mats'); ?>
    <?php single_cat_title( ); ?> 
    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php the_content();?>
    <?php endwhile; 
      wp_reset_query();?>
    <?php endif; ?>

Thanks to everyone who would want to help me with this...

2

There are 2 answers

1
KodeFor.Me On

First of all you can use this service that can write the code for you : http://generatewp.com/

This is a cool tool that allowing you to build the code for several parts of the WordPress theme.

Otherwise you can use the WP_Query class to create a query object like that:

$products = new WP_Query(
    array(
        'post_type'              => 'product',
        'post_status'            => 'publish',
        'cat'                    => 'product_cat',
        'category_name'          => 'striped-mats',
    )
);

if($products->have_posts())
{
while($products->have_posts())
    {
        $products->the_post();
        // do something
}
}
else
{
// Np products found
}
0
user2186701 On

Thanks you for your time and effort to answer my question. The thing is that I need a loop that will list terms and posts assigned to that terms but with prediction that user will add new terms. So in short if a user adds term and a post to that term it is displayed in the site.