• Hi!

    I can’t get the autogenerated description to display on my blog index page. I have checked that in my query I am using the individual post’s ID, not the index page ID.

    I have a feeling it is related to me using custom fields for the text content.

    Currently I’m using the_seo_framework_description_excerpt filter for feeding the generator with textcontent from my custom text blocks. (get_textcontent() finds all the text field ACF layouts in the post and then returns just the strings).

    \add_filter(
        'the_seo_framework_description_excerpt',
        function( $excerpt ) {
    
            $excerpt = static::get_textcontent();
    
            return $excerpt;
        },
        10,
        2
    );

    Then using tsf()->description()->get_description( [ 'id' => $post_id ] ) to output the excerpt on the single blog post page template works fine, and it also outputs nicely in the meta description tags and shows up in admin correctly.

    I think the issue lies in that on the blog index page I’m not getting the textcontent from all the posts, but I guess that would be necessary for the autogeneration to be able to do it’s thing.

    So my question is 😀 – is there a good way to get autodescriptions generated from custom fields data on the blog index page?

    • This topic was modified 1 month, 1 week ago by megamatias.
Viewing 1 replies (of 1 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    I’m unsure if you want to fetch generated descriptions or set one.

    If you just want to fetch one, you should call tsf()->description()->get_generated_description() and ignore the filter altogether. You can find more methods of tsf()->description() here: https://github.com/sybrew/the-seo-framework/blob/5.0.6/inc/classes/meta/description.class.php.

    Otherwise, if you want to set a custom-generated description, you need to use the filter. The filter you use has a second argument: $args. That argument allows you to influence the description in the admin area.

    So, this would be a better starting point — though, I’m not sure what static::get_textcontent() would do, so I just fed it the post ID:

    add_filter(
    	'the_seo_framework_description_excerpt',
    	function ( $excerpt, $args ) {
    
    		$tsf       = tsf();
    		$tsf_query = $tsf->query();
    
    		if ( null === $args ) {
    			// In the loop.
    			$is_blog = $tsf_query->is_blog_as_page();
    		} else {
    			// Out the loop, admin etc.
    			if ( 'single' === The_SEO_Framework\get_query_type_from_args( $args ) ) {
    				$is_blog = $tsf_query->is_blog_as_page( $args['id'] );
    			}
    		}
    
    		if ( $is_blog ?? false ) {
    			$post     = get_post( $args['id'] ?? $tsf_query->get_the_real_id() );
    			$post_id  = $post->ID;
    
    			// Fall back to the old excerpt if the new one has nothing useful.
    			$excerpt = static::get_textcontent( $post_id ) ?: $old_excerpt;
    		}
    
    		return $excerpt;
    	},
    	10,
    	2,
    );

    At $excerpt =, you can fill in the custom-generated description. TSF has various methods to extract content from HTML, as exemplified here in the if ( $excerpt ) {-clause.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.