0

I am trying to run a code only in blog posts, and another code only in single pages. But I can't even make a single working php if statement, that contains a wordpress conditional tag.

In footer.php, I tried to make some simple functions

if( is_singular() ) {
    // should show work in singular pages, bot is not working
}

In the below code, the code4 get's executed, on the front page

if ( is_front_page() && is_home() ) {
  code1;
} elseif ( is_front_page() ) {
  code2;
} elseif ( is_home() ) {
  code3;
} else {
  code4; 
}

Can you please tell me what am I doing wrong? And how can I make some codes only for blog posts, and some only for single pages?

0

It’s just that you got it a bit wrong - it’s actually

if(is_single())

(not singular). If you want to address pages in general (not posts I assume), you would use

if(is_page())

. If you need to address a specific page you could use its ID like so

if(is_page(13))

where 13 would be the ID of the page. But you have a lot of options - see http://codex.wordpress.org/Conditional_Tags .

However, of course you can use conditional tags in both header and footer (i.e. outside the loop) to tailor your template better ;).

3
  • If you are unsure of how to correctly form PHP just take a look at the examples, it is a bit tricky at first, but you’re getting used to it. – physalis Feb 20 '15 at 23:50
  • It's not that, I tried other conditionals, but they all return negative. – Marian07 Feb 21 '15 at 9:57
  • So, none is actually working? Can you try putting up <?php if(is_single()) { echo 'This is working!'; } ?> into your footer.php and see whether it is being shown on the website/in the source code? – physalis Feb 22 '15 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.