WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using the_date() in php if elseif else statements (10 posts)

  1. Revion
    Member
    Posted 1 year ago #

    So this is what I'm trying to do:

    Change the css background-color of a div that surrounds my date (I have it all nice and fancy :P ) depending on the month that the post was written. I am attempting to accomplish this by using the_date within a series of if elseif statements and depending on what the_date('m') outputs suchas 11 then the color for november posts will be #123456. Here's my code:

    <?php
    /*This is php code to change the background color of the meta circle of a post depending on the month*/
    /*setting $monthColor equal to the numerical month value */
    $month = the_date("m", FALSE);
    
    if ($month == "11"){
    echo "aklsjdflfjasdlkjflskjdflkasjdf";
    }else{
    echo "AKSJDLKJDFLKJSDLKFJLDS";
    }
    
    if ($month == '1') {
    $monthColor = "#5E7CFF";
    } elseif ($month == '2') {
    $monthColor = "#FFD06F";
    }  elseif ($month == '3') {
    $monthColor = "#55B541";
    }  elseif ($month == '4') {
    $monthColor = "#B24D2E";
    }  elseif ($month == '5') {
    $monthColor = "#528CB5";
    }  elseif ($month == '6') {
    $monthColor = "#96B523";
    }  elseif ($month == '7') {
    $monthColor = "#630907";
    }  elseif ($month == '8') {
    $monthColor = "#23634B";
    }  elseif ($month == '9') {
    $monthColor = "#0B4B63";
    }  elseif ($month == '10') {
    $monthColor = "#634C20";
    }  elseif ($month == "11") {
    $monthColor == "#F0CF44";
    }  elseif ($month == '12') {
    $monthColor = "#42BDF0";
    } else {
    $monthColor = "#000000";
    }
    echo $monthColor;
    ?>

    Here's the code that I'm using and is working (using the else statement from the above code to display black):

    <div class="posts_Date" style="background-color:<?php echo($monthColor); ?>;">

    Any help with this would be much appreciated!!! :)

  2. Revion
    Member
    Posted 1 year ago #

    One more thing, This snippet of code is just checking to see if the value of the_date('m') is equal to 11 and if so to echo the string value. What doesn't make sense is that according to what is being outputted the_date('m') will echo 11 BUT the_date('m') does not equal 11. (11 is the month that my post was written)

    if ($month == "11"){
    echo "aklsjdflfjasdlkjflskjdflkasjdf";
    }else{
    echo "AKSJDLKJDFLKJSDLKFJLDS";
    }
  3. Andrew Bartel
    Member
    Posted 1 year ago #

    Have you tried just using get_the_date('m') instead?

  4. Revion
    Member
    Posted 1 year ago #

    Unfortunately (unless I'm just using the get_the_date method wrong) it will then not post any information and it won't even enter into the if elseif statements, thus leaving the background color white.

  5. Andrew Bartel
    Member
    Posted 1 year ago #

    Ok, I'm confused. Why wouldn't this work?

    <?php
    $month = get_the_date('m');
    if($month == 1) $color = '#5E7CFF';
    elseif($month == 2) $color = '#FFD06F';
    // etc etc
    ?>
    
    <div class="posts_Date" style="background-color:<?php echo $color; ?>">

    This would also be an ideal place for a switch

  6. Revion
    Member
    Posted 1 year ago #

    This is why I posted this on here... The the_date('m') echo is 11 (it prints it onto the post) but when I do if the_date('m') == "11" it returns false...which makes no sense in my mind 11=/=11... I'll look at what you just posted and see if that works...

  7. Revion
    Member
    Posted 1 year ago #

    IT WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!! :) :) :) Thank you so much!!!!

  8. alchymyth
    Forum Moderator
    Posted 1 year ago #

    The the_date('m') echo is 11 (it prints it onto the post)

    exactly;
    the_date() prints the result immediately, and it is not available for any string operation or conditional statements.

    you need to use a function which returns the result, like get_the_date()

    http://codex.wordpress.org/Function_Reference/get_the_date

    edit:
    my reply was too late - well done.

  9. Revion
    Member
    Posted 1 year ago #

    When I was reviewing the codex none of that clicked for me thanks for everything guys!!!

  10. Andrew Bartel
    Member
    Posted 1 year ago #

    When I was reviewing the codex none of that clicked for me

    Ah, sorry I didn't explain it in better detail. Alch is always really good about that.

    Remember, if you ever have questions about a function in WordPress, you can always just go look at the code.

Topic Closed

This topic has been closed to new replies.

About this Topic