- This topic has 13 replies, 2 voices, and was last updated 4 years ago by David.
-
AuthorPosts
-
March 2, 2020 at 6:15 am #1182158Swadhin
Hi there,
I want to add some codes in the header of specific posts. I could do this with a plugin like simple headers and footers but I want to know if there is a functionality in GeneratePress itself to add header and footer to custom pages or posts.
If it is there, can we access it in the post edit screen?
Thanks
-SwadhinMarch 2, 2020 at 7:08 am #1182382DavidStaffCustomer SupportHi there,
you would use the Hook Element in GP:
https://docs.generatepress.com/article/hooks-element-overview/
And then simplest method of being able to add a value in the post editor would be using WordPress Custom Fields, you can see an example here for adding a subtitle to the post ( same logic applies ):
March 2, 2020 at 8:45 am #1182491SwadhinHi David,
Thank you so much for helping me out. I understood the basics of how this would work but I am not knowing how to implement it.
To be more precise about my query, I want to add the FAQ schema markup to say some of my posts. I got the code generated from this page (please take a look at the page and see the instructions to add it inside the blog post). (Thank you for taking the time to see that David and sorry for having to make you do a bit of extra stuff.)
Now, please tell me what code should I place in the hook for the custom field to show up in the post editor. Also, is this feature/workaround theme independent?
Thank you,
-SwadhinMarch 2, 2020 at 10:08 am #1182572DavidStaffCustomer SupportOK for a Theme independent method add this snippet to your site:
add_action ('wp_head', 'custom_rich_snippet'); function custom_rich_snippet() { // Get custom field value $richsnippet = get_post_meta( get_the_ID(), 'your-custom-field-name', true ); // Output richsnippet if not empty if ( ! empty( $richsnippet ) ) { echo $richsnippet; } }
Now you need to create a Custom Field:
https://wordpress.org/support/article/custom-fields/In the code above change
your-custom-field-name
to match your custom field slug.March 5, 2020 at 5:36 am #1185726SwadhinHi David,
Thank you so much. But I think I am still confused about what to add as a slug? This thing is supposed to help me add FAQ codes on multiple pages of my site (ones already published and ones I will publish in the future).
What slug should I add? My blog post’s slug? Which blog post because I will use this in multiple blog posts.
-swadhin
March 5, 2020 at 5:58 am #1185747DavidStaffCustomer SupportOK
1. Edit the first post you want to add the FAQ schema to.
2. Create a New Custom Field.
https://wordpress.org/support/article/custom-fields/
2.1 Give it Name eg.FAQ Schema
2.1 Add your JSON to the Value field.Now the slug you need to add is the 2.1 Name ie.
faq-schema
in the example aboveSo the code would look like this:
add_action ('wp_head', 'custom_rich_snippet'); function custom_rich_snippet() { // Get custom field value $richsnippet = get_post_meta( get_the_ID(), 'faq-schema', true ); // Output richsnippet if not empty if ( !empty( $richsnippet ) ) { echo $richsnippet; } }
Now everytime you want to add schema to a Post select your FAQ Schema custom field and update the Value accordingly.
March 5, 2020 at 10:01 pm #1186619SwadhinHi David,
Can I thank you enough? Never!
I am so proud to be a member of this community. Thanks so much.
-Swadhin
March 6, 2020 at 5:33 am #1186865DavidStaffCustomer SupportAwesome – always happy to help đ
March 11, 2020 at 7:14 am #1191972SwadhinHi David,
There’s an issue here. I tested the pages and see that the structured data testing tool doesn’t show any FAQ markup even though I have added it using the above method.
To recap:
I added this snippet in the snippet plugin:
add_action ('wp_head', 'custom_rich_snippet'); function custom_rich_snippet() { // Get custom field value $richsnippet = get_post_meta( get_the_ID(), 'faq-schema', true ); // Output richsnippet if not empty if ( ! empty( $richsnippet ) ) { echo $richsnippet; } }
I added a new custom field in the post editor (of this post) called Faq Schema and added the following code:
<script type="application/ld+json">{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How much can I earn from Infolinks?","acceptedAnswer":{"@type":"Answer","text":"There is no limit to how much you can earn from Infolinks. You can earn a decent amount with Infolinks. Having said that, donât expect earning 5 figures with it."}},{"@type":"Question","name":"Can we use Infolinks with AdSense?","acceptedAnswer":{"@type":"Answer","text":"Yes, you can absolutely use Infolinks with AdSense. But, I will recommend not using it with AdSense because it ruins the user experience plus AdSense pays more."}},{"@type":"Question","name":"Is Infolinks better than AdSense?","acceptedAnswer":{"@type":"Answer","text":"Simple answer, no. Infolinks is nowhere close to AdSense in terms of earning potential, CPC and user experience. AdSense has better ad inventory, more features like âexperiment featureâ to see what type of ads and how much of it performs well on your site."}},{"@type":"Question","name":"Is Infolinks good for Indian Traffic?","acceptedAnswer":{"@type":"Answer","text":"Yes, Infolinks is good for Indian traffic but you have better options. However, if youâve decent traffic you might also want to consider other ad networks like Ezoic or CPA programs to get better revenue."}},{"@type":"Question","name":"Is Infolinks a good website monetization platform?","acceptedAnswer":{"@type":"Answer","text":"With all honesty, Infolinks is great for beginners as an AdSense alternative. However, if you have good traffic (at least 200 unique or 300-400 page views per day) I recommend you to try other blog monetization models."}},{"@type":"Question","name":"Infolinks vs Chitika (which is better?)","acceptedAnswer":{"@type":"Answer","text":"I havenât used Chitika very recently and I donât think it would be fair to compare the Chitika I used (5-6 years ago) to the Infolinks of today. You can try and decide for yourself."}}]}</script>
Can you please help? đ
-Swadhin
March 11, 2020 at 7:57 am #1192038DavidStaffCustomer SupportMy bad – looks like an additional space here after the
!
:if ( ! empty( $richsnippet ) ) {
Try removing it ie.
if ( !empty( $richsnippet ) ) {
March 11, 2020 at 10:01 pm #1192486SwadhinFixed it. No luck đ
Doesn’t work even after this.
March 12, 2020 at 2:11 am #1192611DavidStaffCustomer SupportLets get rid of the condition – to test if it even sees the custom field – so try this code:
add_action ('wp_head', 'custom_rich_snippet'); function custom_rich_snippet() { // Get custom field value $richsnippet = get_post_meta( get_the_ID(), 'faq-schema', true ); echo $richsnippet; }
March 13, 2020 at 4:08 am #1193613SwadhinHi David,
I tried with the updated code. It didn’t work out.
Then what I did is, I used the old code, went to a different article (other than the one I was editing) and added the same schema there.
I changed the name from faq schema to faq-schema thinking let’s see what happens when it matches to that of the snippet code.
This time it worked. I am using it on different pages now and it works but I couldn’t get it to work on that one previous page, so I used another alternative there (please check here to the bottom of the article) and it works.
Not sure why the custom field did not work only on that one page.
-Swadhin
March 13, 2020 at 5:08 am #1193658DavidStaffCustomer SupportHow peculiar – sometimes WP does things ( or doesn’t do things ) without any explanation. Could be a slight glitch in the database, the slug wasn’t unique etc.
So is it all working ok now ?
-
AuthorPosts
- You must be logged in to reply to this topic.