• I have set up different sidebars per category to allow for search within each category. The link to the searchform is

    <form name="searchform" id="searchform" method="get" action="<?php bloginfo('url'); ?>/topic/news/#content">

    This returns search results for the category ‘news’ (I’ve set my category base to ‘topic’)

    I’ve created separate search results pages for each category. For example, search-news.php is the search results page for news category.

    My search.php is

    <?php
     if ( have_posts() ) { the_post(); rewind_posts(); }
     if ( in_category(55) ) {
     include(TEMPLATEPATH . '/search-art.php'); // Sends search for articles to different search page
     }
     if ( in_category(3) ) {
     include(TEMPLATEPATH . '/search-news.php'); // Sends search for news to different search page
     }
     else {
     include(TEMPLATEPATH . '/search-blog.php'); // Defaults to search page for blog
     }
    ?>

    All this works fine. Where it hangs is if the search query returns nothing. Then, the searchform that gets called up in the search results page is ALWAYS the default blog searchform. The part after the
    <?php else : ?> below doesn’t seem to be working. Can anyone tell me where I am going wrong?

    <?php if (have_posts()) : ?>
    <h1 class="pagetitle"><?php _e('Search Results for '); ?><?php the_search_query(); ?></h1>
    
    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
    <h2 class="posttitle" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to'); the_title(); ?>"><?php the_title(); ?></a></h2>
    
    	<?php endif; ?>
    			</div>
    
    		<?php endwhile; ?>
    		</div>
    
    	<?php else : ?>
    	<h1 class="pagetitle"><?php _e('No posts found. Try a different search?') ?></h1>
    <?php include (TEMPLATEPATH . '/searchform-news.php'); ?>
    
    	<?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter milo2man

    (@milo2man)

    Let me try to explain. When the search query returns a result, the conditional tag in_category(55) works to send the search to the search results page for that category, search-art.php.

    However, if the search query returns nothing (no results) then the conditional tag does not work (I guess because no results are actually in category 55) and the results page defaults to that for the blog.

    How do I write the conditional tag so that it works even when there are 0 results?

    I am breaking my head on this one, please help!

    rohandsouza

    (@rohandsouza)

    Am not too good at PHP but i guess you should use a page referer. That informs the search page to default to the category search even if there are no results from the category.

    rohandsouza

    (@rohandsouza)

    Some crude coding

    <?php
    
    		$rohan = $_SERVER['HTTP_REFERER'];;
    		$rohan1 = 'https://localhost/projects/Sairo/wordpress/?cat=15';
    
    		if ( $rohan == $rohan1 ) {
    			echo $rohan;
    			 include(TEMPLATEPATH . '/celeblist.php');
    			}
    		else {
    
    			include(TEMPLATEPATH . '/archivelist.php');
    
    		}
    
    		?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Different Searchform for Each Category’ is closed to new replies.