Forums

[resolved] Conditional Statement (11 posts)

  1. ellaj
    Member
    Posted 3 years ago #

    Hi,

    I would like to know how to write and where to put a conditional statement.

    On the post page (single.php), I need to be able to have a separate header style depending on the year. Basically, if it's 2009 or before use style a...if not use style b. So all posts from this year forward will have style a.

    Hope that made sense and thank you.

  2. MichaelH
    Member
    Posted 3 years ago #

    If you create a different header file for each year of your posts, such as header-2010, header-2009, header-2011 you don't even have to do a conditional--just something like this:

    <?php
    $year = mysql2date("Y", $posts[0]->post_date);
    get_header($year); //will use header-xxxx where xxxx is the year
    ?>

    But just in case:

    <?php
    $year = mysql2date("Y", $posts[0]->post_date);
    if ($year == '2010') {
      //do your 2010 thing here
    } elseif ($year == '2009') {
      //do your 2009 thing here
    }
    ?>
  3. ellaj
    Member
    Posted 3 years ago #

    Thanks, Michael. Someone told me to do:
    <div class="content_top-<?php if(get_the_time('Y')<='2009') echo 'no_header'; ?>">
    Is there a right way or wrong way?

    Thanks again.

  4. MichaelH
    Member
    Posted 3 years ago #

    Right way = what works for you ;)

  5. ellaj
    Member
    Posted 3 years ago #

    Sounds good to me.

    In addition to the posts from 2009 and earlier having 'style a', I need to also give posts that don't have the lead_image custom field value to have 'style a' as well.

    I have a posts from 2010, but there is no 'lead_image' custom field associated with it so I need it to have the style that earlier posts do.

    Can I add something like (get_post_meta($post->ID, 'books', true)): in there somewhere? (I tried that bit and it didn't work at all.) As you can tell I don't know php.

    Thank you for any help.

  6. MichaelH
    Member
    Posted 3 years ago #

    Will offer

    <?php
    $books = get_post_meta($post->ID, 'books', true);
    if ($books){
    //there is a books custom field on this post
    }
    ?>
  7. ellaj
    Member
    Posted 3 years ago #

    OK. Now I'm really confused...this is what I did because I have no idea how to combine if statements:

    <?php $lead_image = get_post_meta($post->ID, 'lead_image', true); ?>
    
           <div class="content_top-<?php if(get_the_time('Y')<='2009') && $lead_image == 'true' { echo 'no_header';} ?>">

    Thank you!

  8. MichaelH
    Member
    Posted 3 years ago #

    if (get_the_time('Y')<='2009' && $lead_image == 'true' )
  9. ellaj
    Member
    Posted 3 years ago #

    Thank you for your help Michael. It didn't work and I know there is a bunch wrong with the code. I realized it would be safe to avoid using the date because it won't matter. All that matters is whether or not a post has a 'lead_image' custom field or not. Here's the code from the top of the loop. What am I doing wrong?

    <?php
      wp_reset_query();
      if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <?php $lead_image = get_post_meta($post->ID, 'lead_image', true); ?>
    
    <div id="single" class="single-narrow">
      <div class="post post-single">
    		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
          <?php if (strlen($wpzoom_ad_content_imgpath) > 1 && $wpzoom_ad_content_select == 'Yes' && $wpzoom_ad_content_pos == 'Before') { echo '<div class="sep">&nbsp;</div><div class="banner">'.stripslashes($wpzoom_ad_content_imgpath)."</div>"; }?>      
    
    	 <div class="content_top-<?php if(get_the_time('Y')<='2009' && $lead_image == 'false') echo 'no_header'; ?>">
           <div class="wrap">
           		<img src="<?php echo $lead_image; ?>" alt="Image for Post #<?php the_ID(); ?>" />
    
    			<div class="title">
         			<img src="<?php bloginfo('stylesheet_directory'); ?>/images/brownlens.png" alt="Lens Icon" width="43px" height="56px" class="lens" />
          			<h1 class="spt"><?php the_title(); ?></h1>
          			<p id="title_data">in <span class="category"><?php the_category(', '); ?></span> by <?php the_author_posts_link(); ?> <span class="datetime"> &mdash; <?php the_time("$dateformat \a\\t $timeformat"); ?></span> | <a href="<?php the_permalink() ?>#commentspost" title="Jump to the comments"><?php comments_number('0 comments','1 comment','% comments'); ?></a><?php edit_post_link( __('Edit'), ' | ', ''); ?></p>
          		</div><!-- end title -->
         	</div>
          </div>
    
    				<div class="sep">&nbsp;</div>
    
            <?php the_content(''); ?>
  10. MichaelH
    Member
    Posted 3 years ago #

    So maybe:
    delete this line:

    <?php $lead_image = get_post_meta($post->ID, 'lead_image', true); ?>

    and change this

    <div class="content_top-<?php if(get_the_time('Y')<='2009' && $lead_image == 'false') echo 'no_header'; ?>">
           <div class="wrap">
           		<img src="<?php echo $lead_image; ?>" alt="Image for Post #<?php the_ID(); ?>" />

    to this:

    <?php
      $lead_image = get_post_meta($post->ID, 'link_url', true);
      if (!$lead_image ) { ?>
        <div class="content_top-no_header">
      <?php } else { ?>
        <div class="content_top-<?php echo get_the_time('Y'); ?>">
          <img src="<?php echo $lead_image; ?>" alt="Image for Post #<?php the_ID(); ?>" />
       <?php } ?>
     </div> <!--content_type-xxxxxxx -->
  11. ellaj
    Member
    Posted 3 years ago #

    Michael,
    That worked! You are awesome!
    Thank you SO MUCH for all of your help. Taking the time to see this through means a lot.
    Seriously, I can't thank you enough.
    :)

Topic Closed

This topic has been closed to new replies.

About this Topic