• Hi,

    I am working on search.php because i would like to have 2 different sidebars appear on my search results page. One sidebar would appear on all pages apart from 1, and the other would appear only on 1 page and not the rest, and that is an image library page I have made.

    1 sidebar is called “searchform.php” and the other sidebar is “sidebar.php”

    I have looked at examples, and have come up with this

    <?php
    if (is_page(‘450’) ) {

    echo ‘<?php include(TEMPLATEPATH.”/searchform.php”);?>’;

    }

    else {

    echo ‘<?php include(TEMPLATEPATH.”/sidebar.php”);?>’;

    }
    ?>

    but its not working ??

    any ideas on how I can get this to work ?

    I hope I have been clear, please let me know if you have any questions.

    cheers,

    tom

Viewing 7 replies - 1 through 7 (of 7 total)
  • You could try with the variable sidebar content example from the codex
    https://codex.www.ads-software.com/Conditional_Tags#Variable_Sidebar_Content

    The variable content is called from within the (same) sidebar.

    This can help you too, conditional tags from within sidebar.php
    https://seogadget.co.uk/rethink-that-sidebar-again/

    I see several problems:

    • is_page is for pages, not search results (search.php).
    • Don’t echo php includes.
    • searchform.php sounds like the search box form and not a sidebar.

    I don’t know if $paged works with search results, but you could try something like this in search.php:

    <?php if ($paged <= "1") { ?>
    <?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
    <?php } else { ?>
    <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
    <?php } ?>
    Thread Starter tommo77funk

    (@tommo77funk)

    Thanks for informative answers, I am still trying with no joy however, so thought it might best to explain exactly what is needed.

    I have 2 forms, 1 in my header and 1 on this page

    The form on library page I want to display search results with searchform.php (which is a sidebar with a search form, just badly named, this is the current sidebar on library page)

    I can make both appear with results, just not with a conditional statement that could decide which sidebar to display depending on the page.

    I consider I have few options.

    1. conditional statement in search.php to choose sidebar

    2. conditional statement somewhere that activates search.php or searchtemplate.php depending on what form is used or what page is currently displayed.

    Any suggestions, very much appreciated ??

    cheers,

    tom

    Thread Starter tommo77funk

    (@tommo77funk)

    <?php if ($paged <= “1”) { ?>
    <?php include (TEMPLATEPATH . ‘/sidebar2.php’); ?>
    <?php } else { ?>
    <?php include (TEMPLATEPATH . ‘/sidebar1.php’); ?>
    <?php } ?>

    kind of works,(sidebar appears) but I dont know what ($paged <= “1”) refers too or how to make it specific to the library page, so I cant test it properly. I guess it means pages less than 1 ?

    my library page id is 450, i hjave pages greater and less than.

    I have also tried these…

    <?php if (is_page(‘450’)) { ?>

    or

    <?php if (is_page(‘library’)) { ?>

    or

    <?php if (is_page_template(‘about.php’) ) { ?>

    but these do not work for me either. Any suggestions, very much appreciated ??

    cheers,

    tom

    My understanding is that the conditional statement should be in sidebar.php and not search.php

    Sidebar.php is the one called by default, and that in turn calls either one of the two templates for the content.

    I just tested this on my local copy and it worked.

    Rename the two templates, to avoid confusion with the actual search form. Call them sidebar-library.php (the sidebar that appears only on the Image Library page), and sidebar-general.php (the one for all other pages), or something like that.

    I am assuming that these templates have the content you want to display conditionally.

    Then, on the default sidebar.php (not search.php) that came with your theme, try this

    <?php
    if(is_page('Image Library')) {
    	include(TEMPLATEPATH . '/sidebar-library.php');
    } 
    
    else {
    	include(TEMPLATEPATH . '/sidebar-general.php');
    } ?>

    Hope this helps!

    I use WordPress’ search.php to display search results, and I’ve never tried to display them on a Page. I don’t know if this can be done. You can put a search form on a page.

    Thread Starter tommo77funk

    (@tommo77funk)

    thanks buddha trance and iridiax,

    buddha trance’s idea is a good one, I’ve tried already

    sidebar.php

    <?php
    if(is_page(‘Image Library’)) {
    include(TEMPLATEPATH . ‘/sidebar-library.php’);
    }

    else {
    include(TEMPLATEPATH . ‘/sidebar-general.php’);
    } ?>

    Sidebar-general.php being the standard sidebar and sidebar-library being the sidebar that appears only on the Image Library page.

    In Search.php I have gone back to <?php get_sidebar() ?> (but have also tried sidebar.php)

    Anyway still no joy + i have to go to bed, will try again all day tomorrow I guess…

    Many thanks for your help, great wishes,

    tom

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘search results conditional sidebar’ is closed to new replies.