1
<?php
/*
Plugin Name: Members
*/

   function myFilter2($query) 
    {

        if ($query->is_category)
        {
            $currently_listing_categories = $query->query_vars['category__in'];             
            print_r($currently_listing_categories);
        }

    }

    add_filter('pre_get_posts','myFilter2');
?>

当 url 对 SEO 不友好时,此插件会显示类别 ID

http://domain.com/wplab/wpla4/?cat=4

. 但是当我打开 SEO

http://domain.com/wplab/wpla4/category/members/

数组为空

如何使用 SEO 友好的网址获取类别 ID

4

2 回答 2

1

使用此函数获取 wp 中的当前类别:

function getCurrentCatID(){

 global $wp_query;
 if(is_category() || is_single()){
  $cat_ID = get_query_var('cat');
 }
 return $cat_ID;

}

echo getCurrentCatID();

刚给你找的也试试这个,

if(isset($wp_query->get_queried_object()->cat_ID))
{
    $cur_catId = $wp_query->get_queried_object()->cat_ID;
}
if(isset($wp_query->get_queried_object()->ID))
{
    $cur_postId = $wp_query->get_queried_object()->ID;
}
于 2012-07-06T09:12:24.347 回答
0

粘贴到您的插件中functions.php或在您的插件中使用

add_filter('pre_get_posts','myFilter2');
function myFilter2()
{
    global $wp_query;
    $cat_name= $wp_query->query_vars['name'];
    $cat_id=get_cat_id($cat_name);
    echo $cat_id; // the category id will be available, echo is for testing only
}

当 url 像http://example.com/category_name

在此处输入图像描述

于 2012-07-06T09:40:45.293 回答