- This topic has 12 replies, 2 voices, and was last updated 3 years, 8 months ago by David.
-
AuthorPosts
-
May 28, 2020 at 12:54 am #1304537Michael
I hope all is well.
I have a follow-up question on the category page:
Currently I have set the Excerpt word count to 56, which on my site http://www.blojfri.se/pottrana is just fine.But for my new site http://www.sauberwerden.de/sauberwerden, I have a different set-up on the pages in that specific category. I would still like the the excerpt word count to be 56. But I would prefer if the excerpt did not start from the top of the relevant page.
If you go to one of the pages in the category you will see that I always start the page with the following text: Hilfeartikel für diejenigen, die Sauber! Hand in Hand weg von der Windel befolgen.
This means that that text is also always first in the excerpt.
Is there a way to make the excerpt start a bit in on the text?
I’m thinking either
– “hard-coding” excerpt (but that sounds difficult and tedious) or
– Somehow “hiding” the first sentence or two on page (so that the excerpt function does not see it).May 28, 2020 at 7:27 am #1304978DavidStaffCustomer SupportHi there.
try adding this PHP Snippet:
function replace_content_on_the_fly($text){ $replace = array( // 'words to find' => 'replace with this' 'This is the text to be replaced with' => '' ); $text = str_replace(array_keys($replace), $replace, $text); return $text; } add_filter('the_excerpt', 'replace_content_on_the_fly');
May 28, 2020 at 8:20 am #1305196MichaelHi David,
Nice to chat again 🙂
I assume adding that snippet means I would need to manually write the excerpt for each specific page in the category? And that I would need one snippet per page?
Isn’t there an “easier” way?
I’m thinking about when I for instance receive some emails from marketers. It’s quite often that the one-liner preview, next to the subject, in the inbox, is then not visible anywhere once I open the email the email. I believe it is called “Hidden preview text” ( see a bit down on this page: https://www.litmus.com/blog/the-ultimate-guide-to-preview-text-support/ ).
They have this solution:
Insert preview text here.So I guess I could potentially copy a part of the text I would like to be shown in the excerpt, and place that inside the div at the top of the page?
May 28, 2020 at 8:21 am #1305197MichaelThe div did not show… but it is visible on the link if you scroll down to that header
May 28, 2020 at 8:44 am #1305221DavidStaffCustomer SupportThat code i provided only needs to be added once to your site, as per this documents instructions:
https://docs.generatepress.com/article/adding-php/
Within the code you need to update this line to include the text you want to remove:
'This is the text to be replaced with' => ''
Then whenever an excerpt contains that text it will automatically remove it.
May 28, 2020 at 9:52 am #1305324MichaelOk!
I added the code, and it works 🙂However, and pardon my stupid question, but how is the code limited to only searching for that specific text in the excerpt part, on that particular category page?
It feels like I am missing to include in the code to only perform this replace function on a specific category page? Like I can do with an array, when I want to block certain pages from the search function.
Regards
MichaelMay 28, 2020 at 2:16 pm #1305603TomLead DeveloperLead DeveloperHi there,
Try this:
add_filter('the_excerpt', function( $text ) { if ( is_category( 'your-category-slug' ) ) { $replace = array( // 'words to find' => 'replace with this' 'This is the text to be replaced with' => '' ); $text = str_replace(array_keys($replace), $replace, $text); } return $text; } );
Just update the slug and you should be good to go 🙂
May 29, 2020 at 4:16 am #1306146MichaelThank Tom (and David)! You guys are simply awesome 🙂
May 29, 2020 at 9:14 am #1306641TomLead DeveloperLead DeveloperGlad we could help 🙂
July 28, 2020 at 5:32 am #1379104MichaelHi again,
I have a follow-up question on the snippet I installed previously.How do I add more words that are to be filtered out (replaced with blank)?
I’ve tried several different ways of changing the snippet, but could not make it works.
Best regards
Michael
July 28, 2020 at 6:46 am #1379183DavidStaffCustomer SupportYou can add multiple strings in the array eg.:
$replace = array( // 'words to find' => 'replace with this' 'This is the text to be replaced with' => '', 'This is the other text to be replaced with' => '' );
July 28, 2020 at 6:49 am #1379187MichaelThank you David!
July 28, 2020 at 6:51 am #1379189DavidStaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.