WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] help with PHP nested IF Category array '2008','2009' (6 posts)

  1. boots
    Member
    Posted 5 years ago #

    Hi - The code below in my index.php works today in its present form sans the comments. I would like this code to only work if the post is from categories named '2008' or '2009'.

    I don't know how to add the if test for 2008 or 2009 after looking for clues online. It would be acceptable to rework my original code to make this work instead of just having a "php if category" wrap around the entire block.

    I believe this is the intital test: if is_category(array('2008','2009'))

    Thanks in advance for your helping out.

    <?php $expired = get_post_meta($post->ID, 'expdate', $single = true); $todays_date = date("Y-m-d"); ?>  <!-- from custom field -->
    
    <!-- NEED PHP IF HERE TO TEST IF POST IS FROM CATEGORY NAMED '2008' or '2009' -->
    <!-- IF CATEGORY == '2008' or '2009' DO THIS CODE ELSE SKIP THIS CODE -->
    
        <?php if( $todays_date < $expired ) { ?>
            <?php if (function_exists('sharethis_button')) { sharethis_button(); } ?>
        <?php } else { ?>
            <?php echo '<span style="color:#334D84;font-weight:bold">The Party is Over...</span>' ; ?>
    
        <?php } ?>
    
    <!-- NEED TO CLOSE NEW PHP IF HERE -->
    
        <?php the_content(__('Read more'));?><div style="clear:both;"></div>

    Tks.

    -Boots

  2. boots
    Member
    Posted 5 years ago #

    I tried adding the is_category test and closing arg below without success. It provides the same functionality as before. I tested with an echo that I have the right category, 2008 or 2009, in memory. Even on posts from other categories my code with 'sharethis_button' and echo 'The Party is Over...' is executing while discriminating on $todays_date < $expired.

    <?php $expired = get_post_meta($post->ID, 'expdate', $single = true); $todays_date = date("Y-m-d"); ?>  <!-- from custom field -->
    
        <?php if (is_category == '2008' || '2009' ) { ?>
    
        <?php if( $todays_date < $expired ) { ?>
            <?php if (function_exists('sharethis_button')) { sharethis_button(); } ?>
        <?php } else { ?>
            <?php echo 'The Party is Over...' ; ?>
    
        <?php } ?>
    
        <?php } ?>
    
        <?php the_content(__('Read more'));?><div style="clear:both;"></div>
  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

  4. boots
    Member
    Posted 5 years ago #

    Otto42: I had read the in_categoy function. Note I am operating in the loop. What else am I missing?

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <h1><?php the_title(); ?></h1>
                <?php $expired = get_post_meta($post->ID, 'expdate', $single = true); $todays_date = date("Y-m-d"); ?>
    
    test for category:
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
    
        <?php if (in_category == '2008' || '2009' ) { ?>
        <?php if( $todays_date < $expired ) { ?>
            <!-- do nothing -->
        <?php } else { ?>
            <?php echo '<span style="color:#334D84;font-weight:bold">The Party is Over...&nbsp;&nbsp;did you have GOOD time?</span> ' . ' <span style="color:#666666";> ' ; ?>  <br /><br />
        <?php } ?>
        <?php } ?>
    
                <?php the_content(__('Read more'));?><div style="clear:both;"></div>
    
        <?php if (in_category == '2008' || '2009' ) { ?>
        <?php if( $todays_date < $expired ) { ?>
            <?php if (function_exists('sharethis_button')) { sharethis_button(); } ?>  <br />    <br />
        <?php } else { ?>
            <?php echo '<span style="color:#334D84;font-weight:bold">add your 2 cents below...</span> ' . ' <span style="color:#666666";> '; ?>    <br />    <br />
        <?php } ?>
        <?php } ?>
    
                <div class="postmeta">
                    <p><!--  Written by <?php the_author(); ?> &middot; Filed Under <?php the_category(', ') ?>&nbsp;<?php edit_post_link('(Edit)', '', ''); ?><br />Tagged: <?php the_tags('') ?> --> </p>
                </div>
    
                <?php endwhile; else: ?>
    
                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    if (in_category == '2008' || '2009' )

    That ain't right. Did you look at any of the examples on the page I told you? Your code looks nothing like those examples, now does it?

    Hint: Look REALLY CLOSE at this:
    http://codex.wordpress.org/Template_Tags/in_category#Examples

  6. boots
    Member
    Posted 5 years ago #

    Hey Otto - Paying attention paid off. Thanks. Boots

    <?php
    IF   ( in_category(5) || in_category (6) )
    {
    IF  ($todays_date < $expired)
    {
    sharethis_button(); echo '<br /> . <br />' ;
    } else {
    echo 'add your 2 cents...';> . <br /> . <br />' ;
    }
    } else {
    ;
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic