• Resolved paa1605

    (@paa1605)


    Hi folks,

    Im trying to write some code that contains php within php. Whatever i do seems to result in an error so can anyone help. basically it is a conditional statement that asks wordpress if this is a certain category then to echo something.

    <h1>
       <?php
       if ( is_category('new-this-week') ) {
    					echo 'New This Week';
    					} elseif ( is_category('new-this-month') ) {
    					echo '<?php the_time('F, Y') ?>';
    					} else {
    					}
        ?>
      </h1>

    Thanks to anyone that can help

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try changing this echo '<?php the_time('F, Y') ?>'; to this echo the_time('F, Y');

    Thread Starter paa1605

    (@paa1605)

    Fantastic, works perfectly! Thanks for that.

    I don’t suppose you know if its possible for WordPress to echo a date range? For example, rather than have the above code echo ‘new this week’, is there code that will enable it to echo ‘posts from 10-17 November’ and then on the next day automatically change to echo ‘posts from 11-18 November’ and so on?

    Thanks for your help.

    Moderator keesiemeijer

    (@keesiemeijer)

    Did you mean, only show Posts from 10-17 November?
    Look at the time parameters of WP_Query.
    https://codex.www.ads-software.com/Function_Reference/WP_Query#Time_Parameters

    There is a PHP inconsistency in your code. Either you’re IN PHP script mode or your are outside (HTML mode). Change echo '<?php the_time('F, Y') ?>'; to just the_time('F, Y');

    @keesiemeijer:
    The function the_time() will output the time of the post. Nothing to echo in this case.

    Thread Starter paa1605

    (@paa1605)

    @knut Sparhell

    So i don’t need the echo, just the_time('F, Y'); ? It appears to work either way.

    @keesiemeijer

    No i don’t want it to display posts from a date range as i already have that in the code that follows. Its just an h1 tag on the sidebar and i would like it to automatically echo the time period that the posts are showing, which in this case is for the last 7 days. So if today was November 17th it would simply echo ‘posts from November 10-17’. Then tomorrow it would echo ‘posts from November 11-18’ and so on. Is this possible?

    Thanks to both of you for your help.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    <?php
    echo date('d'); // today
    echo date('d', strtotime('-1 week')); // today minus 7 days
    ?>

    https://php.net/manual/en/function.date.php
    https://www.the-art-of-web.com/php/strtotime/

    echo the_time() will output exactly the same as the_time()

    Point is, you have an extra <?php inside a php block. This will give an error.

    Moderator keesiemeijer

    (@keesiemeijer)

    something like this:

    <?php
    $this_month = date('F');
    $this_month_minus_seven_days = date('F', strtotime('-1 week'));
    // check if today minus 7 days is the same month
    if($this_month != $this_month_minus_seven_days) {
    echo 'posts from '. date('F d', strtotime('-1 week')).' - '. date('F d');
    } else {
    echo 'posts from '. date('F d', strtotime('-1 week')).' - '.date('d');
    }
    ?>

    Thread Starter paa1605

    (@paa1605)

    Awesome!

    Much appreciated.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Glad you got it resolved.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Help needed with php within php code’ is closed to new replies.