Solved

how should I use wordpress conditionals ?

Posted on 2014-01-22
9
324 Views
Last Modified: 2014-01-28
Dear experts,

I am having some issues adding a text below a page header of my custom taxonomies template. For example I have the following custom taxonomy template: taxonomy-dirtax-games.php following wordpress docs such as taxonomy-{taxonomy}-{term}.php. I am trying to add a text below the header with the next code:

function stufftodo_nav_menu() {
      
      if (has_term( 'games', 'dirtax' ) ){
    echo '<div class="side-nav-text">GAMES</div>';
      }
};
add_action('genesis_after_header', 'stufftodo_nav_menu', 5);

For some reason it does not display the text unless I have post under that taxonomy. For example If I go to the Games page and there is nothing in there, the text Games won´t be displayed unless I add  a post.

Could you please help me to solve that, I am not sure if it has to do with the way I am using the conditional

Thanks
0
Comment
Question by:glepiza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 300 total points
ID: 39801875
What if you used this instead:

if (is_page_template("taxonomy-dirtax-games.php ")) {
      echo '<div class="side-nav-text">GAMES</div>';
}

Or maybe I'm misunderstanding what you are trying to do.
0
 

Accepted Solution

by:
glepiza earned 0 total points
ID: 39801902
Hi Tom,

nop, you did not missundertand me. But for some reason it is still not working. My conditional   if (has_term( 'games', 'dirtax' ) ) only works if I there is a post, otherwise it won´t show anything. Do you know if there is something like isset(has_term( 'games', 'dirtax' ) )
0
 

Author Comment

by:glepiza
ID: 39801925
Hello,

One question, I was adding that function inside function.php, but now I am adding it inside axonomy-dirtax-games.php and it seems to be working. Is that ok? Sometimes I don´t know what should go into functions.php or page templates etc...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:glepiza
ID: 39801926
I mean, I am not writing the condition, only:

function stufftodo_nav_menu() {
     
    echo '<div class="side-nav-text">GAMES</div>';
 };
add_action('genesis_after_header', 'stufftodo_nav_menu', 5);
0
 

Author Comment

by:glepiza
ID: 39801942
hmm, never mind, i need to display that word game in other templates as well. So am back to the beginning.
0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 300 total points
ID: 39801948
has_term() only checks for a matching term inside of posts so it should do nothing on a page without posts. If the parenthesis are left empty, it will check if there are any terms in the post (kind of like isset).

I'm still a little confused. If you are creating a certain custom page templates to display posts with a certain term, why don't you just place the heading directly in the page template?
0
 

Author Comment

by:glepiza
ID: 39805453
Hello Tom,

thank you very much for being willing to answer my wordpress question, I appreciate it.

I did what you suggested me to add the code directly into the template, that worked but when I was having issues on single post pages. So I ended up doing this:

if (in_array('term-stay',$classes) || (pa_in_taxonomy('directorytax', 'stay')&& (is_single()))) {
            ?>
          <div class='section-title'><span>GAMES</span></div>
      <?php
      }

So, first I was checking if the class term-stay was on the template, then I checked if the post belonged to a custom taxonomy following this tutorial :http://cazue.com/articles/wordpress-check-if-post-belongs-to-custom-taxonomy-2013
and also I checked if it was a single post.

I know this question was difficult because there were many things going on in here. But thank you for answered me because your last input led me to find the answer



Thanks!!!!

Winter
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39805468
You're welcome, glad I could be of some assistance. Thanks for the points.
0
 

Author Closing Comment

by:glepiza
ID: 39814576
I found the answer. But Tom did helped me to think about what I was doing wrong. Thanks!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question