Hello Mehmet,
Are you building your own schema.org markup for the Article type, or are you relying on other software?
In any case, the_excerpt() prints the output, it doesn’t return a value. This makes the function quite useless in building JSON because you can’t (easily) store that. So, if you want the value, you should use get_the_excerpt().
WordPress has many of these quirky functions that take some time to wrap your head around 🙂
But, get_the_excerpt() is unaware of The SEO Framework’s output. We aren’t fiddling with that, because it’s used for theme-display.
So, if you want to use The SEO Framework’s description–which uses the excerpt as a fallback–you’d want to use this method:
$tsf = function_exists( 'the_seo_framework' ) ? the_seo_framework() : null;
$descripton = $tsf ? $tsf->get_description() : '';
You can see how I’ve done that for the Articles Extension, here: https://github.com/sybrew/The-SEO-Framework-Extension-Manager/blob/2.0.4/extensions/essentials/articles/trunk/inc/classes/front.class.php
Hello Sybre,
I’m building my own schema.org markup for the MedicalWebPage type, and I hope I’m doing it right 🙂
I use Astra theme’s custom layouts where you can add any code to hooks, where I add the markup code to all posts in the header hook.
My script starts like this:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "MedicalWebPage",
"additionalType": "WebPage",
"mainEntityOfPage": "<?php the_permalink(); ?>",
"headline": "<?php the_title(); ?>",
"description": "<?php if (have_posts()) : while (have_posts()) : the_post(); ?><?php echo the_seo_framework()->get_description(); ?><?php endwhile; endif; ?>",
"about": {
"@type": "Thing",
"name": "<?php the_title(); ?>"
},
When I tried adding the code you provided it didn’t return anything, I probably didn’t do it right as I directly pasted it where is the “echo the_seo_framework()->get_description();” code.
Then I tried another suggestion from Reddit, the one which is in the code above and it worked.
Thanks for all your help.
Hi Mehmet,
I’m glad you’ve been able to figure it out!
Please keep in mind that The SEO Framework caches the query, to speed things up. If you wish to put this in a loop, you’d be better off feeding an ID to the method, so you’ll bypass the cache.
i.e.
the_seo_framework()->get_description( [ 'id' => get_the_ID() ] );
Hi Sybre,
That worked like a charm 🙂 Thanks a million for helping out.