Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Conditional tags in sidebar not working for pages using template WordPress

  • SOLVED

Hello,

I've created this simple navigation below in my sidebar.

I've inserted conditional statements to add an extra class onto the a tag when that current page is being viewed. Simple I thought.

<strong>See my sidebar markup below.</strong>


<ul id="sidebar-pages">
<li class="list-header">// Pages</li>
<li>
<a href="<?php bloginfo('url'); ?>" class="icon-main <?php if (is_home()) { echo 'sbp-current' ; } ?>">Home</a>
</li>
<li>
<a href="<?php bloginfo('url'); ?>/articles" class="icon-latest <?php if (is_page('articles')) { echo 'sbp-current' ; } ?>">Latest Articles</a>
</li>
<li>
<a href="<?php bloginfo('url'); ?>/most-loved" class="icon-loved <?php if (is_page('most-loved')) { echo 'sbp-current' ; } ?>">Most Loved</a>
</li>
<li>
<a href="<?php bloginfo('url'); ?>/editorial" class="icon-editorial <?php if (is_page('editorial')) { echo 'sbp-current' ; } ?>">Editorial</a>
</li>
<li>
<a href="<?php bloginfo('url'); ?>/feedback" class="icon-feedback <?php if (is_page('feedback')) { echo 'sbp-current' ; } ?>">Feedback</a>
</li>
<li>
<a href="<?php bloginfo('url'); ?>/contact" class="icon-contact <?php if (is_page('contact')) { echo 'sbp-current' ; } ?>">Contact</a>
</li>
</ul>


<strong>Same sidebar markup below, stripped of html with comments</strong>



This works...
<?php if (is_home()) { echo 'sbp-current' ; } ?>

This does not work and echos nothing on page articles?
<?php if (is_page('articles')) { echo 'sbp-current' ; } ?>

This does not work and echos nothing on page most-loved?
<?php if (is_page('most-loved')) { echo 'sbp-current' ; } ?>

This works...
<?php if (is_page('editorial')) { echo 'sbp-current' ; } ?>

This works...
<?php if (is_page('feedback')) { echo 'sbp-current' ; } ?>

This works...
<?php if (is_page('contact')) { echo 'sbp-current' ; } ?>



But i've tried...



<?php if (is_page_template('articles.php')) { echo 'sbp-current' ; } ?>

<?php if (is_page_template('most-loved.php')) { echo 'sbp-current' ; } ?>



...but they are still not adding the class when on these 2 pages.

The only thing different about these pages 'articles' and 'most-loved' , is that they are using page templates: 'articles.php' and 'most-loved.php'

On 'articles.php' and 'most-loved.php', they are running query's to retrieve posts, instead of page content. But I would of thought <strong>is_page</strong> and <strong>is_page_template</strong> should work still?


Has anyone experience similar problem?

Thanks



Answers (5)

2011-12-04

Jurre Hanema answers:

Are you making the calls to is_page_template() before or after you have executed your custom queries? If after, try calling this after you are done with your custom queries:


wp_reset_query();


Josh Cranwell comments:

Was a good idea, as I had not put them in. But conditional tags are still not recognizing that what page its on. Thanks


Josh Cranwell comments:

Yeah the sidebar is after. But surely conditional tags should pick what page your on. Using <strong>is_page</strong>, never had this issue before.

2011-12-05

Manoj Raj answers:

I am not sure what exactly is your problem as you have not given any url or other things needed to solve this issue?

I assume the following links may help you.

Anyway,

As Jurre Hanema said,

wp_reset_query();

should work.

check this link
http://codex.wordpress.org/Conditional_Tags#In_a_theme.27s_footer.php_file

or you can try this way

<?php

if(is_page(2) || $post->post_parent==2) { echo 'sbp-current' ;}
?>


Josh Cranwell comments:

Hi Manoj

Thanks for you help, this has moved me forward.

Writing it like this...

