Forums

Conditional tag help (4 posts)

  1. SS_Minnow
    Member
    Posted 6 months ago #

    I'm trying to write some conditional tag code so that I can show different banner images on different pages of a site. I started with this, to show a different header on the "disclaimer" page:

    <?php
      if (is_page("disclaimer")) {
      echo "<div id="header" style=" background:url('<?php bloginfo('url'); ?>/wp-content/themes/themename/images/top2.jpg') no-repeat bottom; ">";
      } else {
       echo "<div id="header" style=" background:url('<?php bloginfo('url'); ?>/wp-content/themes/themename/images/top.jpg') no-repeat bottom; ">";
      }
    ?>

    The error message I'm getting is:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/myfoldername/public_html/1/wp-content/themes/themename/header.php on line 31

    Line 31 is the first of the two "echo" lines, and it does end with a semi-colon, so I'm not sure what I'm doing wrong.

    If I get rid of the conditional stuff and just have

    <div id="header" style=" background:url('<?php bloginfo('url'); ?>/wp-content/themes/themename/images/top.jpg') no-repeat bottom; ">

    It works fine but then of course it displays the same header graphic on every page.

    myfoldername and themename are replacements of the real names for the purposes of this example. In reality, they are both something else.

    Thanks

  2. mfields
    Member
    Posted 6 months ago #

    Your php code is wrong - you have nested your <?php ?> blocks as well as put a function that prints a string inside of a string. Please try the following code:

    <?php
      if (is_page("disclaimer")) {
      echo '<div id="header" style="background:url(' .  get_bloginfo('url') . '/wp-content/themes/themename/images/top2.jpg) no-repeat bottom; ">';
      } else {
       echo '<div id="header" style="background:url(' . get_bloginfo('url') . '/wp-content/themes/themename/images/top.jpg) no-repeat bottom; ">';
      }
    ?>
  3. SS_Minnow
    Member
    Posted 6 months ago #

    Excellent, thank you. Ultimately there will be about 5 pages that will each have their own header graphic and also a default header graphic for posts, search results, etc. but I wanted to get one page to work first.

    I'm pretty new to PHP scripting (obviously) and until now have been a code hacker rather than a code writer. I guess everyone has to start somewhere.

  4. mfields
    Member
    Posted 6 months ago #

    No problem skipper!

Reply

You must log in to post.

About this Topic