douyou2234 2015-07-25 17:04
浏览 39

如何在wordpress中从php获取url的值?

I want to get the value of url from php in wordpress. url: https://www.domain.com/category/cricket/

I want to get cricket value from url i tried by

<?php $value=$_GET['cat']; ?>

Above code worked fine when i set url to defult (https://www.domain.com/?cat=17) in wordpress but didn't worked when it is set to "pretty permalinks" (https://www.domain.com/category/cricket/).

How can i get the value of url in wordpress from php when it set to "pretty permalinks" (https://www.domain.com/category/cricket/)

Thank you in advance. :)

  • 写回答

2条回答 默认 最新

  • douhongxie5436 2015-07-25 17:09
    关注

    You can use get_query_var() to get the category ID, and then get_category() to get the category object:

    if ( is_category() ) {
        $category = get_category( get_query_var('cat'), false );
        echo $category->slug;
    }
    

    Alternatively, you can use get_queried_object() to get the current category:

    $category = $wp_query->get_queried_object();
    echo $category->slug;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题