[resolved] Separate post titles with a comma, don't show nothing if query is empty (3 posts)

  1. r1987
    Posted 4 years ago #


    I have a WP_Query that outputs posts that belong to a custom post type taxonomy.

    echo "Productions:&nbsp;";
    $args = array('post_type' => 'productions',
       'tax_query' => array (
          array ( 'taxonomy' => 'directors',
             'field'    => 'id',
             'terms'    => get_the_ID()
    $query = new WP_Query( $args );
    // The Query
    $the_query = new WP_Query( $args );
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>,
    <?php endwhile; ?>

    This all works fine, but there are two things that I don't know how to do:

    -i want to separate the output with commas -> Productions: Rambo, Terminator 2, Highlander

    -If there are no posts, then it should be empty. At the moment, when there are no posts, it shows Productions:


  2. Michael
    Forum Moderator
    Posted 4 years ago #

    to check for 'empty' add a conditional statement and move things around:


  3. r1987
    Posted 4 years ago #

    Wow, really big thanks for the quick response and a working code!

Topic Closed

This topic has been closed to new replies.

About this Topic