0

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. :)

0

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

This is what you need to do:

<?php
$cat = get_category( get_query_var( 'cat' ) );
$cat_id = $cat->cat_ID; //the category id
$cat_name = $cat->name; //the category name
$cat_slug = $cat->slug; //the category slug
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.