logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Conditional header-objects per page/category

I'm trying to add conditional php for changing a header-object based on the page and category. I get a parse error unexpected T.ELSE There are over 25 pages, I've included a few as a sample to check code. There is no div ID for headerobject, I inserted under the dive class as follows:


    <div class="art-headerobject">
<?php
if(is_page('BASEBALL')||(is_category('14')){
echo 'img src="images/header-object-baseball.png" />';
} ?>
<?php
else if(is_page('BASKETBALL')||(is_category('8')){
echo 'image src="images/header-object-basketball.png" />';
} ?>
<?php
else if(is_page('HOCKEY')||(is_category('26')){
echo 'image src="images/header-object-hockey.png" />';
} ?>
else(is_page('HOME')){
echo 'image src="images/header-object-general.png" />';
} ?>
</div>

This question has been answered.

Kathy Goss | 07/07/11 at 12:40pm Edit


(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/07/11
    12:48pm
    Svilen Popov says:

    There is a missing ) in your code

    <?php
    if(is_page('BASEBALL')||(is_category('14'))){
    echo 'img src="images/header-object-baseball.png" />';
    }
    else if(is_page('BASKETBALL')||(is_category('8'))){
    echo 'image src="images/header-object-basketball.png" />';
    }
    else if(is_page('HOCKEY')||(is_category('26'))){
    echo 'image src="images/header-object-hockey.png" />';
    }
    else(is_page('HOME')){
    echo 'image src="images/header-object-general.png" />';
    }
    ?>

    Previous versions of this answer: 07/07/11 at 12:48pm

  • avatar
    Last edited:
    07/07/11
    12:53pm
    John Cotton says:

        		<div class="art-headerobject">

    <?php
    if( is_page('BASEBALL') || is_category('14') ) {
    echo 'img src="images/header-object-baseball.png" />';

    } else if( is_page('BASKETBALL') || is_category('8') ) {
    echo 'image src="images/header-object-basketball.png" />';

    } else if( is_page('HOCKEY') || is_category('26') ) {
    echo 'image src="images/header-object-hockey.png" />';

    } else if( is_page('HOME') ) {
    echo 'image src="images/header-object-general.png" />';

    }
    ?>

    </div>

    Previous versions of this answer: 07/07/11 at 12:53pm

    • 07/07/11 2:49pm

      Kathy Goss says:

      Hi thanks for the reply. I am not getting the parsing error with your code change but I am not getting the images at all.

    • 07/07/11 10:37pm

      Kathy Goss says:

      Hi John,
      My major problem is that my original web designer is no longer available. Originally, we were going to have a separate site for each sport category so she just defined a single header object in the CSS style as:


      /* begin HeaderObject */
      div.art-headerobject
      {
      display: block;
      left: 0;
      position: absolute;
      top: 0;
      width: 481px;
      height: 522px;
      background-image: url('images/header-object.png');
      background-repeat: no-repeat;
      }


      We since decided it made more sense to just add category pages for alternative sports and I was told to accomplish this was to put the conditional statements in the header.php. Your code accomplished removing the parsing error, however, the images do not change. Do we need to define anything more in the CSS as well?

    • 07/08/11 4:45am

      John Cotton says:

      Hi Kathy

      For a moment, modify the code as below. At least then you'll see if it's just the tests that are failing.


      <div class="art-headerobject">
      <?php

      if( is_page('BASEBALL') || is_category('14') ) {

      echo '<img src="images/header-object-baseball.png" />';



      } else if( is_page('BASKETBALL') || is_category('8') ) {

      echo '<image src="images/header-object-basketball.png" />';



      } else if( is_page('HOCKEY') || is_category('26') ) {

      echo '<image src="images/header-object-hockey.png" />';



      } else if( is_page('HOME') ) {

      echo '<image src="images/header-object-general.png" />';
      } else {
      echo 'Nothing to show!';
      }
      ?>
      </div>


      John

    • 07/08/11 4:46am

      John Cotton says:

      Hi Kathy

      For a moment, modify the code as below. At least then you'll see if it's just the tests that are failing.


      <div class="art-headerobject">
      <?php

      if( is_page('BASEBALL') || is_category('14') ) {

      echo '<img src="images/header-object-baseball.png" />';



      } else if( is_page('BASKETBALL') || is_category('8') ) {

      echo '<img src="images/header-object-basketball.png" />';



      } else if( is_page('HOCKEY') || is_category('26') ) {

      echo '<img src="images/header-object-hockey.png" />';



      } else if( is_page('HOME') ) {

      echo '<img src="images/header-object-general.png" />';
      } else {
      echo 'Nothing to show!';
      }
      ?>
      </div>


      John

  • avatar
    Last edited:
    07/07/11
    12:47pm
    Utkarsh Kukreti says:

    You're missing ( and ) on all your if statements.

        	if(is_page('BASEBALL')||(is_category('14')){


    should be

        	if((is_page('BASEBALL')||(is_category('14'))){

  • avatar
    Last edited:
    07/07/11
    12:50pm
    Joshua Nelson says:

    You're missing some callouts and else ifs are acutally elseif. I'd change it to ditch the echos while you're at it - like this:


    <div class="art-headerobject">

    <?php if(is_page('BASEBALL')||(is_category('14')){?>

    <img src="images/header-object-baseball.png" />

    <?php } elseif(is_page('BASKETBALL')||(is_category('8')){ ?>

    <image src="images/header-object-basketball.png" />

    <?php } elseif(is_page('HOCKEY')||(is_category('26')){ ?>

    <image src="images/header-object-hockey.png" />

    <?php } else(is_page('HOME')){ ?>

    <image src="images/header-object-general.png" />

    <?php } ?>

    • 07/07/11 2:24pm

      Kathy Goss says:

      I still get this with your code:

      Parse error: syntax error, unexpected '{' in /home/content/31/7500231/html/sportslife/wp-content/themes/Basketball/header.php on line 54

    • 07/07/11 2:29pm

      Joshua Nelson says:

      You can't have a else with a condition, I missed that. Did you want the last one to be a sort of default? then it would just be } else { on the final one, and that will result if none of the other if states are satisfied.

      Try this:


      <?php if(is_page('BASEBALL')||(is_category('14')) {?>
      <img src="images/header-object-baseball.png" />
      <?php } elseif(is_page('BASKETBALL')||(is_category('8')) { ?>
      <image src="images/header-object-basketball.png" />
      <?php } elseif(is_page('HOCKEY')||(is_category('26')) { ?>
      <image src="images/header-object-hockey.png" />
      <?php } elseif(is_page('HOME')) { ?>
      <image src="images/header-object-general.png" />
      <?php } ?>


      or this if you want the default for the last line


      <?php if(is_page('BASEBALL')||(is_category('14')) {?>
      <img src="images/header-object-baseball.png" />
      <?php } elseif(is_page('BASKETBALL')||(is_category('8')) { ?>
      <image src="images/header-object-basketball.png" />
      <?php } elseif(is_page('HOCKEY')||(is_category('26')) { ?>
      <image src="images/header-object-hockey.png" />
      <?php } else { ?>
      <image src="images/header-object-general.png" />
      <?php } ?>

    • 07/07/11 3:04pm

      Kathy Goss says:

      Joshua, I do want the last one to be a default. I tried your second option and still got:

      Parse error: syntax error, unexpected '{' in /home/content/31/7500231/html/sportslife/wp-content/themes/Basketball/header.php on line 54

    • 07/07/11 7:22pm

      Joshua Nelson says:

      Kathy,

      Try this:


      <?php if(is_page('BASEBALL')|| is_category('14')) {?>
      <img src="images/header-object-baseball.png" />
      <?php } elseif(is_page('BASKETBALL')|| is_category('8')) { ?>
      <image src="images/header-object-basketball.png" />
      <?php } elseif(is_page('HOCKEY')|| is_category('26')) { ?>
      <image src="images/header-object-hockey.png" />
      <?php } else { ?>
      <image src="images/header-object-general.png" />
      <?php } ?>


      You don't need additional () around statements like is_category or is_page so we were missing some closing ). That should work now. Sorry for the runaround, doing a few things at once... Seems this site was down for a bit, too.

  • avatar
    Last edited:
    07/07/11
    10:45pm
    Pixel Coder says:

    Add this to functions.php in your theme directory.


    add_theme_support('post-thumbnails', array('page'));
    // Change 960 to the width of your header image in pixels.
    // Change 200 to the height of your header image in pixels.
    add_image_size('header', 960, 200);


    Then place this in the file you would like the image to appear.


    <div class="art-headerobject">
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <?php the_post_thumbnail('header'); ?>
    <?php endwhile; else : ?>
    <img src="http://lorempixum.com/960/200/sports/" alt="Default image" />
    <?php endif; ?>
    </div>


    Now you can add a custom header image to each page in the WordPress admin.

    To do this look for the "Featured image" option to the lower right when editing a page.

    Click "Set featured image", upload the image and make sure to chose the option to "Set as featured" once uploaded.

    Hope that helps.

    Previous versions of this answer: 07/07/11 at 10:42pm | 07/07/11 at 10:43pm | 07/07/11 at 10:43pm | 07/07/11 at 10:45pm

    • 07/07/11 11:04pm

      Kathy Goss says:

      Alistair,
      Thank you for the response. I'll try it out tomorrow. Interesting approach. My previous designer created the header with multiple items. The header oject is only part of the header. She defined the header-object in CSS as:


      /* begin HeaderObject */
      div.art-headerobject
      {
      display: block;
      left: 0;
      position: absolute;
      top: 0;
      width: 481px;
      height: 522px;
      background-image: url('images/header-object.png');
      background-repeat: no-repeat;
      }


      Would this added information change the approach?

    • 07/07/11 11:08pm

      Pixel Coder says:

      Hi Kathy,

      Not at all, the CSS styles only that area of the site.

      div.art-headerobject is saying div with a class of art-headerobject.

      Without looking at your site the div.art-headerobject might be contained within another element which has a CSS attribute of


      .art-container {
      position: relative;
      padding: 1px;
      background: #FFF;
      }


      Above is only an example.

      All the code I posted does is allow you to create images as you wish instead of diving into code each time you desire a new header image.


      Best

    • 07/10/11 9:32am

      Kathy Goss says:

      Hi Alistair,
      I really like this approach, but I'm not certain which file you are referring to:
      "Then place this in the file you would like the image to appear." - Would this still go into header.php?

  • avatar
    Last edited:
    07/08/11
    12:49am
    alchemist alchemist says:

    Hi ,

    You were missing some parenthesis ')' at the end of your if statements and also the correct fomat for iamge tag is :

    <img src='source/path/here' />

    I have corrected the code as shown below, have a try :


    <div class="art-headerobject">

    <?php

    if( (is_page('BASEBALL')) || (is_category('14')) ){

    echo "<img src='images/header-object-baseball.png' />";

    }

    ?>

    <?php

    else if( (is_page('BASKETBALL')) || (is_category('8')) ){

    echo "<img src='images/header-object-basketball.png' />";

    } ?>

    <?php

    else if( (is_page('HOCKEY')) || (is_category('26')) ){

    echo "<img src='images/header-object-hockey.png' />";

    } ?>

    <?php
    else if( is_page('HOME') ){

    echo "<img src='images/header-object-general.png' />";

    } ?>

    </div>


    Previous versions of this answer: 07/08/11 at 12:49am

  • avatar
    Last edited:
    07/10/11
    1:38pm
    Sergio V. de la Garza says:

    Hi Kathy, here is the code you should use to get the image that belongs to each page or category.

    the code you give us sometimes you need to use the exact path to your files on the server that's why i added <?php bloginfo('template_directory'); ?>

    cheers!

    <div class="art-headerobject">

    <?php

    if (is_page('baseball') || is_category('14')) { ?>

    <img src="<?php bloginfo('template_directory'); ?>/images/header-object-baseball.png" />

    <?php } elseif (is_page('basketball') || is_category('8')) { ?>

    <img src="<?php bloginfo('template_directory'); ?>/images/header-object-basketball.png" />

    <?php } elseif (is_page('hockey') || is_category('26')) { ?>

    <img src="<?php bloginfo('template_directory'); ?>/images/header-object-hockey.png" />

    <?php } elseif (is_page('home')) { ?>

    <img src="<?php bloginfo('template_directory'); ?>/images/header-object-general.png" />

    <?php } else { ?>

    <img src="<?php bloginfo('template_directory'); ?>/images/header-object-general.png" />

    <?php } ?>
    </div>

    =============================
    i updated the code to show header-object-general.png on non specified categories
    cheers!

    Previous versions of this answer: 07/10/11 at 1:38pm

    • 07/10/11 8:06am

      Kathy Goss says:

      Sergio,
      Your code works and adding <?php bloginfo('template_directory'); ?>

      was required to bring up the pictures.

      How would I add the else default for all other pages not specified?

    • 07/10/11 3:57pm

      Kathy Goss says:

      This solution worked perfectly. Thanks.

This question has expired.



Kathy Goss voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.