I want to pull some post/page specific data (author name, post ID, publish time, etc) into header.php to be used in meta tags. I am having trouble figuring out the most efficient way to do this. It is my understanding that I will need to create a loop within header.php to pull this data. How can I go about creating a loop for the current page/post if I don’t yet know the ID?
3 comments
Comments are closed.
Main query is actually processed before template is loaded, so data is available in header (if a little less convenient to use).
You don’t actually need loop,
get_the_ID()
should give you ID for the queried object and most template tags have version that will return you results for that specific ID.Loop would work just as well, but it’s not very common to run it this early.
You obviously know the current page/post id. Because loading any template file, the query perform to fetch data of post, page, term or any other thing.
To add information into tag, you should be using
wp_head
hook. Below is an example –This is how I managed to obtain a meta tag for the author on
header.php
: