Archived topic
Display Excerpt on post (as added in excerpt field) and read more
7 replies · Started by JC on March 18, 2021
Hello,
I think I have found the solution for one of my problems but just want to confirm.
Issue 1 is when Excerpt field is filled in, custom excerpt, it does not display on category views so will this solve it?: https://docs.generatepress.com/article/activating-read-custom-excerpt/
Issue 2, displaying the custom excerpt (as added to excerpt field during post edit) on a post and changing it to H2. What is the best way to go about this without adding any additional plugins or altering the theme files.
I do have the paid version so can this be done via ELEMENTS if so any help/direciton would be apprediated :-)
For ISSUE 2, I have tried this:
add_action('generate_after_entry_header', 'db_single_post_excerpt');
function db_single_post_excerpt() {
if ( is_single() && has_excerpt() ) {
$single_excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
echo '<div class="single-excerpt">' . $single_excerpt . '</div>';
}
}
The only issue is the image is set to display below title (Customiser/Layout/Blog) but now I get the Excerpt first then image, I want it below the image and not before.
Hi there,
1. Yep thats correct.
2. In that code try changing the first line ie.
add_action('generate_after_entry_header', 'db_single_post_excerpt');
to:
add_action('generate_after_entry_header', 'db_single_post_excerpt', 50);
Thank you David. Just for the sake of learning what does the 50 represent? That sets it to priority 50, right?
Yes exactly :)
Hello,
So I have this setup:
add_action('generate_after_entry_header', 'db_single_post_excerpt', 50);
function db_single_post_excerpt() {
if ( is_single() && has_excerpt() ) {
$single_excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
echo '<div class="single-excerpt"><h2>' . $single_excerpt . '</h2></div>';
}
}
The problem is that the output is this;
<h2><p>excerpt text</p></h2>
I don't want the Paragraph elements in there it must only be in the H elements. What am I doing wrong or how can I do this better maybe by setting it up via ELEMENTS?
I feel so enabled... I solved it with a bit of Googling and luck so sharing.
I changed:
the_excerpt
to
get_the_excerpt
So the output is now without the paragraph elements for the excerpt. :-)
/** excerpt on post and wihtout paragraph elements **/
add_action('generate_after_entry_header', 'db_single_post_excerpt', 50);
function db_single_post_excerpt() {
if ( is_single() && has_excerpt() ) {
$single_excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post_id));
echo '<div class="single-excerpt"><h2>' . $single_excerpt . '</h2></div>';
}
}
Glad to hear you found the solution :) And thanks for sharing!!!