-1

I'm learning my ways in PHP lately and play allot with WP and PHP. I've created a some PHP that fetches some posts with the same 1th tag. This with some help with a tutorial here and there. The thing the tutorial is missing is a message when there is no tag attached to the post.

Here is the code:

<?php $tags = wp_get_post_tags($post->ID); if ($tags) { ?>
    <h3>Related</h3>
    <?php $first_tag = $tags[0]->term_id; $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'posts_per_page'=>10,
        'caller_get_posts'=>1
    );?>
    <ul>
        <?php $my_query = new WP_Query($args); if($my_query->have_posts() ) {
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <li><h5><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h5></li>
        <?php endwhile; } wp_reset_query();
    } ?>
    </ul>

I thought this would be a solution:

<?php endwhile; else: echo('no tags'); } wp_reset_query(); } ?>

But it's not working, but to be honest... it doesn't seem right at all. I know it's pretty basic, but the if else ifelse while foreach.... sometimes I can't see the wood for the trees.

Also I think the:

</ul>

is placed wrong.

Thanks, /Paul

3
  • I wouldn't have felt good about a close vote before this received an answer, so as to not discourage the OP, but given that it has 2 now: I feel this is off-topic, even though the code is full of WP functions. The question is PHP and PHP only. Jul 9, 2013 at 19:34
  • I'm sorry... Indeed I thought it was wp related due the wp functions. I will keep this in mind in the future, and will not make the same mistake again.
    – Kortschot
    Jul 9, 2013 at 19:42
  • Again, not to worry. Trying to stay on-topic is obviously a good idea, but the worst than can happen is the question getting put on hold. Jul 9, 2013 at 20:03

2 Answers 2

0

There is no while/else in PHP that I know of.

Second, I find "alternate" control structure syntax impossible to read, especially with two or three on the same line. Use brackets and properly indent your code. It will make a lot more sense.

<?php 
$tags = wp_get_post_tags($post->ID); if ($tags) { ?>
<h3>Related</h3>
<?php $first_tag = $tags[0]->term_id; $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'posts_per_page'=>10,
    'caller_get_posts'=>1
);?>
<ul><?php 
  $my_query = new WP_Query($args); 
  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) {
      $my_query->the_post(); ?>
      <li><h5><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h5></li><?php 
    }
  } else { // this is where the else goes; after the have_posts conditional
    echo('no tags');
  }
  wp_reset_query(); ?>
</ul>

Noticed where I put the else. It follows the if($my_query->have_posts) conditional. If that condition is false your else should run, which sounds like what you want.

2
  • I believe it ain't supposed to be "while/else", but invoked if if ($tags) {} (line 2) evaluates to false. Agree though, hardly readable. Jul 9, 2013 at 19:32
  • Ah, yes I see, it's much more "readable" this way. Like I said I'm still finding my way in php and the correct syntax. In your code I actualy see where the else should go, well see.... Understand. I tend to step ahead a little to fast. Thanks for this.
    – Kortschot
    Jul 9, 2013 at 19:33
1

In the above example, the if construct is comprised of

  • the keyword itself (i.e. the if)
  • an expression that is evaluated to be either true or false (i.e. what's in the parentheses)
  • several statements to be executed if the expression evaluates to true

The latter are enclosed in curly braces ({}), in your above code block.
The else statement should hence follow after the closing curly brace.

Hence

<?php endwhile; } else { echo('no tags'); } wp_reset_query(); } ?>

ought to work for you.

I second s_ha_dum's opinion though: Make an effort to not only write working, but readable code.
The effort will be worth the time, if you ever have to come back to your code later. Not to mention if you'd ever forward a project to another developer.

2
  • Thanks, also good advice and I will follow it. Have no idea though who's answer to accept.
    – Kortschot
    Jul 9, 2013 at 19:45
  • It does not matter. You accepted s_ha_dum's answer, stick with it. Jul 9, 2013 at 20:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.