Question

I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?

this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer): http://ivry.sweetyams.ca/?s=new

and a search not working here: http://ivry.sweetyams.ca/?s=asjdfkl%3B

Code I am using: (I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page

<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */  if (is_search() ) { ?>
  <?php query_posts('category_name=0&showposts=5'); ?>
  <?php while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <p> <?php the_title(); ?></p>
        </a>
  <?php endwhile;?>

<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
  <?php query_posts('category_name=escalade&showposts=5'); ?>
  <?php while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <p> <?php the_title(); ?></p>
        </a>
  <?php endwhile;?>

<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
  <?php query_posts('category_name=montagne&showposts=5'); ?>
  <?php while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <p> <?php the_title(); ?></p>
        </a>
  <?php endwhile;?>

<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`

I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.

The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:

This is getting every post below escalade, OR every post below the montagne category

My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.

Was it helpful?

Solution 2

I found a way to get it to work,

I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/

also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/

This code checks whether you are on a category or subcategory,

<?php
$my_posts = new WP_Query(); ?>

<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
    <?php $my_posts->query('posts_per_page=5&cat=6');?> 

<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
    <?php $my_posts->query('posts_per_page=5&cat=14'); ?>

<?php } /* AND OTHER PAGE */ else { ?>
    <?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>

<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
    <a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
    <p><?php echo substr($post->post_title,0,200); ?></p>
    </a>

<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>

OTHER TIPS

first, showposts is deprecited since v2.1, used posts_per_page instead.

second, after each query_post, you need to reset the query like this:

<?php
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
?>

this is how I understand it. see more detail here:query_posts(), hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top