treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Quick Question on Wordpress/PHP conditional tags.

  • Hey all,
    I have a problem and I'm sure it's a very simple fix. I have one page of my wordpress site that needs to have a different logo image than the rest of the site. It's working fine, except it's also bringing in the default logo as well. I've been playing around with if/else statements and conditional tags, but cannot wrap my head around how to get this to work. I really don't know any php, I've used just what I've seen from tutorials. Here is what I currently have: <!-- BEGIN LOGO -->
    <div class="logo">

    <?php if (is_page_template('store.php')) { ?>
    <a href="<?php bloginfo('url') ?>"><img src="<?php bloginfo('template_url'); ?>/assets/img/preemplateslogo.png" alt="Preemplates" /></a>
    <?php } ?>


    <?php $customField1 = get_option("theme_logo");
    if (isset($customField1[0])) { ?>

    <a href="<?php bloginfo('url') ?>"><img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" /></a>

    <?php } else { ?> <a href="<?php bloginfo('url') ?>"><img src="<?php bloginfo('template_url'); ?>/assets/img/bloglogo.png" alt="Preeminent Productions Blog" /></a> <?php } ?>
    </div>
    <!-- END LOGO -->


    As you can see, when you are on the "STORE" template page, I need that "preemplateslogo.png" logo to display, which it is doing perfectly, but I now need to customize the rest of this code (that came with the theme I'm using). What's happening though, is the default, regular "bloglogo.png" logo is also showing up right next to the "preemplateslogo.png" logo. How do I edit this, so that only the top statement is utilized on the "STORE" template page. I hope this makes sense to you all! Thanks!
  • I think adding an 'else' and wrapping the rest of the code should work. I think. Try:

    <!-- BEGIN LOGO -->
    <div class="logo">
    <!--Check page type-->
    <?php if (is_page_template('store.php')) { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/preemplateslogo.png" alt="Preemplates" />
    </a>

    <?php } else { ?><!-- I have added an else here... -->

    <?php $customField1 = get_option("theme_logo");
    if (isset($customField1[0])) { ?>

    <a href="<?php bloginfo('url') ?>">
    <img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" />
    </a>

    <?php } else { ?>

    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/bloglogo.png" alt="Preeminent Productions Blog" />
    </a>
    <?php } ?>
    <?php } ?><!--End of my added 'else'-->

    </div>
    <!-- END LOGO -->
  • Thank you very much for your reply. I tried it and it now throws a "Parse error: syntax error, unexpected" in the "<?php } else { ?><!-- I have added an else here... --> line.
  • oops, revert back for the time being, hopefully someone else will give it a go. :-)
  • Thanks for trying though!
  • <html>
    <body>

    <!-- BEGIN LOGO -->
    <div class="logo">
    <!--Check page type-->
    <?php

    $var = 1;

    if ( $var==1 )
    { ?>
    <a href="#">
    <img src="image1.jpg" alt="alt1" />
    </a>
    <?php }
    elseif ( $var==2 )
    { ?>
    <a href="#">
    <img src="image2.jpg" alt="alt2" />
    </a>
    <?php }

    else
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="image3.jpg" alt="alt3" />
    </a>
    <?php } ?>

    </div>
    <!-- END LOGO -->

    </body>
    </html>


    The above code worked for me, although I simplified it a bit using my own images and removing the bloginfo('url') and such so you should put that back. It should still work then however.
  • Bob! Thank you so much for your reply. Well your method kind of works, but it makes the secondary "preemplates" store logo appear on all pages of my blog. I need it to appear only on the store page. My question is this: With the $var==1 and $var==2 parts of the code. How do I assign the different logos to each of those $var's? If I can somehow assign the logo to $var==2 for example then it will work great.
  • The $var==1 and $var==2 was just an example for me to be able to test it locally, 'cause if I used is_page_template('store.php'), it wouldn't work for me since I obviously don't have that page template here on my computer.

    Can you post your code like you have it now? Or did you not modify it at all? The code I posted above worked fine for me like I said, but you have to adapt it to fit your needs like I said. Thus, replace if ( $var==1 ) with if (is_page_template('store.php')) and so on.
    Basically, from what I see, the if..elseif..else statements I created should work; you just have to adapt it to your own code, so please post back what you have now.
  • Ok, I'm starting to understand a bit more.

    Here is where I'm at now,but I'm stuck:

    <div class="logo">
    <!--Check page type-->
    <?php

    $var = 1;

    if (is_page_template('store.php'))
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/preemplateslogo.png" alt="Preemplates" />
    </a>
    <?php }
    elseif ( $var==2 )
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" />
    </a>
    <?php }

    else
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/bloglogo.png" alt="Preeminent Productions Blog" />
    </a>
    <?php } ?>

    </div>
    <!-- END LOGO -->


    I started to adapt the code for my use, but now I don't know what I replace $var =1; and elseif ( $var==2) with? In other words, with my original code(up at the top of this thread) I don't have the elseif ( $var==2 ) anywhere in use. So how should I edit that?
  • Sorry, I'm terrible with php. I hope I'm explaining myself ok? Thanks for your time!
  • Ok, check out the following code:

    <html>
    <body>

    <!-- BEGIN LOGO -->
    <div class="logo">
    <!--Check page type-->
    <?php

    if ( is_page_template('store.php') )
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/preemplateslogo.png" alt="Preemplates" />
    </a>
    <?php }

    $customField1 = get_option("theme_logo");

    elseif ( isset($customField1[0]) )
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" />
    </a>
    <?php }

    else
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/bloglogo.png" alt="Preeminent Productions Blog" />
    </a>
    <?php } ?>

    </div>
    <!-- END LOGO -->

    </body>
    </html>


    I've adapted it so it should fit your needs exactly. I put back in your links to your images etc.
    Try this code to see if it works, and if it doesn't, post back why it isn't working.
  • Ok, I'm back. It looks like you edited the original code by adding elseif instead of just if in the line elseif ( isset($customField1[0]) ) correct? That is throwing a syntax error and the site won't load with it added. This is crazy, haha. I figured it would be something so easy.
  • Can you please post the whole error it is giving?
  • Hey bob,
    Here is the error message: Parse error: syntax error, unexpected T_ELSEIF in /homepages/37/d221555405/htdocs/blog/wp-content/themes/aspire/header.php on line 133" which, of course is elseif ( isset($customField1[0]) )
  • I think you have broken the 'if' statement when running:

    $customField1 = get_option("theme_logo");


    Maybe set that before the main 'if'?
  • Thanks clokey, I'll give it a shot!
  • eh, NO GO! Didn't work, thanks though! I don't get it, there has to be a way to do this, haha.
  • Hmm, that is odd. Try removing the line:

    $customField1 = get_option("theme_logo");
    And replacing the line:
    elseif ( isset($customField1[0]) )
    with the line:
    elseif( is_page_template('templatnamehere.php') )
    Replace templatenamehere with another template you have, just make sure its different from store.php 'cause thats already being used in the first if statement so you wouldn't see any difference then. Its just for testing purposes so either create a test template and use that in the above elseif statement or use an other template you have.
    See if that helps.
  • BOB! You are awesome, thank you sir! That did the trick! But wait, so I guess I could just leave this elseif ( is_page_template('TESTtemplate.php' ) ) the way it is then? I mean I obviously won't use the "testtemplate.php" on any real page of my blog. So it won't really matter if I leave it in there. I really appreciate your time on this!
  • Glad to hear its working.

    But hang on, if you leave it like that, you do realize the <img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" />, which belongs to the elseif ( is_page_template('TESTtemplate.php' ) ), will never show up right? If you're ok with that, I'm glad because that means its working for you, but if you don't mind it showing up at all, why did you add an if statement for it in the first place (see your first post)? Don't you need the image to show sometime?
  • AH! ok, I knew I was forgetting something! Well basically that statement is grabbing a logo from the theme's settings area. It's optional. In other words, I could just link to a logo that I want used in my theme, right into my theme's setting area. And I wouldn't need to even worry about hard coding my logo into my pages. Does that make sense? So basically, I'm not using that option, obviously, and I'm hard coding it right in. HAHA, but now I realize I completely made this simple problem even more complicated, right? So taking that <img src="<?php echo get_option('theme_logo',true); ?>" alt="Logo" /> out of the equation, how should I of written this code?
  • Well if you don't need the elseif statement at all, all thats left is a simple if...else statement, which would look like this:
    <!-- BEGIN LOGO -->
    <div class="logo">
    <!--Check page type-->
    <?php

    if ( is_page_template('store.php') )
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/preemplateslogo.png" alt="Preemplates" />
    </a>
    <?php }

    else
    { ?>
    <a href="<?php bloginfo('url') ?>">
    <img src="<?php bloginfo('template_url'); ?>/assets/img/bloglogo.png" alt="Preeminent Productions Blog" />
    </a>
    <?php } ?>

    </div>
    <!-- END LOGO -->
  • fantastic. Once again, thank you so much for your help man! I wish more people could be like you!