Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过permalink获取帖子的类别_Php_Wordpress_Wordpress Theming - Fatal编程技术网

Php 通过permalink获取帖子的类别

Php 通过permalink获取帖子的类别,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我有一篇文章分为两类(a类,B类)。当我访问帖子时,例如“/category-b/post name”,获取页面当前类别的最佳方式是什么?在这种情况下,我希望收到“b类” 谢谢,您可以使用parse_url从url获取路径,然后使用explode将其拆分为段: $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $uri_segments = explode('/', $uri_path); echo $uri_segme

我有一篇文章分为两类(a类,B类)。当我访问帖子时,例如“/category-b/post name”,获取页面当前类别的最佳方式是什么?在这种情况下,我希望收到“b类”


谢谢,

您可以使用
parse_url
从url获取路径,然后使用
explode
将其拆分为段:

$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);

echo $uri_segments[1]; // for www.example.com/category-b/post-name you will get 'category-b'.