Skip to main content
5 of 5
added 1415 characters in body
Johan Dahl
  • 1.3k
  • 3
  • 19
  • 32

Help with if and else statement

I have this custom loop, which, if It doesnt get any result should display some text, like 'There are no news regarding this item". But I´m not sure how to.

Code:

<?php
// Loop in the ten latest news with the same taxonomy term as the current post
$backup = $post;  // backup the current object
$found_none = '';
$taxonomy = 'fastighetslista';//  e.g. post_tag, category, custom taxonomy
$param_type = 'fastighetslista'; //e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
    foreach ($tags as $tag) {
        $args=array(
            "$param_type" => $tag->slug,
            'post__not_in' => array($post->ID),
            'post_type' => $post_types,
            'showposts'=> 10,
            'caller_get_posts'=>1
);
$my_query = null;
$my_query = new WP_Query($args); ?>
<?php
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="press-entry-small">
        <div class="press-entry-date-small">
            <p class="press-day-small"><?php the_time('d'); ?></p>
            <p class="press-month-small"><?php the_time('M'); ?></p>
        </div>
    
        <li class="news-link">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>

    </div>
        
    <div class="clear"></div>
        
    <?php $found_none = '';
endwhile;
}
}
}
if ($found_none) {
    echo $found_none;
}
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again
?>  

Where can I put my 'else' statement?

Thanks

Edit (again).

Ok I modified the code, It looks like this now, but it´s not working at all.

                        <?php
            $taxonomy = 'fastighetslista';//  e.g. post_tag, category, custom taxonomy
            $param_type = 'fastighetslista'; //  e.g. tag__in, category__in, but genre__in will NOT work
            $post_types = get_post_types( array('public' => true), 'names' );
            $tax_args=array('orderby' => 'none');
            $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
            if ($tags) {
              foreach ($tags as $tag) {
                $args=array(
                "$param_type" => $tag->slug,
                'post__not_in' => array($post->ID),
                'post_type' => $post_types,
                'showposts'=> 10,
                'caller_get_posts'=>1
            );
            $my_query = null;
            $my_query = new WP_Query($args); ?>
            
            <?php if( $my_query->have_posts() ) : ?>
            <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
            
            <div class="press-entry-small">
            <div class="press-entry-date-small">
            <p class="press-day-small"><?php the_time('d'); ?></p>
            <p class="press-month-small"><?php the_time('M'); ?></p>
            </div>
            
            <li class="news-link"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            </div>
            
            <div class="clear"></div>
            <!-- your output here -->
            <?php endwhile; else : ?>
            <!-- your failure output here -->
            <?php endif; ?>

Edit. Ok I got this working.

Code:

                <?php
            $taxonomy = 'fastighetslista';//  e.g. post_tag, category, custom taxonomy
            $param_type = 'fastighetslista'; //  e.g. tag__in, category__in, but genre__in will NOT work
            $post_types = get_post_types( array('public' => true), 'names' );
            $tax_args=array('orderby' => 'none');
            $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args); ?>
            <?php
            if ($tags) ?>
              <?php foreach ($tags as $tag) ?>
              <?php
                $args=array(
                "$param_type" => $tag->slug,
                'post__not_in' => array($post->ID),
                'post_type' => $post_types,
                'showposts'=> 10,
                'caller_get_posts'=>1
            );
            $my_query = null;
            $my_query = new WP_Query($args); ?>
            
            <?php if( $my_query->have_posts() ) : ?>
            <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
            
            <div class="press-entry-small">
            <div class="press-entry-date-small">
            <p class="press-day-small"><?php the_time('d'); ?></p>
            <p class="press-month-small"><?php the_time('M'); ?></p>
            </div>
            
            <li class="news-link"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            </div>
            
            <div class="clear"></div>
            <!-- your output here -->
            <?php endwhile; else : ?>
            Blablabla               
            <?php endif; ?>
Johan Dahl
  • 1.3k
  • 3
  • 19
  • 32