wordpress:打开SEO URL时无法获取类别ID

时间:2012-07-06 09:04:37

标签: php wordpress wordpress-plugin wordpress-theming

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

当网址不是SEO友好时,此插件会显示类别ID

  

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

。但是当我打开SEO时

  

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

数组为空

如何通过SEO友好网址获取类别ID

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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
}

当网址与http://example.com/category_name

相似时

enter image description here