How to add meta description to a search result page?
-
Hi. I was wondering if anyone has already figured it out to save me the headache.
When the search result page displays posts, it adds the post’s excerpt. But for pages, it adds whatever the first words are on the page, which is far from ideal.
I tracked it down that in OceanWP this comes from partials/search/content.php
<div class="search-entry-summary clr"<?php oceanwp_schema_markup( 'entry_content' ); ?>> <p> <?php // Display excerpt. if ( has_excerpt( $post->ID ) ) { the_excerpt(); } else { // Display custom excerpt. echo wp_kses_post( wp_trim_words( strip_shortcodes( $post->post_content ), $length ) ); } ?> </p> </div>
So, in English,
if the post has an excerpt, then show it; else, then show 30 words of the full post.
This works for posts, but doesn’t work for pages since they don’t have excerpts.
I use AIOSEO plugin which allows me to specify meta description which comes up in search engine results. I fill those out for my website’s pages. So I have the page’s equivalent of an excerpt available.
In the html, this is stored in the <head> as <meta name=”description” content=”…
So, in theory, there should be a way to modify the code in partials/search/content.php (in my child theme) with the following logic:
if the post has an excerpt, then show it; elseif the post has a meta description, then show it; else, then show 30 words of the full post.
I’m not a php whizz but I can figure it out, eventually. But if someone has already done that, would you share?
An alternative that I can think of is if I could use custom fields? Does OceanWP have custom fields that would populate the excerpt property? That would be some extra work to manually copy the meta description to the custom field but then I wouldn’t need to modify the php.
The page I need help with: [log in to see the link]
- The topic ‘How to add meta description to a search result page?’ is closed to new replies.