admin管理员组文章数量:1130349
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 badges3 Answers
Reset to default 2Main 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
}
}
?>
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 badges3 Answers
Reset to default 2Main 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
}
}
?>
本文标签: loopPulling current postpage data into headerphp
版权声明:本文标题:loop - Pulling current postpage data into header.php 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749023123a2304686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论