How do I add meta tags from a template?

I’m developing a website using WordPress as my Framework. The templates are highly customized and really doesn’t use much of WordPress’s features. I just use it for registering users and publish a few articles here and there.

One of the templates is a product template. In this template I retrieve product information based on product ID sent in the URL.

Read More

What I want to do, is to take info from this product and add it to my <meta> tags.

According to WordPress documentation, headers are loaded before templates.

So, how can I add meta tag information based on data loaded in my template?

Do I have to query the database “a second time” in the header in order to retrieve the data wanted?

I know WordPress has some functionality for this, since article titles can be retrieved in the header file. But I’m not sure how I can take advantage of this.

Related posts

Leave a Reply

1 comment

  1. Even though header is loaded before template, few global variable are available even before header is loaded and $post is one of them which hold post data + metadata.

    So you can access post meta in header as well.

    global $post;
    echo get_post_meta($post->ID, 'ur_meta_key', true);