Forums

Conditional custom field based on time (4 posts)

  1. RenFromPenn
    Member
    Posted 1 year ago #

    I know how to display a custom field. I just use the code shown here:

    $customField = get_post_custom_values("new");
    if (isset($customField[0])) {
        echo "New: ".$customField[0];
    }

    What I need to know is how to make that code display on only a certain date and then have a different custom field display if it isn't that date.

    Say that I want the code above to show on December 14, but on December 15 I want this code to be used instead:

    $customField = get_post_custom_values("previous-post");
    if (isset($customField[0])) {
        echo "Previous Post: ".$customField[0];
    }

    How would I do that?

  2. Mark / t31os
    Moderator
    Posted 1 year ago #

    I assume you just want help with the specifics of figuring out if the current day matches a specific date, see below..

    if( '12-25-2010' == date("m-d-Y") ) {
       // It's christmas day
       // month-day-year
    }
    else {
       // It's any other day
    }

    Hope that helps..

  3. RenFromPenn
    Member
    Posted 1 year ago #

    That's kind of what I'm looking to do. The only thing is I need the specific date portion of your code to be a variable. The actual date would be entered in a custom field. Can you please tell me how I could change your code to achieve that?

  4. Mark / t31os
    Moderator
    Posted 1 year ago #

    Replace the appropriate part of the example with your custom field variable?

    $your_var = get_post_meta( $post->ID, 'your_meta_key', true );
    if( $your_var == date("m-d-Y") ) {
    ....etc..

    or

    $your_var = get_post_meta( $post->ID, 'your_meta_key', true );
    if( '12-25-2010' == $your_var ) {
    ....etc..

    Not sure i follow, but i assume one of the above is what you're looking for?

    Meta functions:
    http://codex.wordpress.org/Custom_Fields#Function_Reference

    PHP basics:
    http://php.net/manual/en/control-structures.if.php
    http://www.php.net/manual/en/control-structures.else.php
    http://www.php.net/manual/en/language.operators.comparison.php
    http://php.net/manual/en/language.operators.php

Topic Closed

This topic has been closed to new replies.

About this Topic