Support » Developing with WordPress » How to Call comments_popup_link with a Specific Post

  • Resolved dhurlburtusa

    (@dhurlburtusa)


    Many of the functions (aka template-tags) in WordPress either work on the current post when inside the WP loop or can be given a post ID or a post instance (i.e., WP_Post instance). For example, get_the_title accepts a post ID or a post. However, it seems comments_popup_link doesn’t accept a post or post ID parameter. Is this a known issue?

    How do I go about requesting that all functions that get or echo post data to accept an optional post/post ID parameter?

Viewing 5 replies - 1 through 5 (of 5 total)
  • It is known, but it’s subjective as to whether it’s an issue.
    The function works on the current post.

    If you want to suggest a change, search first for existing tickets:
    https://core.trac.wordpress.org/

    Moderator bcworkz

    (@bcworkz)

    As a workaround for using such template tags outside of the loop, you can setup what the tags consider to be the current post yourself. Let’s say the post ID you want applied is 123. Do something like this:

    global $post;
    setup_postdata( 123 );
    comments_popup_link();
    Thread Starter dhurlburtusa

    (@dhurlburtusa)

    Thanks @bcworkz.

    So, would adding something similar to the following function to a site-specific plugin or functions.php of my child theme work so that I don’t lose what the current post may be?

    
    function myprefix__the_comments_popup_link ( $post_id, ...$args ) {
        global $post;
        $curr_post_id = get_the_ID();
        setup_postdata( $post_id );
        call_user_func_array( 'comments_popup_link', $args );
        setup_postdata( $curr_post_id );
    }
    

    Then I can use myprefix__the_comments_popup_link in place of comments_popup_link.

    Moderator bcworkz

    (@bcworkz)

    Yep, should work. I’ve not tested that specific approach before though. From what I know of post globals it should work. The only way to know for sure is to try it 🙂

    Thread Starter dhurlburtusa

    (@dhurlburtusa)

    @joyously, @bcworkz Thank you both for your help. I am going to try a work-around function like the one above.

    Hopefully version 6 of WP provides more consistency for all the functions (aka template tags) in the library. I wish I had time to help contribute but at the moment I don’t.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to Call comments_popup_link with a Specific Post’ is closed to new replies.