0

I've created a shortcode that should be showing an image depending on the day of the week.

   function custom_shortcode() {
        return '<img src="/wp-content/themes/coworker/images/daily-social-image-' . the_weekday() . '.gif" width="100%" />';
    }
    add_shortcode( 'weekday', 'custom_shortcode' );

The issue being that the function the_weekday() is not working - the rest of the code appears to be working correctly.

2 Answers 2

1

You should not echo any content in your shortcode. the_weekday() function echos the date. You can use output buffering or directly get the date:

Output buffering:

function custom_shortcode() {
    ob_start();
    the_weekday();
    $week = ob_get_contents();
    ob_end_clean();
    return '<img src="/wp-content/themes/coworker/images/daily-social-image-' . $week . '.gif" width="100%" />';
}
add_shortcode( 'weekday', 'custom_shortcode' );

Or use the global $wp_locale to filter the post's date:

By using globals:

This is the way the original function gets its contents:

function custom_shortcode() {
    global $wp_locale;
    $weekday = $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) );
    $week = apply_filters( 'the_weekday', $weekday );
    return '<img src="/wp-content/themes/coworker/images/daily-social-image-' . $week . '.gif" width="100%" />';
}
add_shortcode( 'weekday', 'custom_shortcode' );
5
  • This seems to be working, but it is grabbing the wrong weekday - I'm not sure why, but it seems to be stuck grabbing Tuesday as the weekday. Jun 15, 2017 at 18:23
  • also in your $week variable, you used $the_weekday, where $weekday should have been used. Jun 15, 2017 at 18:25
  • I believe it may be an issue with my server's chron - doesn't seem to be grabbing the correct date or time, and neither are changing as time should be, but posts and pages have the correct posted time. Jun 15, 2017 at 18:27
  • @TrentonMoore I apologize for the type error. Neither of methods return the correct date? If not, then there can be something wrong with the server, since the first method should absolutely work.
    – Johansson
    Jun 15, 2017 at 18:38
  • Yes, I've tried both - seems to be an issue with the server... Jun 15, 2017 at 18:47
1

you don't have the_weekend() function you if you are referring to the_weekday() you can do something like this to get the day of week

mb_strtolower(get_the_time('l'));

get_the_time('l') returns the day of week.Since you are outside of loop can do

global $post;
get_the_time('l', $post->ID)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.