$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Pulling current postpage data into header.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - Pulling current postpage data into header.php

matteradmin11PV0评论

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?

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?

Share Improve this question asked Feb 24, 2014 at 17:18 Josh MountainJosh Mountain 4386 silver badges19 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

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 -

add_action('wp_head', 'wpse_wp_head');
function wpse_wp_head(){
    // first make sure this is a single post/page
    if( !is_page() || !is_single() )
        return;

    // then get the post data
    $post = get_post();

    echo '<meta name="post_id" value="'. $post->ID .'" />';

    $author = get_user_option('display_name', $post->post_author );
    echo '<meta name="author" value="'. esc_attr( $author ) .'" />';
}

This is how I managed to obtain a meta tag for the author on header.php:

  <?php
    if (is_singular()) {
      $post = get_post();
      $autor_fn = get_the_author_meta('first_name',$post->post_author);
      $autor_ln = get_the_author_meta('last_name',$post->post_author);
      if (!empty($autor_fn) && !empty($autor_ln)) {
  ?>
        <meta name="author" content="<?php echo "$autor_fn $autor_ln"; ?>">
  <?php
      }
    }
  ?>
Post a comment

comment list (0)

  1. No comments so far