0

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; ?>
2
  • Why do you have 3 }s after your endwhile;? Is that closing out some structure we're not seeing?
    – mor7ifer
    Commented Mar 29, 2012 at 10:01
  • Im not sure =) Someone else helped me with this code, I couldn´t do it by myself. But maybe they didn´t write it correct, it works though!
    – Johan Dahl
    Commented Mar 29, 2012 at 10:51

1 Answer 1

1

What you're gonna want to do is completely rewrite that super-complex structure you have to something like this:

<?php if( $my_query->have_posts() ) : ?>
    <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?>
        <!-- your output here -->
    <?php endwhile; else : ?>
        <!-- your failure output here -->
<?php endif; ?>
8
  • I tried that now, but there is probably a syntax error somewhere, as the page isnt working at all now. Plz check the code I reposted. :)
    – Johan Dahl
    Commented Mar 29, 2012 at 10:34
  • Typo. Should be $my_query->the_post(); Original answer updated.
    – mor7ifer
    Commented Mar 29, 2012 at 10:36
  • Still something wrong, no page at all...i updated my code to the current one
    – Johan Dahl
    Commented Mar 29, 2012 at 10:46
  • You put your output in place of the comments, correct? And you realize this is not intended to replace your entire code, just the loop segment of it, correct?
    – mor7ifer
    Commented Mar 29, 2012 at 10:51
  • No I didnt put anything there yet, but not doing so would not break the page right? I tried now, just adding some text, and the page still breaks. Yes I´m not replacing all my code with this if thats what you mean :) Just the this loop of course!
    – Johan Dahl
    Commented Mar 29, 2012 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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