• Resolved kdlunneborg

    (@kdlunneborg)


    Hey y’all.

    I’ve got a site (private) where I have several not necessarily tech-savy people posting. The Excerpts aren’t normally seen, but I’d love to get them more visible because people usually put somewhat important things in there. Is there a way that I can have the post excerpt automatically show up on the post page, right above it?

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    Sure, that’s definitely possible. But how you go about it depends a lot on your theme. Which one are you using?

    Thread Starter kdlunneborg

    (@kdlunneborg)

    Stone blog.

    sakshamsharma5

    (@sakshamsharma5)

    Hey there! One way to do that would be by using a WordPress filter to prepend the excerpt to the post content.

    Here’s the code:

    /**
    * Prepends the manual excerpt to the post content.
    *
    * @param string $content The default post content.
    * @return string Modified content with the excerpt prepended.
    */
    function stone_blog_show_excerpt_above_content( $content ) {
    // return if not on a single post, not in the main loop, or not the main query.
    if ( false === is_singular( 'post' ) || false === in_the_loop() || false === is_main_query() ) {
    return $content;
    }

    // return if the post does not have a custom manual excerpt.
    if ( false === has_excerpt() ) {
    return $content;
    }

    // Retrieve the excerpt.
    $excerpt = get_the_excerpt();

    // Build the HTML.
    $excerpt_html = '<div class="post-excerpt-highlight">';
    $excerpt_html .= wp_kses_post( $excerpt );
    $excerpt_html .= '</div>';

    // Return the newly wrapped excerpt followed by the original content.
    return $excerpt_html . $content;
    }
    add_filter( 'the_content', 'stone_blog_show_excerpt_above_content' );

    You’ll need to add this code to the functions.php file of your child theme. Alternatively, if you aren’t using a child theme, another way is to use a free code snippet plugin like WPCode or Code Snippets to insert it safely into your project.

    Optionally, you can style the excerpt using CSS via Appearance → Customize → Additional CSS.

    Thread Starter kdlunneborg

    (@kdlunneborg)

    Thanks @sakshamsharma5. That works great. Is there any good way I could add a horizontal line, or some sort of separator between the excerpt and the post?

    sakshamsharma5

    (@sakshamsharma5)

    Glad that helped! Yes we can do that. Just head over to Appearance → Customize → Additional CSS and drop in one of these snippets:

    Option 1: A simple horizontal line

    .post-excerpt-highlight {
    border-bottom: 2px solid #eaeaea; /* Adjust the thickness and color here */
    padding-bottom: 1.25rem; /* Space between the text and the line */
    margin-bottom: 1.75rem; /* Space between the line and the post content */
    }

    Option 2: A highlighted callout box

    .post-excerpt-highlight {
    background-color: #f7f7f7;
    border-left: 4px solid #ff4c60; /* Change color to match your theme */
    padding: 1rem; /* Inner spacing around the text */
    margin-bottom: 1.5rem; /* Space between the box and the post content */
    }
    Thread Starter kdlunneborg

    (@kdlunneborg)

    @sakshamsharma5 Works great! Thanks for your help.

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.