WordPress.org

Forums

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

  1. r1987
    Member
    Posted 2 years ago #

    Hello!

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

    <p>
    <?php
    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; ?>
    </p>

    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:

    Anyone?

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

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

    http://pastebin.com/PXqmaAzL

  3. r1987
    Member
    Posted 2 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