- This topic has 4 replies, 4 voices, and was last updated 3 years, 2 months ago by David.
-
AuthorPosts
-
February 16, 2021 at 8:25 am #1660611Mark
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
February 16, 2021 at 7:42 pm #1661173ElvinStaffCustomer SupportHi 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
February 19, 2021 at 7:25 am #1664649MarkThank you so much both issues are solved.
February 19, 2021 at 8:37 am #1664734Denhey,
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 🙂
February 19, 2021 at 9:10 am #1664771DavidStaffCustomer SupportHi 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]
-
AuthorPosts
- You must be logged in to reply to this topic.