- This topic has 7 replies, 3 voices, and was last updated 3 years, 9 months ago by David.
-
AuthorPosts
-
June 24, 2020 at 1:50 pm #1340699Reza
Hi,
I would like to add Schema markups to my articles using a hook. I am not using any SEO or page building plugins, if possible I would like to keep it that way..
<script type=”application/ld+json”>
{“@context”:”http://schema.org”,
“@type”:”—–”,
mainEntityOfPage”:”—–“,
“url”:”—–“,
“headline”:”—–“,
“description”:”—–“,
“logo”:{“@type”:”ImageObject”,”url”:”—–“,”width”:xxx,”height”:xx}},
“articleSection”:”—–“,
“keywords”:[“—–“,”—–“,”—–“],
“image”:[{“@type”:”ImageObject”,”url”:”—–“,”width”:xxx,”height”:xxx},
</script>I understand that this is the script I have to insert in the header.
Could anyone please help on how to best implement this on my posts AND how to automatically fill out the blanks. I would like to use the post’s excerpt for “description”. If I am not mistaken, WordPress automatically creates an excerpt if the manual excerpt box is empty.
As far as keywords are concerned, is there a way to add them without using a plugin like ACF?
Also is there a practical way to change “@type” depending on tags? Like the default being “Article” and if the article has a tag called “News”, the value will be “NewsArticle” instead.
Thanks in advance
June 24, 2020 at 2:54 pm #1340756TomLead DeveloperLead DeveloperHi there,
You could use a Hook Element for this, but it will require a certain amount of PHP/WordPress knowledge.
To get the excerpt, you can use this function: https://developer.wordpress.org/reference/functions/get_the_excerpt/
When it comes to getting the logo URL, width and height, this should help: https://wordpress.stackexchange.com/a/356961/90661
The same applies for the featured image, but you’d replace
$logo
withget_the_ID()
to get the featured image.Keywords could be tough depending on where they’re coming from.
As for the type, you could try something like:
$type = 'Article'; if ( has_tag( 'News' ) ) { $type = 'NewsArticle'; }
June 24, 2020 at 3:36 pm #1340778RezaHi,
So it is not possible to add template tags like “headline”:”{{post_title}}“, to fill out the fields?
June 25, 2020 at 3:43 am #1341194DavidStaffCustomer SupportHi there,
Template Tags only work if they are output within a Header Element, they’re not a global thing.
June 25, 2020 at 8:40 am #1341661RezaHi,
Is this approach a good idea or should I look for a way using the Loop? Not that I yet know what to do in both cases but using hooks seems more appealing.
Any guidance would be much appreciated, I have nothing against detailed guides 😉
June 25, 2020 at 8:58 am #1341683DavidStaffCustomer SupportThis is where SEO and other plugins make stuff easier. If you’re not after a full blown SEO plugin then maybe this:
https://en-gb.wordpress.org/plugins/wp-structuring-markup/
If you want to pursue the manual route then maybe this users example will help:
June 26, 2020 at 3:24 am #1342399RezaI’ll try to wrap my head around the code and get back to you if there are any theme related questions.
Thanks for the help.
June 26, 2020 at 3:38 am #1342412DavidStaffCustomer SupportThat code looks like it should work as it is, its not theme specific.
You’re welcome -
AuthorPosts
- You must be logged in to reply to this topic.