Site logo

Archived topic

How to add header and footer per post

13 replies · Started by Swadhin on March 2, 2020

Viewing posts 1–14 of 14

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
-Swadhin

Hi 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,
-Swadhin

OK 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.

Hi 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

OK
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 above

So 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.

Hi David,

Can I thank you enough? Never!

I am so proud to be a member of this community. Thanks so much.

-Swadhin

Awesome - always happy to help :)

Hi 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

My bad - looks like an additional space here after the !:

if ( ! empty( $richsnippet ) ) {

Try removing it ie.

if ( !empty( $richsnippet ) ) {

Fixed it. No luck :D

Doesn't work even after this.

Lets 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;
}

Hi 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

How 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 ?

This archived topic is closed to new replies.