• Resolved Maria C.

    (@cyberkitto)


    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.

    • This topic was modified 9 months, 3 weeks ago by Maria C..

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @cyberkitto,

    Thank you for reaching out,

    In this case, you need some customizations.
    Before it, please install the child theme. Then, by using FTP or cPanel access (if didn’t use the localhost), Go to this directory: wp-content\themes\oceanwp\partials
    Find the template that you want to customize and copy that template file.

    Then head over to the same path on the child theme. There isn’t an entry folder as a default on your child theme, so you need to create a new one(one time).

    Paste that template there.
    Then you can edit the template on the new path in the child theme: wp-content\themes\oceanwp-child-theme\partials

    In that file, you can change the code to something like this:

    
    <div class="search-entry-summary clr"<?php oceanwp_schema_markup( 'entry_content' ); ?>>
    	<p>
    		<?php
    		// Get the post meta description
    		$meta_description = get_post_meta( $post->ID, '_aioseo_description', true );
    		
    		// Display excerpt.
    		if ( has_excerpt( $post->ID ) ) {
    			the_excerpt();
    		} elseif ( ! empty( $meta_description ) ) {
    			echo wp_kses_post( $meta_description );
    		} else {
    			// Display custom excerpt.
    			echo wp_kses_post( wp_trim_words( strip_shortcodes( $post->post_content ), 30 ) );
    		}
    		?>
    	</p>
    </div>
    
    

    Screenshot: https://postimg.cc/7JkDvVyP.
    I’ve tested and it is working well on my end with AIOSEO plugin.

    You can modify this code to include both expert and meta descriptions, or anything else you want, or keep it as it is.

    Furthermore, if you prefer using custom fields, you can use the Advanced Custom Fields (ACF) plugin to create a custom field for the excerpt. Then, you can update the code to check for this custom field before falling back to the post content. However, the provided method using the meta description is more straightforward and leverages the existing data you already input for SEO purposes.

    * Please check this screenshot: https://i.postimg.cc/sxjYTWyt/image.png.
    * Download child theme: https://docs.oceanwp.org/article/90-sample-child-theme.
    * Developer docs: https://docs.oceanwp.org/collection/19-developer-doc.

    Note: It’s working with the /woocommerce/ folder or root of the theme files.

    I hope it helps.
    Best Regards

    Thread Starter Maria C.

    (@cyberkitto)

    This worked perfectly. Thank you for saving me the hassle.

    You’re most welcome.
    I’m glad it is resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add meta description to a search result page?’ is closed to new replies.