How to retrieve the ID of the page the user is currently viewing in Wordpress sites?

I am developing a Wordpress plugin in which I am displaying the Archive based on Categories. Categories are my pages in my site and each Page should display post in the Archive belongs to that Category only. So i need to dynamically get the Category ID of the page the user is currently viewing.How can I retrieve it?

Solutions

Remember that a post can belong to more than one category. This code might work for you:

if(is_category()){
    $cat_id = get_query_var('cat');
} else if (is_single()) {
    $cat_id = '';
    foreach (get_the_category() as $catt) {
        $cat_id .= $catt->cat_ID.' '; 
    }
    $cat_id = str_replace(" ", ",", trim($cat_id));
}
if (!intval($cat_id)) $cat_id='';

$query = "&category=$cat_id";
$posts = get_posts($query);
$postlist = ''; 
foreach ($posts as $post) {
   // something for each post
}

You're using the word page a little confusingly there - are you talking about WordPress pages that you have created in the admin, or the webpage displayed for each category archive?

If the latter, you can use the global $wp_query to get the category ID like so;

$cat_ID = $wp_query->get_queried_object_id();
Tags: Wordpress / Plugins

Similar questions

Conditional based on the User Role of the Current Profile the user is viewing - BuddyPress
I'm using BuddyPress and need a certain block of code to show up on user role Store Owner profiles only. I'll be putting it in member_header.php, so it's just a template file. I've been digging for a long time now and just can't seem to figure this out. I found a way to target current user role, but that makes it show up on everyone's profile for a...
Viewing all user metadata in user-edit.php or how to override
I have added some metadata into the account page of my users. I want this data to be shown and editable in the user-edit.php page which shows user information. I thought of modifying this file, but I later realized that if there is a WordPress update, this file would be overwritten. How should I do this?
WordPress, retrieving the ID of the user profile page currently being edited
I'm in the process of developing a WordPress plugin. When editing a user's profile page, I need to be able to retrieve the ID of that user (not the currently logged in user). I'm using the Advanced Custom fields plugin, and using a load filter for the function in question. The line $current_user = (int) $_GET['user_id']; is returning null. Is there...
How to get the currently logged in user's role in wordpress?
How to get the currently logged in user's role in wordpress?
A way to display Google Analytics data for the currently logged-in Wordpress user's website?
Long story short, I run a web development company, and when my clients log into OUR Wordpress site, I'd like to be able to provide some some basic, high-level analytics of THEIR website (the site we built for them and set up analytics for). We cater primarily toward small businesses and the GA platform would completely overwhelm them. We already ma...
How can I access the currently logged in Wordpress user from a separate PHP file?
I have a simple separate PHP page hosted in the same directory as my Wordpress blog. This page is standalone so it knows nothing of Wordpress, but will of course receive the same cookies since it's on the same host. The question is simple: How do I access the logged in Wordpress user's ID from this standalone page? I know you can access logged in u...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.