Forums

[resolved] Conditionals in WordPress? i.e. "do not display this code on page ID =" etc? (15 posts)

  1. thehalogod
    Member
    Posted 1 year ago #

    How do I use if conditionals in WordPress? Here are all the things I'm looking to do:

    1. Do not display this code on page ID 7 (but display it on all others)
    2. Do not display this code on the home page (but display it on all others)
    3. Display this code only on the home page and no where else

    How do I use PHP / if conditionals to do this?

  2. lockettpots
    Member
    Posted 1 year ago #

  3. Chip Bennett
    Member
    Posted 1 year ago #

    1. Do not display this code on page ID 7 (but display it on all others)

    <?php if ( ! is_page( '7' ) ) { // do something } ?>

    2. Do not display this code on the home page (but display it on all others)

    <?php if ( ! is_front_page() ) { // do something } ?>

    3. Display this code only on the home page and no where else

    <?php if ( is_front_page() ) { // do something } ?>

    Note: in WordPress, the "homepage" is the Front Page:

    Front Page: the default page for the domain; corresponds to is_front_page()

    Home: the blog posts index page (may not be the Front Page); corresponds to is_home()

  4. thehalogod
    Member
    Posted 1 year ago #

    So is it safe to say this is how I'd display code from my Adsense

    <?php if ( is_front_page() ) 
    
    { // 
    
    <script type="text/javascript"><!--
    google_ad_client = "pub-XXXXXXXXXXXXXXXX";
    /* 160x600, created 10/2/11 */
    google_ad_slot = "XXXXXX";
    google_ad_width = 160;
    google_ad_height = 600;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script> } 
    
    ?>
    <?php endif; ?>

    Is that how it'd work?

  5. Chip Bennett
    Member
    Posted 1 year ago #

    Except that it's not syntactically correct, yes.

    You're combining an opening brace { with an endif;. You need to pick one method:
    { }
    or
    : endif;

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    there are actually two more syntax errors with the php tags;
    at the beginning (corrected):

    <?php if ( is_front_page() ) 
    
    { // ?>
    
    <script ........

    and towards the end (also corrected; below is all there should be at the end):

    </script> <?php } 
    
    ?>

    (i.e. there should be no <?php endif; ?> )

  7. thehalogod
    Member
    Posted 1 year ago #

    So because I'm code stupid. What would it look like if you just took what I was trying to do and pasted exactly what I was trying to make into the code area?

    Then I can just modify the beginning part to fit the 3 different situations I was mentioning?

    Sorry I only learn when I see like a completed code and then work backwards.

  8. zoonini
    help me help you
    Posted 1 year ago #

    If you go with the colon syntax (which I find easier than the curly braces):

    <?php if ( is_front_page() ) : ?>
    
    <script type="text/javascript">
    google_ad_client = "pub-XXXXXXXXXXXXXXXX";
    /* 160x600, created 10/2/11 */
    google_ad_slot = "XXXXXX";
    google_ad_width = 160;
    google_ad_height = 600;
    </script>
    
    <?php endif; ?>

    More about PHP if/else syntax:
    http://php.net/manual/en/control-structures.alternative-syntax.php
    http://ca2.php.net/manual/en/control-structures.if.php
    http://php.net/manual/en/control-structures.elseif.php

    And the invaluable WordPress conditional tags page:
    http://codex.wordpress.org/Conditional_Tags

  9. thehalogod
    Member
    Posted 1 year ago #

    <?php if ( is_page( '7' ) ) : ?>
    
    <script type="text/javascript">
    google_ad_client = "pub-XXXXXXXXXXXXXXXX";
    /* 160x600, created 10/2/11 */
    google_ad_slot = "XXXXXX";
    google_ad_width = 160;
    google_ad_height = 600;
    </script>
    
    <?php endif; ?>

    So this code would say "display this Adsense if it's on page 7, but not if it's on another page" - right?

  10. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    THat is correct

  11. thehalogod
    Member
    Posted 10 months ago #

    How would I do multiple pages?

    This didn't work...

    <?php if ( is_page( '7, 5, 6' ) ) : ?>
    
    <script type="text/javascript">
    google_ad_client = "pub-XXXXXXXXXXXXXXXX";
    /* 160x600, created 10/2/11 */
    google_ad_slot = "XXXXXX";
    google_ad_width = 160;
    google_ad_height = 600;
    </script>
    
    <?php endif; ?>
  12. Chip Bennett
    Member
    Posted 10 months ago #

    Try an array:

    <?php if ( is_page( array( '7, 5, 6' ) ) ) : ?>
  13. thehalogod
    Member
    Posted 10 months ago #

    Sweet thanks for the reply. What would you suggest I do if I wanted to try and start learning PHP? About all I know is some HTML and some CSS in a very hack style guess and check manner (which of course sucks)

    Any suggestions on the best way to get the basics down?

  14. Chip Bennett
    Member
    Posted 10 months ago #

    What would you suggest I do if I wanted to try and start learning PHP? About all I know is some HTML and some CSS in a very hack style guess and check manner (which of course sucks)

    Any suggestions on the best way to get the basics down?

    Honestly, I only know PHP because of WordPress. I say that because, IMHO, if I can learn it, anyone can.

    I would start with the Codex, learning the WordPress template tags and functions. After that, if you need to learn PHP, I would recommend the PHP Manual and w3schools.

    For specific questions, your best bet is to start with a simple Google search for "how to do X in PHP", and then go from there, using the PHP.net manual and w3schools for help with implementing specific functions.

  15. thehalogod
    Member
    Posted 10 months ago #

    Cool thanks again for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.