-
AuthorPosts
-
September 27, 2023 at 7:05 pm #1420722
Dear Support Team:
Sorry that I have to continue on this closed thread (https://kriesi.at/support/topic/blog-posts-automatically-display-author-date-and-categories/) again. I updated to the most current Enfold update, and all my shortcodes automatically displaying (see private link):
– title
– excerpt
– author
– date
– categorywere gone again. I could recover title and date as they’re quite straightforward, but I’d like to know how:
1) I have to tweak the following code for category (https://developer.wordpress.org/reference/functions/get_the_category_list/) such that it called by [sc_post_category]
2) I have to tweak the following code for author (https://developer.wordpress.org/reference/functions/get_the_author/) such that it called by [sc_post_author]
3) the respective function for excerpt such that it is called by [sc_post_excerpt]
I guess for (1) and (2) I only need to change the shortcode function name. BTW, my old (well working) enfold instance didn’t have any such functions in functions.php…
Thank you very much!
MagnusSeptember 28, 2023 at 6:00 am #1420750Hey mbosse99,
Thank you for the inquiry.
1.) Have you tried using the get_the_category_list function as suggested previously? The function may not work if you want to retrieve the categories of custom post types. You have to use the get_the_terms instead.
// https://developer.wordpress.org/reference/functions/get_the_category_list/
// https://developer.wordpress.org/reference/functions/get_the_terms/2.) You can add this code in the functions.php file to retrieve post author.
function post_author_shortcode(){ if(!is_singular()) return; $author_id = get_queried_object()->post_author; $displayname = get_the_author_meta('display_name', $author_id); return $displayname; } add_shortcode( 'sc_post_author', 'post_author_shortcode' );
3.) And for the excerpt, add this.
function post_excerpt_shortcode(){ if(!is_singular()) return; $post_id = get_queried_object()->ID; $excerpt = get_the_excerpt($post_id); return $excerpt; } add_shortcode( 'sc_post_excerpt', 'post_excerpt_shortcode' );
Best regards,
IsmaelSeptember 28, 2023 at 8:14 am #1420772Thank you Ismael, excerpt and author work nicely, the categories I am going to tackle later. Magnus
September 28, 2023 at 11:04 am #1420787Hi,
Thanks for the update. Please let us know if you should need any further help on the topic, or if we can close it.
Best regards,
RikardOctober 5, 2023 at 7:22 am #1421457For calling the functions with [sc_post_category] I would need to add the following to the end of the get_the_category_list or the get_the_terms function:
add_shortcode( 'sc_post_category', 'get_the_category_list' );
That would be important as I have my blog template set to call the function with that term.
Thanks,
MagnusOctober 6, 2023 at 10:46 am #1421606Hi,
Thank you for the update..
Have you managed to make the shortcodes work? Let us know if you need more help, or if we can close the thread.
Best regards,
IsmaelOctober 10, 2023 at 4:21 pm #1422025Sorry Ismael, I am getting the following error message for categories:
“Your PHP code changes were not applied due to an error on line 2750 of file wp-content/themes/enfold/functions-enfold.php. Please fix and try saving again.
Cannot redeclare get_the_category_list() (previously declared in wp-includes/category-template.php:141)
”Line 2750 in my functions-enfold.php corresponds to line 230 in your linked get_categories_list code.
Thanks,
MagnusOctober 10, 2023 at 5:25 pm #1422030Update: managed to display categories with this simple shortcode:
function post_category_shortcode(){ global $post; return get_the_category_list($separator = ', ', $post->ID); } add_shortcode( 'sc_post_category', 'post_category_shortcode' );
Case closed!
October 11, 2023 at 8:42 am #1422083Hi,
Great, I’m glad that you found a solution, and thanks for sharing. We’ll close this thread for now then, please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Automatically display author, excerpt and categories?’ is closed to new replies.