Multiple conditionals in sidebar
-
Apparently it’s not possible to combine these two conditionals in sidebar.php:
<?php if ( is_single() or is_page() or is_paged() or is_search() or is_archive() or is_404() ) { ?> <div> <?php query_posts("showposts=6"); $i = 1; ?> <?php while (have_posts()) : the_post(); ?> (some content) <?php endwhile; ?> </div> <?php } ?> [some content in between...]
and
[... rest of the content in between] <?php if ( is_home() or is_archive() or is_404() ) { ?> <div> (...) </div> <?php } elseif ( is_single() ){ ?> <div> (xxx) </div> <?php } ?>
I take it there’s a perfectly reasonable explanation for this. But is there a solution to combine these conditionals as well?
-
The PHP syntax for
or
is||
<?php if ( is_home() || is_archive() || is_404() )
I see. However I’ve been using ‘or’ in other php circumstances with success.
Anyway, I’ve changed this to || but with no result.
Should I use echoes instead of hard html between <? ?>’s?
(Btw the echoes don’t work either, it was a rather general advice I was asking). The full code:
First block:
<?php if ( is_single() || is_page() || is_paged() || is_search() || is_archive() || is_404() ) { ?> <div class="dfg"> <p class="gfg"><strong>Brekend</strong></p> <?php query_posts("showposts=4&category_name=Featured"); $i = 1; ?> <?php while (have_posts()) : the_post(); ?> <div class="gdrg"> <?php $values = get_post_custom_values("Image"); if (isset($values[0])) { ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100" alt="<?php the_title(); ?>" class="left" width="99px" height="62px" /></a> <?php } ?> <div class="info"><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><strong><?php the_title(); ?></strong></a> <div class="una">[<?php the_time('j M Y') ?> | <?php comments_popup_link('0 reacties', '1 reactie', '% reacties');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]</div> </div> </div> <?php endwhile; ?> </div> <?php } ?>
Some html and php in between…
<?php if ( is_home() || is_archive() || is_404() ) { ?> <div id="dsfsdf"> <script type="text/javascript"><!-- google_ad_client = "pub-006913815847188"; /* 300x250, created 6-2-09 (o) */ google_ad_slot = "6514371817"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <?php } elseif ( is_single() ){ ?> something else <?php } ?>
You might want to read up on using multiple Loops.
Funny thing is, if the second block goes on top of the sidebar, it works… But that’s not its proper place ofcourse.
I’ll post the full sidebar here, maybe some php-pro can see where it’s going wrong here (meanwhile reading “multiple loops”, esmi):
<div id="mond"> <?php if ( is_single() || is_page() || is_paged() || is_search() || is_archive() || is_404() ) { ?> <div class="dfg"> <p class="gfg"><strong>Brekend</strong></p> <?php query_posts("showposts=4&category_name=decay"); $i = 1; ?> <?php while (have_posts()) : the_post(); ?> <div class="gdrg"> <?php $values = get_post_custom_values("Image"); if (isset($values[0])) { ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=106&h=65&zc=1&q=100" alt="<?php the_title(); ?>" class="left" width="99px" height="62px" /></a> <?php } ?> <div class="info"><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><strong><?php the_title(); ?></strong></a> <div class="una">[<?php the_time('j M Y') ?> | <?php comments_popup_link('0 reacties', '1 reactie', '% reacties');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]</div> </div> </div> <?php endwhile; ?> </div> <?php } ?> <div id="tur"> <script type="text/javascript"><!-- google_ad_client = "pub-0010673412010"; /* 300x250, gemaakt 8-6-09 */ google_ad_slot = "3614536814"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js"></script> </div> <div id="sdfdsf"> <?php include (TEMPLATEPATH . '/searchform.php'); ?> <?php /* Widgetized sidebar, if you have the plugin installed. */ if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(1) ) : ?> <?php endif; ?> </div> <div id="sdfsd"> <?php /* Widgetized sidebar, if you have the plugin installed. */ if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(4) ) : ?> <?php endif; ?> </div> <div id="dsffdss"> <?php /* Widgetized sidebar, if you have the plugin installed. */ if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(3) ) : ?> <?php endif; ?> </div> <div id="dqsf"> <h3>match</h3> <?php query_posts('cat=4&showposts=5'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>"><strong><?php the_title(); ?></strong></a></li> <?php endwhile;?> </ul> <h3>quick</h3> <?php query_posts('cat=3&showposts=5'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>"><strong><?php the_title(); ?></strong></a></li> <?php endwhile;?> </ul> </div> <?php if ( is_home() || is_archive() || is_404() ) { ?> <div id="dsfsdf"> <script type="text/javascript"><!-- google_ad_client = "pub-006913815847188"; /* 300x250, created 6-2-09 (o) */ google_ad_slot = "6514371817"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <?php } elseif ( is_single() ){ ?> something else <?php } ?> </div>
You know, I’m not an expert, but I don’t believe it’s loop-related after doing some read on that link.
Another funny thing: it used to work locally on my computer (PHP 5.2.6), but not online (PHP 5.2.10). I’ve upgraded my local system to PHP 5.2.10. Doesn’t work locally either now ??
Not that it should have something to do with it. A well written piece of PHP should work on any upgrade.
It could be if this sidebar is being called within a page/archive template that is also running a Loop.
Is there a way I can break this loop on top of the sidebar? (tried
<?php endwhile;?>
,<?php endif ?>
and<?php rewind_posts(); ?>
with no luck)Like I said: If I erase all the rest, except for the second block in the bottom of that code, being
<?php if ( is_home() || is_archive() || is_404() ) { ?> <div id="dsfsdf"> <script type="text/javascript"><!-- google_ad_client = "pub-006913815847188"; /* 300x250, created 6-2-09 (o) */ google_ad_slot = "6514371817"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <?php } elseif ( is_single() ){ ?> something else <?php } ?>
it actually works. So I thinks the code conflicts somewhere, somehow. Any idea on what’s going wrong there?
I’ve been breaking my head over it, with no progress. Is there another way in which I can achieve the same result, but with another method?
I also remarked there this
is_archive
in both blocks. But it seems like it’s considering the homepage is anis_archive
as well?!When I remove it in the second block on the very bottom, like this
<?php if ( is_home() || is_404() ) { ?>; this should appear on the homepage (and 404) <?php } elseif ( is_archive() ){ ?>; this not, but yet it does. <?php } ?>;
it works on the single page, but it appears on the homepage as well! Can someone help me out here? (I’m using the Arthemia theme, if this would be of any relevance)
I solved it with a bit of luck on the internet: adding
<?php wp_reset_query(); ?>
on top of the second box solves the problem:<?php wp_reset_query(); ?> <?php if ( is_home() || is_404() ) { ?>; this should appear on the homepage (and 404) <?php } elseif ( is_archive() ){ ?>; this not, but yet it does. <?php } ?>;
Thanks for guiding me to the right trail, esmi (loops).
- The topic ‘Multiple conditionals in sidebar’ is closed to new replies.