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');