admin管理员组文章数量:1021387
I have an archive template where I am displaying a "featured post" at the top of the page. I am building my post loop for the rest of the page in the functions.php file. What I would like to do is get the post ID of the featured post from archive.php
and then be able to access it in my functions.php
file so that I can exclude it in my post args. The reason I am using this approach is that there are multiple posts tagged with featured and I only want to exclude the one that is being displayed from the loop that's lower on the page.
I tried storing it as a session variable by placing the following code in my archive.php
file within the loop:
<?php $_SESSION['featuedID'] = get_the_ID(); ?>
And then I tried echoing it in my functions.php
file with the following code:
<?php echo $_SESSION['featuredID']; ?>
But it does not seem to work. Is it possible to pass the ID from archive.php
to functions.php
?
I have an archive template where I am displaying a "featured post" at the top of the page. I am building my post loop for the rest of the page in the functions.php file. What I would like to do is get the post ID of the featured post from archive.php
and then be able to access it in my functions.php
file so that I can exclude it in my post args. The reason I am using this approach is that there are multiple posts tagged with featured and I only want to exclude the one that is being displayed from the loop that's lower on the page.
I tried storing it as a session variable by placing the following code in my archive.php
file within the loop:
<?php $_SESSION['featuedID'] = get_the_ID(); ?>
And then I tried echoing it in my functions.php
file with the following code:
<?php echo $_SESSION['featuredID']; ?>
But it does not seem to work. Is it possible to pass the ID from archive.php
to functions.php
?
1 Answer
Reset to default 0I was able to pass the variable using update_option(). In my archive.php
I have the following code in my loop:
<?php update_option('featuredID', get_the_ID()); ?>
And then in my functions.php
file I am using the following code to get that ID:
$featuredPostID = get_option('featuredID');
I have an archive template where I am displaying a "featured post" at the top of the page. I am building my post loop for the rest of the page in the functions.php file. What I would like to do is get the post ID of the featured post from archive.php
and then be able to access it in my functions.php
file so that I can exclude it in my post args. The reason I am using this approach is that there are multiple posts tagged with featured and I only want to exclude the one that is being displayed from the loop that's lower on the page.
I tried storing it as a session variable by placing the following code in my archive.php
file within the loop:
<?php $_SESSION['featuedID'] = get_the_ID(); ?>
And then I tried echoing it in my functions.php
file with the following code:
<?php echo $_SESSION['featuredID']; ?>
But it does not seem to work. Is it possible to pass the ID from archive.php
to functions.php
?
I have an archive template where I am displaying a "featured post" at the top of the page. I am building my post loop for the rest of the page in the functions.php file. What I would like to do is get the post ID of the featured post from archive.php
and then be able to access it in my functions.php
file so that I can exclude it in my post args. The reason I am using this approach is that there are multiple posts tagged with featured and I only want to exclude the one that is being displayed from the loop that's lower on the page.
I tried storing it as a session variable by placing the following code in my archive.php
file within the loop:
<?php $_SESSION['featuedID'] = get_the_ID(); ?>
And then I tried echoing it in my functions.php
file with the following code:
<?php echo $_SESSION['featuredID']; ?>
But it does not seem to work. Is it possible to pass the ID from archive.php
to functions.php
?
1 Answer
Reset to default 0I was able to pass the variable using update_option(). In my archive.php
I have the following code in my loop:
<?php update_option('featuredID', get_the_ID()); ?>
And then in my functions.php
file I am using the following code to get that ID:
$featuredPostID = get_option('featuredID');
本文标签: loopPass post ID from archive template to functions file
版权声明:本文标题:loop - Pass post ID from archive template to functions file 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745505546a2153587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论