Wordpress getting current category ID - wordpress

I'm trying to get the current ID of the category page I'm viewing.
I had checked the_category_ID
But this echo'd my result when I used
<?php $catID = the_category_ID(); ?>
Is there a way to get it to return the value to the variable so its hidden?

The current category ID is in the global $cat variable, when you are in a category page.
You can test it with:
<?php echo "Current Category ID is: " . $cat ;?>
when you are in for example this page http://example.com/category/test

Try the following
$catID = get_query_var( 'cat' );

$category= get_queried_object();
echo $category->term_id;

the_category_ID was deprecated in 2003.
Try this:
if (is_category()) {
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
}

Function the_category_ID is deprecated. You need to use get_the_category() function instead. E.g.:
$category = get_the_category();
echo $category[0]->cat_name;
See more at wordpress codex: get_the_category

This code current to get Category ID:
<?php
$category = get_the_category();
echo $category[0]->cat_ID;
?>
It's work for me, today 18 Oct 2016.

This writes the variable instead of echoing
<?php $catID = the_category_ID($echo=false);?>

You will get current category id in variable,
<?php $catID = the_category_ID($echo);?>
This not print directly, whenever give print statment that time only print.

Related

Show actual thumbnail of the category page with the ID with wordpress

i need some help on wordpress
I have to show the thumbnail of my category product of my current category page
So for that i use :
[product_categories ids='116']
It's display the cat id 116.
But i'm blocked for the next thing. Get the id of the current page instead of writing it manually.
Thanks for the help
You can get the category ID using the below code
$category = get_queried_object();
$cat_id = $category->term_id;
echo $cat_id;
You can use this ID and do the rest of things.
So with the help of full stop i write this to solve my problem
function wpc_elementor_shortcode2( $atts ) {
$category = get_queried_object();
$cat_id = $category->term_id;
echo do_shortcode('[product_categories ids="$cat_id"][/product_categories]');
}
add_shortcode( 'my_elementor_php_output2', 'wpc_elementor_shortcode2');

How an i get product category name from it's id in woocommerce?

I want to create a shortcode for my wp+woocommerce site, that will show the name of the current products category. I get the category id from my url with get request - this is a specialty. It will look something like:
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
// here must be a string to get the $cat_name from $cat_id;
echo $cat_name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
What can be the solution?
Use get_term_by().
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
$product_cat = get_term_by( 'id', $cat_id, 'product_cat' );
echo $product_cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
USEFUL LINKS
get_term_by()
Since you're on the product category page, then you could use get_queried_object, and then from that object you could get the name like so:
$cat = get_queried_object();
echo $cat->name;
//or if you want to get its id
echo $cat->term_id;
//if you want to see the object and what's in it then you could use print_r()
print_r($cat);
Let me know if that's what you're looking for.
So your code would be something like this:
function this_product_category_name()
{
$cat = get_queried_object();
echo $cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );

Get an array of category details with category name in WordPress

I have an URL like this https://example.com/parent-category/child-category/
With the function single_cat_title()
$category = single_cat_title('', false); I am getting the name of the active category. Now, how can I get the details of the category with the name I am getting in the $category variable?
<?php $queried_object = get_queried_object();
print_r( $queried_object ); ?>

Wordpress Primary Category

I recently needed to change my permalink structure in Wordpress. However the change caused all my social sharing counters to reset. I found this great script which I modified below, http://encosia.com/preserving-social-sharing-counters-through-a-url-change/. I rewrote it so it would remove the %poste-id% at the end of the url on older posts.
The problem is that for the most part this works, however I need it to call the permalink category not the first category in the get_the_category(); array. Is there anyway I can pull this info, or modify this script to work that way? This is also being used in the loop. Thanks!!
<?php
$url_change_id = 68135;
$postid = $post->ID;
$category = get_the_category();
$slug = $post->post_name;
$permalink_url = get_permalink();
if (intval($postid) < $url_change_id) {
$url_date_prefix = "/" . $category[0]->category_nicename .
"/" . $slug .
".html";
$sharing_url = str_replace($permalink_url,
"http://website.com" . $url_date_prefix,
$permalink_url);
} else {
$sharing_url = get_permalink();
}
?>
<?php echo $sharing_url; ?>

How to get the category title in a post in Wordpress?

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?
Use get_the_category() like this:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
It returns a list because a post can have more than one category.
The documentation also explains how to do this from outside the loop.
You can use
<?php the_category(', '); ?>
which would output them in a comma separated list.
You can also do the same for tags as well:
<?php the_tags('<em>:</em>', ', ', ''); ?>
To find the category id, category link and category name in wordpress using php, you can use below code:
$cat_id = get_cat_ID('Category Name');
$category_link = get_category_link($cat_id);
$category_name = get_cat_name($cat_id);
echo $category_name; // It will print your category name

Resources