Site logo

Archived topic

Schema Markup with Elements

7 replies · Started by Reza on June 24, 2020

Viewing posts 1–8 of 8

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

Hi 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 with get_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';
}

Hi,

So it is not possible to add template tags like “headline”:”{{post_title}}“, to fill out the fields?

Hi there,

Template Tags only work if they are output within a Header Element, they're not a global thing.

Hi,

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 ;)

I'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.

That code looks like it should work as it is, its not theme specific.
You're welcome

This archived topic is closed to new replies.