Get the categories joined to a custom taxonomy - php

I have a custom post of portfolio items. Within the custom post I have categories set up, as well as a custom taxonomy of clients. I am listing the portfolio items by client, but I need also need to filter these by which category they appear in. I have this so far:
$terms = get_terms('client');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<ul>";
foreach ( $terms as $term ) {
$term_id = $term->term_id;
echo '<li class="item"> ';
$taxonomy = 'category';
$client_cats = get_terms($taxonomy);
foreach ($client_cats as $client_cat) {
echo '<li>' . $client_cat->name.'</a></li>';
}
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
echo $term->name . "</li>";
}
echo "</ul>";
} else {
echo "No posts";
}
But this gives me a list of the clients with a list of all the categories after each one. How can I filter the categories so that only the ones the client is linked to is displayed?
Thanks for any help! :)

Related

Only display Woo Commerce sub-categories based on selected Parent Category in a wordpress widget sidebar

I have a WordPress sidebar that I want to only display the children of the parent category. My structure is as follows
All Products > Parent Category > Children Categories
How do I get the sidebar to only display the Parent Category and Children Categories.
I read a solution to implement this code but I'm not sure where to put it inside the files.
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<ul>'.$this_category.'</ul>';
}
}
?>
These are screenshots from what my shop looks like and what I want.
Create a shortcode in your theme's functions.php file as follows:
add_shortcode('child_category_list', 'get_child_category_list');
function get_child_category_list(){
ob_start();
// Only on product parent category pages
if( is_product_category() ) {
$parent = get_queried_object();
$categories = get_term_children( $parent->term_id, 'product_cat' );
if ( $categories && ! is_wp_error( $categories ) ) :
echo '<ul>';
echo '<li>';
echo ''.$parent->name.'';
echo '<ul>';
foreach($categories as $category) :
$term = get_term( $category, 'product_cat' );
echo '<li>';
echo '<a href="'.get_term_link($term).'" >';
echo $term->name;
echo '</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo '<li>';
echo '</ul>';
endif;
}
return ob_get_clean();
}
Then, go to Appearance > Widgets, grab either shortcode or text widget into your sidebar and paste following shortecode:
[child_category_list]
Tested on localhost & its working fine.

Display current category first then child categories in WooCommerce

I'm trying to display in the sidebar the current page's category and it's subcategories. The title should be the current category's name and linked to the current category as well. An example of what I'm trying to achieve can be seen here in the sidebar: https://food52.com/shop/pantry
This is my current site as an example:https://farmtofrank.wpengine.com/product-category/prepared-foods/
This is the code I've created so far:
<?php
$terms = get_terms([
'taxonomy' => get_queried_object()->taxonomy,
'parent' => get_queried_object_id(),
]);
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
echo '<div>';
foreach ( $terms as $term) {
echo '<p class="filters">' . $term->name . '</p>';
}
echo '</div>';
?>
It works but it puts the parent link at the bottom of the list. How can I keep the parent link at the top above the subcategories?
I suppose that this is related to product category archive pages. In this case, you are very near, try:
$queried_object = get_queried_object();
if ( is_product_category() && is_a($queried_object, 'WP_Term') ) {
$taxonomy = $queried_object->taxonomy;
echo '<h2 class="shop__sidebar-heading">
' . $queried_object->name . '
</h2>';
$children_terms = get_terms(['taxonomy' => $taxonomy, 'parent' => $queried_object->term_id]);
if ( ! empty($children_terms) ) {
echo '<ul class="'.$queried_object->slug.' child-terms">';
// Loop through children terms
foreach ( $children_terms as $term) {
echo '<li class="filters">' . $term->name . '</li>';
}
echo '</ul>';
}
}
Tested and works. It will require some styling (CSS).

Shortcode - Taxonomy terms list with ACF term icon

