[Resolved] shortcode for post comments and category

Home Forums Support [Resolved] shortcode for post comments and category

Home Forums Support shortcode for post comments and category

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1660611
    Mark

    Hi,

    First I would like to say what an amazing theme I am having a great time building my site with it.

    So for my issue:

    I have created 2 shortcodes one for the post comments and one for the post category.

    You can find them here in the left sidebar:
    https://www.markhendriksen.com/divi-business-free-layout-pack/

    However, if you click on the comment count it just refreshes the post.
    I would like that it will scroll to the comment section of the post.

    Under the comment count, you find the category, I would like this to be a link to the category page.

    The code snippets that I used for these shortcodes are these:

    
    // SHORTCODE FOR CATEGORY NAME
    
    function category_name_shortcode(){
        global $post;
        $post_id = $post->ID;
        $catName = "";
        foreach((get_the_category($post_id)) as $category){
            $catName .= $category->name . " ";
        }
        return $catName;
    }
    add_shortcode('post_category','category_name_shortcode');
    
    // SHORTCODE FOR COMMENT COUNT
    
    function comments_shortcode($atts) {
        extract( shortcode_atts( array(
            'id' => ''
        ), $atts ) );
     
        $num = 0;
        $post_id = $id;
        $queried_post = get_post($post_id);
        $cc = $queried_post->comment_count;
            if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments';
            else : $cc = $cc.' Comment';
            endif;
        $permalink = get_permalink($post_id);
     
        return '<a href="'. $permalink . '" class="comments_link">' . $cc . '</a>';
     
    }
    add_shortcode('comments', 'comments_shortcode');
    

    If this is outside the scope of support then I understand and will try to find a different solution.

    Thanks in advance.

    Regards,

    Mark

    #1661173
    Elvin
    Staff
    Customer Support

    Hi there,

    However, if you click on the comment count it just refreshes the post.
    I would like that it will scroll to the comment section of the post.

    That happens because your linking it to the page’s permalink rather than the comment section.

    Try changing the href value line on your shortcode.

    Instead of having this:
    <a href="'. $permalink . '"

    try changing it to this <a href="'. $permalink . '/#comments" so it links to the comments area.

    I’m not sure if $permalink already adds in the extra / on the end of the link. If it does, change /#comments to just #comments.

    As for the category links, you can modify the href and use this function for it.
    https://developer.wordpress.org/reference/functions/get_category_link/

    Example usage: https://wordpress.stackexchange.com/a/149023

    A wise man once said:
    "Have you cleared your cache?"

    #1664649
    Mark

    Thank you so much both issues are solved.

    #1664734
    Den

    hey,

    This may off topic but the code that Mark has given above looks simple to use

    // SHORTCODE FOR CATEGORY NAME
    
    function category_name_shortcode(){
        global $post;
        $post_id = $post->ID;
        $catName = "";
        foreach((get_the_category($post_id)) as $category){
            $catName .= $category->name . " ";
        }
        return $catName;
    }
    add_shortcode('post_category','category_name_shortcode');

    (as I’m a non-techy) Do I have to place this code in my function.php file and then add shortcode wherever I want to show them? (like in this case post_category)

    Is this the right way to do this?

    Thanks in advance 🙂

    #1664771
    David
    Staff
    Customer Support

    Hi there,

    the code can be added to your Child Themes functions.php – never edit the Parent Themes files as any changes will be lost when the theme updates.

    More info on adding PHP here, including the Code Snippets plugin method:

    https://docs.generatepress.com/article/adding-php/

    And yes the shortcode would then be added like so: [post_category]

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.