最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Pass post ID from archive template to functions file

programmeradmin0浏览0评论

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?

Share Improve this question asked May 10, 2019 at 14:55 user13286user13286 2272 gold badges12 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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');
发布评论

评论列表(0)

  1. 暂无评论