How to get the current category id from a category template

Posted in: Newspaper
Post count: 162

Hello,

I am using PODS in order to extend the wc product categories and have images per category etc (we have about 3000+ categories so using the built in functionality to change templates per category is not an option).

Right now it works only if I explicitly state the ID of the category e.g.

https://carro.gr/product-category/bmw/1600-2002/1600-2002-66-77/

The image in the top left corner is via this code
[pods name=”product_cat” ID=”967″][/pods]

However, what it is needed is to get the category ID from the template either transparently or with another shortcode.

How can this be done?

Post count: 162

To clarify:

In my functions.php if add this code:

add_shortcode( 'category_pods', 'pods_category_shortcode' );
function pods_category_shortcode() {

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

}

And add the shortcode in the theme, I do not get the current category id… (so I cannot then create the shortcode for the pods that will contain the current category id.

So the question is what do I have to do to get the current category id, of a category page in the theme.

Post count: 35449

Hi,
Maybe you can try the suggestions from here https://stackoverflow.com/questions/8829632/get-current-category-id-of-the-active-page
to get the current category id.
I hope this will help!

Post count: 162

Sadly it didn’t.

Because as we have discussed time and time again here, the cloud templates override the get_queried_object and related calls. So it returns absolutely nothing.

I will be glad if you can provide me a function that can be placed in functions.php that you have tested in Newspaper theme that echo’s the current category id if placed in a page like this:

https://carro.gr/product-category/bmw/1600-2002/1600-2002-66-77/

It should be extremely easy for you.

  • This reply was modified 4 years by dimitrisv.
Post count: 35449

I understand what you mean, unfortunately we do not make functions, those are made by developers and maybe those are easy for them, but not for us, the support team do not have skills to create functions, we can help only with existed options and research from google.
I asked a developer and he provide this code -> https://pastebin.com/FKjZKZJg
Thank you for your understanding!

Post count: 162

Thank you Calin!

I just did and I am looking forward to your offer.

Post count: 35449

I asked a developer and he provide this code -> https://pastebin.com/FKjZKZJg

Post count: 162

Thank you very much indeed! I will try it and will let you know.

It was impossible for me (or any other who will not try to reverse engineer your theme) to come up with this.

Post count: 162

Sadly it is not working for neither product categories nor post categories.

Do I have to add something else?

Post count: 35449

I’m sorry, but I don’t know if you have to add anything or not, that’s how I got the code with this screenshot -> https://i.imgur.com/SK8axeY.png from what I see there it applies by category.
Thank you!

Post count: 162

I did manage to make it work on Categories for post items.

But it doesn’t work in Woo Product Categories.

It gave me the idea to try to get the relevant code from the theme (wp-content\plugins\td-cloud-library\includes\tdb_functions.php
wp-content\plugins\td-woo\state\td_woo_state_loader.php
) but I did not know what to pick…

Tried various combinations but with no luck so far.

Post count: 162

https://carro.gr/blog/category/blog/
You can see it in my setup too…
🙂

But please ask him to provide something that will work for product categories.

Thank you!

Post count: 162

I think the code to provide the current product category id is this one:

if ( isset( tdb_state_content::get_wp_query()->queried_object ) ) {
$tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object->term_id;
} else {
$term = get_term_by('slug', tdb_state_content::get_wp_query()->get('term'), tdb_state_content::get_wp_query()->get('taxonomy') );
$tdbLoadDataFromId = $term->term_id;
}

It seems to work but I am not sure if it is foul proof.

  • This reply was modified 4 years by dimitrisv.
Post count: 162

This
$tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object->term_id;

Seems to work.

Here is a short recording regarding the final issue: the fact the it prints everything on the top left of the screen and not on the place where the shortcode is called.

The page that I am experimenting is this one: https://carro.gr/product-category/bmw/1600-2002/1600-2002-66-77/

  • This reply was modified 4 years by dimitrisv.
  • This reply was modified 4 years by dimitrisv.
Post count: 162

I sorted it out. It (you 🙂 ) require return instead of echo.

add_shortcode( 'category_pods', 'pods_category_shortcode' );
function pods_category_shortcode() {
$tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object->term_id;
return do_shortcode(
'[pods name="product_cat" ID="'.$tdbLoadDataFromId.'"]'. '[/pods]'
);
}

Post count: 35449

I’m glad that you manage to achieve what you needed!

Viewing 16 posts - 1 through 16 (of 16 total)
The forum ‘Newspaper’ is closed to new topics and replies.