Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #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
    – category

    were 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!
    Magnus

    • This topic was modified 1 year, 2 months ago by mbosse99.
    • This topic was modified 1 year, 2 months ago by mbosse99. Reason: formatting
    #1420750

    Hey 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,
    Ismael

    #1420772

    Thank you Ismael, excerpt and author work nicely, the categories I am going to tackle later. Magnus

    #1420787

    Hi,

    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,
    Rikard

    #1421457

    For 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,
    Magnus

    #1421606

    Hi,

    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,
    Ismael

    #1422025

    Sorry 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,
    Magnus

    #1422030

    Update: 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!

    #1422083

    Hi,

    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

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Automatically display author, excerpt and categories?’ is closed to new replies.