Igor Simic
6 years ago

Wordpress - how to get header content for specific post


To get a Wordpress generated header for specific post, first you have to use get_post() function to get that post and generate header for it as well and then use another function to grab header content.

So, to get the specific post we will use this:
// inside of your functions.php
global $post;
$post = get_post ($object['id']);

now we will create function to grab generated header:
function myPrefix_head_content() {
            ob_start();
            do_action('wp_head');
            return ob_get_clean();
        }
and now we will finally get the full generated header content for this post:
// inside of your functions.php
global $post;
$post = get_post ($object['id']);

$header_for_this_post = myPrefix_head_content();