• Resolved stevebar84

    (@stevebar84)


    Hi,

    right, this is the new problem that is keeping me up all night.

    i have created a function which allows me to get the date that a post was published, and i’ve created a shortcode so i can call it in the visual editor.

    function thedate(){
    return get_the_date('F jS,');
    }
    add_shortcode('date', 'thedate');

    so i then do the [date] shortcode in the visual editor and when i load the page it shows the date that page/post was first published.

    great.

    except that i want to display the date that a DIFFERENT post was published.

    ie, i’d type [date id='243'] and the page would display the date that the post with id 243 was first published rather than the date of the page the shortcode is written in.

    any ideas? i feel like i’ve tried everything!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter stevebar84

    (@stevebar84)

    Ok, i’m getting closer.

    by adding

    function thedate(){
    	$post = '243';
    return get_the_date('F jS,',$post);
    }
    add_shortcode('date', 'thedate');

    i can get it to display the post date for post 243.

    but, i want to be able to set the $post =’ ‘; value using value i place in the [date id=”] shortcode.

    there must be a way to code my shortcode to output the id attribute as $post = ”; right?

    Moderator bcworkz

    (@bcworkz)

    Yes there is! How is described in Shortcode API. If that isn’t clear enough for you (I had to re-read it many times myself), maybe this will help:
    https://developer.www.ads-software.com/plugins/shortcodes/

    Thread Starter stevebar84

    (@stevebar84)

    Hmmm, from the examples it looks like it should be easy, but i’m getting lost somewere. Any ideas on what code i need to try?

    i’ve cobbled together something this, but it doesn’t work!

    function thedate($atts){
    
    return get_the_date('F jS,',$post);
    }
    add_shortcode('date', 'thedate');
    
    function date_shortcode( $atts ) {
        $a = shortcode_atts( array(
            'id' => $post,
    
        ), $atts );
    
        return "id = {$a['id']}";
    }

    any ideas where i’m going wrong?

    Moderator bcworkz

    (@bcworkz)

    Yeah, you made a few wrong turns. Examples are often deceptively simple, then when you adapt them to your application it all falls apart!

    Mainly, your shortcode right now only causes thedate() to execute. To also execute date_shortcode() you need to call it from thedate(). Actually, it’s probably the other way around. To execute date_shortcode() and not thedate(), specify it in the add_shortcode() like so: add_shortcode('date', 'date_shortcode'); Then call thedate() from date_shortcode().

    If you want to get values from the current post in the loop, you need to declare $post as global. In any case, you wouldn’t want the entire post object as a default for ‘id’. Perhaps $post->ID, but actually the current post ID is better had with get_the_ID().

    Same goes for get_the_date(), don’t pass the entire post object, only the ID, as above or the passed attribute as a$['id']. Don’t put that last one in quotes. In your last line you are returning a string literal of a PHP statement instead of an ID numeric value. This is where you could call thedate(), but use what’s passed to the function (post ID as $atts) in get_the_date() instead of $post.

    I really hope you can work this out, it’s important to understand what’s happening here. It’d be much easier just to provide the code, but then you wouldn’t learn anything. Do the best you can and let’s see how you do.

    Thread Starter stevebar84

    (@stevebar84)

    Hi bcworkz,

    Ah, so the function wasn’t actually being asked to check the post id because i wasn’t actually triggering the date_shortcode function i’d written!

    So what if i tried something more like this?

    add_shortcode('date', 'date_shortcode');
    
    function date_shortcode( $atts ) {
        $a = shortcode_atts( array(
            'id' => get_the_ID(),
    
        ), $atts );
    
        return get_the_date('F jS,', a$'id');
    }

    so with this code my [date id=’243′] shortcode would call the date_shortcode function rather than thedate(), and therefore should get the ID of the page?

    I feel like i need to specify that my id=’234′ in the shortcode = the a$’id’ in the return somehow?

    or should my return be "id = {$a['id']}";? but i suppose that then i’m not calling the date at all!

    and have i got 'id' => get_the_ID(), the correct way round? (or even correct at all!!)

    What do you think?

    (ps, i’m just thinking out loud code wise because i’m at work, so i haven’t had a chance to try it. And you are absolutely right, i love trying to figure these things out! i need to learn the significance of each line, to understand why it is doing what it is doing! and how to apply that knowledge to other situations in the future!)

    Thanks for all your help so far!

    Steve

    Thread Starter stevebar84

    (@stevebar84)

    just released what you said was that i would still need the thedate(), but i need to call it from the date_shortcode()!

    so, something like:

    function thedate($atts){
    
    return get_the_date('F jS,', a$'id');
    }

    for thedate() function?

    and then called within the date_shortcode() like this?

    add_shortcode('date', 'date_shortcode');
    
    function date_shortcode( $atts ) {
    
                              thedate();
                              $a = shortcode_atts( array(
                              'id' => get_the_ID(),
                               ), $atts );
    
           return "id = {$a['id']}";
    }

    so then my return for the date_shortcode function would be "id = {$a['id']}";?

    Have i called thedate() in a correct way? i’ve basically just slapped it in the function, and i suspect thats not the way to do it correctly! or would the return actually be something like

    return thedate($a'id');

    what do you think?

    Thread Starter stevebar84

    (@stevebar84)

    OK, i’m soo close:

    add_shortcode('date', 'date_shortcode');
    
    function date_shortcode( $atts ) {
    
                              $a = shortcode_atts( array(
                              'id' => get_the_ID(),
                               ), $atts );
    
           return "id = {$a['id']}";
    }

    returns the id set in the shortcode in the form of eg, id=’243′

    and:

    add_shortcode('date', 'date_shortcode');
    
    function date_shortcode( $atts ) {
    
                              $a = shortcode_atts( array(
                              'id' => get_the_ID(),
                               ), $atts );
    
           return get_the_date('F jS,');
    }

    returns the date.

    so, how do i get my post ID to appear in the return: return get_the_date('F jS,', <ID should be here> );

    or is that completely wrong. i was basing this thought on my second post in this thread.

    Thread Starter stevebar84

    (@stevebar84)

    I’VE DONE IT!!!

    add_shortcode('date', 'date_shortcode');
    
    function date_shortcode( $atts ) {
    
                              $a = shortcode_atts( array(
                              'id' => $post,
                               ), $atts );
    
           return get_the_date('F jS,', $a['id']);
    }

    I’ve spent 4 days of my life trying to work this out!

    and for anyone else that may need this in the future, you can substitute get_the_date for get_the_time or get_the_author too.

    Thanks for your help bcworkz, couldn’t have done it without your coaching earlier today!

    and now i’m going to get some well earned sleep!

    Moderator bcworkz

    (@bcworkz)

    Wow! Quite the process you went through. Well done. I knew you had it in you! Much time spent, but next time you’ll have some clues about what not to do — you just might make up for some of the time spent on this ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘get_the_date function for specific post ID’ is closed to new replies.