- This topic has 14 replies, 3 voices, and was last updated 6 years, 9 months ago by Tom.
-
AuthorPosts
-
May 16, 2017 at 5:50 pm #318958Mark
Hi,
I need to be able to add JSON-LD schema into the header (I assume that is where it should go) of certain pages and posts. The schema would be different for each of the pages/posts in question so I need to be able to update individually. Is there a built in way to do this in GP?
May 16, 2017 at 6:53 pm #318975MarkA bit more information. Here is what I found on the net in terms of doing this:
1. Create a custom field named “schema”
2. Add code such as this to the custom field:
<script type=”application/ld+json”>
{
“@context”: “http://schema.org”,
“@type”: “LocalBusiness”,
“address”: {
“@type”: “PostalAddress”,
“addressLocality”: “Example Town”,
“addressRegion”: “Essex”,
“streetAddress”: “123 Example Street”
},
“description”: “A super shop that sells everything at super low prices.”,
“name”: “Super Shop”,
“telephone”: “01234567890”
}
</script>3. In the header.php file just before </head> add:
<?php
$schema = get_post_meta(get_the_ID(), ‘schema’, true);
if(!empty($schema)) {
echo $schema;
}
?>Is this the way I should do it in GP or do you have a built in way to accomplish similar things?
Thanks!
May 16, 2017 at 11:59 pm #319039TomLead DeveloperLead DeveloperInstead of placing that in the header.php file, I would put it into the wp_head hook in GP Hooks.
Other than that it looks great 🙂
July 1, 2017 at 3:26 am #342383SergioHi!
One question. How and where do you create that custom field where you have to write the JSON-LD code?
July 1, 2017 at 6:14 am #342423TomLead DeveloperLead DeveloperIf you click “Screen Options” at the top right of your editing page, you’ll see a checkbox that says “Custom Fields” which reveals a meta box for you to add custom fields.
July 1, 2017 at 11:46 am #342548Sergiothanks!
July 2, 2017 at 12:00 am #342737TomLead DeveloperLead DeveloperNo problem 🙂
July 2, 2017 at 3:39 am #342784SergioHi again…
I’m trying to do this section thing.
I added the php script to wp_head and the JSON-LD script with the structured data to she schema section of the page (not post) and it’t not working. I’ve used Google Structured Data Testing Tool and those structured data are not showing, only WPHeader and SiteNavigationElement, data that I supposed are automatically added by WordPress. I have tried with the Execute PHP checked and unchecked.
I’d be very grateful if you could provide some insight in this matter.
S.
July 2, 2017 at 3:46 am #342785SergioAnd I’d like to add some question into this issue.
Ideally, I’d like to have some common (for every page and post) structured data, and then some specific data por each page.
Where should I type de JSON-LD script for the common data?
July 2, 2017 at 8:45 am #342866TomLead DeveloperLead DeveloperGeneratePress adds structured data to all of the built in elements for you.
What kind of data are you specifically trying to add?
July 2, 2017 at 9:17 am #342891SergioI’m trying to add specific information about each service offered, not the classical address and rating snippet thing.
Those data shows when I type the JSON-LD script inside the wp_head hook. The thing is that if I do this this way, it’s shown in every page. I’d prefer to have specific data for each page.
I wouldn’t mind typing the common structured data in every page, but I think it’s more elegant and easier in the long term if I split those data in common information for the whole site (like contact information) and specific information for each page.
July 2, 2017 at 11:13 am #342970SergioCould it be that the following is not for pages but only for posts? I have no idea about his, please forgive me. I’m just used to copy & paste and trial & error.
<?php
$schema = get_post_meta(get_the_ID(), ‘schema’, true);
if(!empty($schema)) {
echo $schema;
}
?>July 2, 2017 at 9:17 pm #343162TomLead DeveloperLead DeveloperJust just grabbing whatever value you add for the “schema” name and outputting it. It won’t do anything unless it’s following schema guidelines inside your specific element.
It will work no matter where you place it as long as that value exists.
July 3, 2017 at 12:26 am #343248Sergiohmmmmm well, then I don’t know what’s going wrong.
I have the section, named “schema”, my JSON-LD script is in that section, and the script itself is working (it follows the guidelines and its actually passing the Google Structured Data Testing Tool on its own and, when I type that code in the wp_head hook it’s also outputted and the URL passes the testing tool), but it seems it’s not working when I do the section thing.
July 3, 2017 at 11:44 am #343547TomLead DeveloperLead DeveloperOk, so in the custom fields meta box, you have:
The name: schema
The value: Your codeThen you’re adding this into the wp_head hook?:
$schema = get_post_meta(get_the_ID(), 'schema', true); if ( !empty( $schema ) ) { echo $schema; }
Execute PHP is checked?
-
AuthorPosts
- You must be logged in to reply to this topic.