I'm trying to create a [shortcode] that will display taxonomy terms of a post in a list with an icon.
I am using ACF for the icon with meta book_icon, then trying to call it into the shortcode following the ACF forum post here.
This is my code so far:
add_shortcode( 'show-books', 'books_output' );
function books_output($atts){
ob_start();
echo '<ul class="books-terms">';
global $post;
$taxonomy = 'books';
$terms = get_the_terms( $post, $taxonomy );
$image = get_field('book_icon');
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
echo '<li><img src="' . $image['url'] . '" class="book-css-class">' . $term->name . '</li>';
}
}
echo '</ul>';
$output = ob_get_clean();
return $output;
}
I'm getting the taxonomy terms in a list, but not the image icon.
Where am I going wrong?

need to create a related taxonomy stores

I have a coupon site, which basically has many vendors in it. I want to create a related stores widget kind of a thing which would display 5-6 stores on the side bar. and i want to them to relate to the current vendor page. till now i have managed to display 5-6 stores links randomly, but i am not able to display the related stores.
I am using clipper theme in wordpress, and i have kept all my stores in Coupons-Stores and not Post-categories.
<?php
$stores = get_terms('stores'); // Note: this returns NULL non-empty cats. It is not normal, could be because of the widget.
// Get all non-empty stores
$nonempty_stores = array();
foreach( $stores as $store )
if ( !empty( $store ) )
$nonempty_stores[] = $store;
$stores_count = count($nonempty_stores);
global $random_seed;
// Set initial seed value
$random_seed = $last_update;
if (DISPLAY_SAME_STORES_LIST_FOR_ALL_STORE_PAGES == false)
$random_seed = $random_seed + $store_catid;
function mtech_rand($min, $max)
{
global $random_seed;
$random_seed = ((1103515245*$random_seed)+12345)%(1<<31);
return (($random_seed%($max-$min+1))+$min);
}
echo "<div class=\"hdsboxbg\"> ";
$cntr = 0;
$displayed_cntr = 0;
while (($cntr < $stores_count) && ($displayed_cntr < NUM_OF_RANDOM_STORES_TO_DISPLAY))
{
$rand_store_idx = mtech_rand($cntr, $stores_count-1);
$rand_store = $nonempty_stores[$rand_store_idx];
if (($store_catid == 0) || ($rand_store->cat_ID != $store_catid)) // Random Stores Widget should not display a link to the current store page
{
$a_text = $rand_store->name . ' Coupons';
//$a_text = $rand_store->name;
echo "<div class=\"hdsmod\"> $a_text </div>";
$displayed_cntr++;
}
$nonempty_stores[$rand_store_idx] = $nonempty_stores[$cntr];
$nonempty_stores[$cntr] = $rand_store;
$cntr++;
}
echo "</div>";
?>
Any other methods are welcome. i have tried to display all the stores like this,
$terms = get_terms('stores');
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
it displays but i am not able to display the related stores, i am struggling to figure out on which grounds should i proceed. Any help would be appreciated.
got it.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>

wordpress - display subcategory siblings when viewing a single post

Thank you all for a great site. I'm learning a lot. I'm trying to get my subcategory siblings to show when a single post is clicked on. I have set up my parent categories using the wordpress menu. I am using a php widget to call for the children (subcategories) in a separate menu (and then style with CSS). The code I'm using is showing the specific (relevant) children when each category is clicked on; however I am unable to figure out how to make them appear when viewing a post.
<?php
if(is_category()) {
$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
if(empty($subcategories) && $thiscat->parent != 0) {
$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}
$items='';
if(!empty($subcategories)) {
foreach($subcategories as $subcat) {
if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
$items .= '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
'.$subcat->name.'
</li>';
}
echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);
}
?>
I'm attempting to mimic the behavior of this menu at pioneer woman.com
Any help or a better solution would greatly be appreciated.
Thanks,
This lists all child categories of the current post's category:
<?php
echo '<ul>';
$post_child_cat = array();
foreach((get_the_category()) as $cat) {
$args = array( 'child_of' => $cat->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
'<a title="'.$category->description.'" href="';
echo bloginfo('url');
echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
$category->name.'</a></li>';
}
}
echo '</ul>';
?>

Categories