• I’m using this code to display the current users 5 recent post in a custom post type. I’m pretty noob at this, but I got it to work correctly if the current user has a post, but if they don’t I’d like to display a text like “You don’t have any questions yet, click here to submit one.”

    <h3 class="section_title"><?php _e('Recent Orders', 'woocommerce'); ?></h3>
    <?php woocommerce_get_template('myaccount/my-orders.php', array( 'recent_orders' => $recent_orders )); ?>
    
    <h3 class="section_title">Recent Questions</h3>	
    
    <?php
    $page_title = 'Contributors Page';
    if ( is_user_logged_in()  && is_page($page_title) ) {
      global $current_user;
      get_currentuserinfo();
      echo 'User ID: ' . $current_user->ID . "\n";
      $args=array(
        'author' => $current_user->ID,
        'post_type' => 'knowledge-base',
        'post_status' => 'publish, private',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'Your Posts';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
Viewing 1 replies (of 1 total)
  • The if (have_posts()) statement needs an ‘else’ clause:

    if( $my_query->have_posts() ) :
       echo 'Your Posts';
       while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
       endwhile;
    else :
       // Put whatever you want for 'no posts' here
    endif;

    Also note that you are not supposed to mix ‘curly braces’ as you had in your original if-statement, with colons as you had in your original while-statement.

Viewing 1 replies (of 1 total)
  • The topic ‘Add Conditional Line’ is closed to new replies.