Home › Forums › Pro Support › Excerpt as title
- This topic has 23 replies, 3 voices, and was last updated 2 years, 1 month ago by
elvin.
-
AuthorPosts
-
June 17, 2020 at 3:09 pm #16642
Bernhard
ParticipantHello,
in some of my cards I use an excerpt of the first paragraph with a defined number of words.Now I have a particular problem. I want to use in some cases cards as title h2, but not the “long” post title. It shall be a “short title” defined e.g. in the manual excerpt field (or in another custom field).
Is this possible?
Thank you
June 18, 2020 at 2:24 pm #16672Tom
KeymasterHi there,
I’m not sure I fully understand – can you explain a bit more?
June 18, 2020 at 3:22 pm #16680Bernhard
ParticipantHi Tom,
on my new designed homepage the table of content links to cards “overlay” style which show only the page/post title with h2. Sample linkThis is fine for this page.
Now I’m developing another new page, where I don’t want to have the full page or post title in cards but a manually defined title which I insert, maybe, in the excerpt field (I have it already there in pages and posts). I use the manual excerpt nowhere else.
So my idea is to pick up the text of the excerpt field and show it as title h2. Would it be possible to insert a “use excerpt as title” checkbox?
Otherwhise would it be possible to hardcode it in the way that, if the “include title” is unchecked it uses the excerpt as title h2 with a font height to be defined?
Thank you
June 19, 2020 at 3:31 pm #16737Tom
KeymasterHmm, tricky.
I suppose you could disable the title, then do this:
add_action( 'wpsp_after_content', function( $settings ) { if ( 123 === $settings['list_id'] ) { echo '<h2>'; echo get_the_excerpt(); echo '</h2?'; } } );
June 20, 2020 at 5:40 am #16757Bernhard
ParticipantHi Tom,
the result is strange, you can see it here. In the Elementor preview I see the standard H2 title size + ►►► (see screenshot above), on the page I see normal text color white with ►►►.
The arrows are coded because WPSP is not compatible with Polylang for the read more function.The full code related to WPSP is
//Disable read more tag in wpsp add_filter( 'wpsp_more_tag', '__return_empty_string' ); //Disable image title on hover in wpsp add_filter( 'wpsp_image_title', '__return_empty_string' ); // Read more function in WPSP add_filter( 'wpsp_ellipses', function() { return sprintf( '...<a href="%1$s"> ►►►</a>', get_permalink() ); } ); add_filter( 'get_the_excerpt', function( $excerpt ) { if ( has_excerpt( get_the_ID() ) ) { $excerpt .= sprintf( '...<a href="%1$s"> ►►►</a>', get_permalink() ); } return $excerpt; } ); //Use excerpt as title WPSP add_action( 'wpsp_after_content', function( $settings ) { if ( 32293 === $settings['list_id'] ) { echo '<h2>'; the_excerpt(); echo '</h2>'; } } );
June 20, 2020 at 5:59 am #16759Bernhard
ParticipantI forgot
// WPSP show excerpt field on pages add_action( 'wpsp_before_content', function( $settings ) { $meta = get_post_meta( get_the_ID(), '_your_custom_meta_key', true ); if ( isset( $meta ) && '' !== $meta && 23138 == $settings['list_id'] ) { echo $meta; } } );
June 22, 2020 at 10:08 am #16804Tom
KeymasterHmm, try the updated code: https://wpshowposts.com/support/topic/excerpt-as-title/#post-16737
June 22, 2020 at 11:51 am #16817Bernhard
ParticipantOK, how can I handle the colour and the size?
June 22, 2020 at 2:41 pm #16821Tom
KeymasterYou will likely need to use CSS like this:
.wpsp-content-wrap h2 { /* your CSS here */ }
June 23, 2020 at 8:03 am #16841Bernhard
ParticipantPerfect.
Is it possible to have more ID’s in one action or must I repeat the full code for every ID?
Can I insert the action into an Elements hook or works it only in the functions.php?June 23, 2020 at 1:15 pm #16846Bernhard
ParticipantOne mor thing: the manual excerpt shall not substitute the automatic excerpt on the blog pages and not in the normal lists. Basically, the manual excerpt shall only be shown in the lists addressed directly with the action above.
June 23, 2020 at 4:46 pm #16853Tom
KeymasterHi there,
You can use Elements if you want to hook it into every single list, just add the hook name into the Custom Hook field.
June 24, 2020 at 6:43 am #16874Bernhard
ParticipantOK. I see that the use of the excerpt field is not practical for me. How can I address a custom field as title, e.g.
wpsp_title2
June 24, 2020 at 3:07 pm #16897Tom
KeymasterYou’d do this:
add_action( 'wpsp_after_content', function( $settings ) { $custom_field = get_post_meta( get_the_ID(), 'wpsp_title2', true ); if ( $custom_field && 32293 === $settings['list_id'] ) { echo '<h2>'; echo $custom_field; echo '</h2>'; } } );
June 25, 2020 at 12:37 am #16909Bernhard
ParticipantPerfect.
A question about the use of Elements: You wrote
just add the hook name into the Custom Hook field
I don’t understand what to use as hook name.
-
AuthorPosts
- You must be logged in to reply to this topic.