Retrieve category description when using wp gridbuilder - php

How to retrieve the category description when using a filter like wp Gridbuilder?
I created a sort filter for the categories, when clicking on a category the url looks something like this: domain.com/music/?_categories=8-bit-chiptune
Normally using <?php echo category_description( $category_id ); ?> would work on the archive category page but whe using a filter there is no category page.
Thanks

You can try the below code & process as your need.
<?php
$cat_slug = $_GET['_categories'];
$idObj = get_category_by_slug( $cat_slug );
if ( $idObj instanceof WP_Term ) {
echo $id = $idObj->description;
}
Feel free to query question about it.

Related

How to use PHP variable with a Wordpress Shortcode of AAWP Plugin

I use Wordpress with the GeneratePress Theme. A feature of this theme called "Elements" let you write php code which will be executed on a specific hook. These Hooks can be found here: https://docs.generatepress.com/article/hooks-visual-guide/
I use the AAWP plugin to show an amazon bestseller list. I want to get the Wordpress category and place it into the bestseller parameter of the aawp shortcode to get Products for the respective categories.
I already read posts here about using php variables in wordpress shortcodes but it seems that something other than the syntax is the problem.
I have this code
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp grid="3" '.$bestseller.' items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
The amazon list doesnt appear on my page. It seems that the "bestseller" parameter is neccesarry in order to get shown.
The variable doesnt really get recognized.
When I remove the variable and replace it with a hardcoded bestseller parameter its working fine.
When I use this code:
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
It seems that the bestseller value doesnt get read but the "empty" bestseller parameter is enough to show the amazon list, so it just shows random products.
How can I make this work? I just want to place a variable as a value into the "bestseller" parameter. Should be the easiest thing but it doesnt work in the scope of this GeneratePress Elements Hook whith the AAWP shortcode
You're possibly not getting what you believe you need back from get_the_category(). Assuming you're able to get the category id you can use get_cat_name() function to return the category name.
<?php
$cat_name = get_cat_name($category_id);
if ( ! empty($cat_name) ) {
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$cat_name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>

Wordpress how to get field from parent category

I'm trying to display an image in a custom field on child category archive pages.
The current setup is:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$custom_field = get_field('brand_background', $post_id);
?>
<div id="brandHeader" class="brand-header-logo" style="background-image: url('<?php the_field('brand_background', $post_id); ?>');">
This obviously gets the 'background image' field from the category archive it's currently on.
If the category is a child category I want to be able to pull the 'background image' from the parent category. I've tried a few things but doesn't seem to be working - can anyone help?
You could use the get_ancestors function to get the parent id and use that id to get your field:
get_ancestors
So let's say your categories looks like this:
-Parent (id = 24)
--Child (id = 42)
$term_id = get_queried_object()->term_id;
$term_ancestor_ids = get_ancestors($term_id, 'product_cat');
// if you do a print_r on $term_ancestor_ids
// you'll see this:
// Array ( [0] => 24 )
$post_id = 'product_cat_'.$term_ancestor_ids[0];
$custom_field = get_field('brand_background', $post_id);
Tested and works, let me know if you were able to get it to work too!

How to limit the results of the_category in Wordpress?

I'm building a wordpress theme, but got stuck on a problem. It's on the blog archive page. When you go to the blog, you see a list of blog posts. And on each post preview, you see the author, title, et cetera. My problem is that I'm trying to display one category for each post preview. I don't want to show people an entire list of every category a post has. I want to limit the results of the_category() to ONE.
You could do something like this:
$category = get_the_category(); echo $category[0]->cat_name;
However, that will just return the first category name on the list. Instead, I'd suggest using a plugin to set the primary category.
For instance, with WP Category Permalink, (possibly outdated) you can get the primary category like this:
<?php
$perma_cat = get_post_meta($post->ID , '_category_permalink', true);
if ( $perma_cat != null && is_array($perma_cat)) {
$cat_id = $perma_cat['category'];
$category = get_category($cat_id);
} else {
$categories = get_the_category();
$category = $categories[0];
}
$category_link = get_category_link($category);
$category_name = $category->name;
?>
<?php echo $category_name ?>

Get Categories of events in Wordpress Events Calendar Pro Plugin

I'm using Events Calendar Pro plugin (https://theeventscalendar.com/product/wordpress-events-calendar-pro/) and I need to get all categories for each event.
I tried single_cat_title() and get_the_category() but they are not what I needed.
Actually, single_cat_title() function shows only the first category and get_the_category() returns empty array.
You can use following code to get details of each terms.
$cats = get_the_terms( $post_id, 'tribe_events_cat' );
$term = get_term( $cats[1], $taxonomy ); // Getting the 2nd term item
$name = $term->name; //Getting names of terms
More details from https://codex.wordpress.org/Function_Reference/get_term.
Let me know if you have further questions.
I tried single_cat_title() and get_the_category() but they are not what I needed.
get_the_category() function retrieves default categories, post categories. As the categories defined by Events calendar pro plugin is not default, you need to use get_the_terms() function.
In get_the_terms() function, you need to pass post_id as first parameter and taxonomy / category name.
So, wrapping all up you can try below code :-
$event_cats = get_the_terms( $post_id, 'tribe_events_cat' )
Let me know if you need further assistance...
You could try to make a copy of the event.php via the following path:
[your-theme] /tribe/events/v2/list/event.php
and then write the following there:
<?php foreach (get_the_terms(get_the_ID(), 'tribe_events_cat') as $cat) { echo $cat->name; } ?>
And then put it in span/p and styled it.
Inspired from: https://wordpress.stackexchange.com/questions/220511/correct-use-of-get-the-terms

Wordpress get the category name on each category

I want to make the code to display 'MORE STORIES FROM #CATEGORYNAME' as a title of the box that appears. As the user is viewing different category the name to change.
Ex. User is viewing a post in category 'cars' when the box appears the title would be MORE STORIES FROM 'cars'.
When he switch to category 'IT', the name to be 'MORE STORIES FROM IT'
if(!emptry($td_query_more_article->posts)) {?>
<div class="td-more-articles-box">
<i class=td-icon-close td-close-more-articles-box"></i>
<span class="td-more-articles-box-title"><?php echo __td('MORE STORIES', TD_THEME_NAME) ?></span>
<div class="td-content-more-articles-box">
I hope I was specific enough. Corrent me if I am wrong
Sorry, this needs bit more clarification how you want to achieve this; do you want this on widget or add it statically on the theme? Is there a reason why excisting plugins don't work for you?
If you are in the WordPress loop, you can simply using the following:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}
This will give you the first category if there are more than one.
If you are not in the loop, you will need to pass the post id as a parameter, like this;
// get the post object
global $post;
// pass post id
$categories = get_the_category($post->ID);
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}
You can get category name using this function.
$cat_name = get_category(get_query_var('cat'))->name;

Categories