<?php wp_reset_query(); if (is_page('articles')) { echo 'sbp-current' ; } ?>

This worked for the 'most-loved' page... but it did not work for the 'articles' page. So I was like hmmmm, why is this...

The 'articles' page query was a WP_query instead of a normal query, so I changed the WP_query to a query and it worked!!!!! Yes.

But weirdly, when I'm on the Most Loved page, both...


<?php if (is_home()) { echo 'sbp-current' ; } ?>
<?php wp_reset_query(); if (is_page('most-loved')) { echo 'sbp-current' ; } ?>


...are echo-ing the class? How can this be possible?


Manoj Raj comments:

Can you please provide me the sample links through PM? Let me check.. Also if you are willing to send page templates source file, send them too so that i can understand your end..


Josh Cranwell comments:

Thanks Manoj, its now working! Like @Jurre said originally, <strong>wp_reset_query();</strong> would have worked! but because originally on 'most-loved.php' I had the query as a WP_query, and it did not work. But because I converted the WP_query to a query, i forgot to include <strong>wp_reset_query();</strong> at the end.

It is all now working fine. Thank you!!!

2011-12-04

Just Me answers:

do you have a link?


Josh Cranwell comments:

I've got to keep it private so I'll PM you.


Just Me comments:

I am using Google Chrome, it is looking fine. What browser are you using?


Just Me comments:

Oops, too soon. I see what you mean.


Josh Cranwell comments:

Strange, and home stays selected, even though it's not on the home page. It's driving me nuts!


Just Me comments:

Are you sure you are using the same code/same sidebar?


Just Me comments:

it can be a small thing that makes it just different from the other one.
If you could set up a temp. account so I can take a look, that would help.
Or send me the sidebar code through email.
Just to be sure, check the data on your pages, title/permalink and so on.


Josh Cranwell comments:

The code above is 100% taken from my side bar. The only thing that removed is the <strong>title="page title"</strong> from the a tag to shrink the code for visual purposes.

Surely there could not be anything in the back end that could change the way conditional tags work?

I'll leave this question open for a couple of days and see what other guys say.

The only thing that I forgot to mention is that on my pages 'most-loved.php' and 'articles.php' is that they are both running wp_querys instead of normal loops?


Just Me comments:

I don't think it is solvable without seeing your code.


Just Me comments:

you should provide the code of your template file. As you can see it is not that people don't want to help. Yep raising the price always attracts more people :) but you are putting them in a dark room.

2011-12-04

Julio Potier answers:

No link, no code, only $6. I pass !

2011-12-05

Denzel Chia answers:

Hi,

is_page_template() does not work within WordPress Loop.
Please read the WordPress codex page http://codex.wordpress.org/Function_Reference/is_page_template

if you print_r $wp_query which is the global variable that holds all information regarding a page or posts, all you can find on a page is the array [is_page] => 1, which means is_page() is true. There is no mention of [is_page_template]. You normally use is page template in template_redirect hook.

In your case, you can try putting


<?php $this_is_article = 'true'; ?>


at the very beginning of your articles.php, before html tag (before any headers), and in articles.php only.

and use the following in your sidebar code to check.


<?php

if ($this_is_article == 'true')) { echo 'sbp-current' ; }

?>


Hope this helps!

Thanks!
Denzel


Josh Cranwell comments:

Hi Denzel,

Thanks for your reply.

Though my sidebar is outside the loop. Like a regular sidebar. But I tried your method anyway and because it's outside the loop it does not work.

But good idea though, I should of been more specific. In my question.

Thanks


Denzel Chia comments:

Hi,

I got a syntax error in my code.

should be this


<?php

if ($this_is_article == 'true') { echo 'sbp-current' ; }

?>


Sorry if this does not help either.

Thanks!
Denzel


Josh Cranwell comments:

Ahhh this didn't break the site this time, was worth a try, but does not echo the class when on this page. I might have to up the bounty. Thanks