Wordpress - Display category on single page - wordpress

I create a custom post type with ACF.
I have two different taxonomy for this custom type :
Project type
House type
In House type, i have 4 different category :
all types
House
Appartement
Building
Each post need 'all type' and one other (for example : all type, house)
i would like to display the main category : House and not display : all types
How can i hide 'all types' ?
My code :
<div class="detailfiche">
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms) {
$output = array();
foreach ($terms as $term) {
$output[] = '<span>' .$term->name .'</span>';
}
echo join( ' ', $output );
};
?>
</div>

Try this
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms)
{
$output = array();
foreach ($terms as $term)
{
if ($term->term_id != 222)
{
$output[] = '<span>' . $term->name . '</span>';
}
}
echo join(' ', $output);
};
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222

Related

How to display taxonomy list with link to taxonomy page on the single custom post type page

I need to list the taxonomies that apply to this post. If the taxonomy only one then this code works well:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
if($terms){
$output = array();
foreach($terms as $term){
$outputnk[] = get_term_link( $term->slug, 'gintype');
$outputme[] = $term->name;
}
}
But if a post has several taxonomy items this code does not work.
This code is working for me:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
foreach($terms as $term){
$term_link = get_term_link( $term );
echo '<div>' . $term->name . '';
}

How to display category name of the woocomerce product in wordpress theme header

as i mentioned in the title, i want to show the category name & its description in the header file of my theme.
i tried to do with several functions which i got from search like i tried below, i have added this in my functions.php, but that not works
function sk_show_product_category_description() {
if (is_singular( 'product' )) {
global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo '<div class="widget-background-wrapper"><div class="widget product-cat-description"><h4 class="widget-title">Note</h4>' . $term->description . '</div></div>';
}
}
lastly i tried by including class WC_Product, but that not works too, i have below mentioned the code which i used for it
global $woocommerce, $post, $WC_Product;
$file =$woocommerce->plugin_path.'/classes/abstracts/abstract-wc-product.php';
$getWooClass = include_once($file);
$test = $getWooClass->get_categories(122);
var_dump($test);
Please guide how can i display the category name of the current product and & its description??
Try this,
global $post;
$args = array( 'taxonomy' => 'product_cat',);
$terms = wp_get_post_terms($post->ID,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
for more
Hope its helps..

List custom taxonomies children links

I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>
use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>

Show Product category list in Woocommerce Wordpress Plugin without thumbnails

I am using the following short code to list the product categories in woocommerce Wordpress plugin
<?php echo do_shortcode('[product_categories number=""]'); ?>
The problem is the category thumbnail that I need to get rid of. I want to hide it and doing it with CSS seems to be a big hassle. Is there anyway I can list the categories without the thumbnails appearing?
You could use the following:
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
if (count($terms) > 0) {
echo '<p class="my_term-archive">';
foreach ($terms as $term) {
echo '' . $term->name . '';
}
echo '</p>';
}
Heres what I used for making a list of all categories by letter excluding empty letters and with category featured images!
All you need to do is style it the way you want ;)
<?php
/* Define which category // to list child categories array('parent'=>42) 42 is category ID */
$designers = get_terms('product_cat', array('parent'=>42));
/* If categ not empty for each designer take first letter */
if ( !empty( $designers ) && !is_wp_error( $designers ) ){
$term_list = [];
foreach ( $designers as $designer ){
$letter = strtoupper($designer->name[0]);
$designer_list[$letter][] = $designer;
}
unset($designer);
foreach ( $designer_list as $key=>$value ) {
/* Wrap around each designer */
echo '<div><span>' . $key . '</span><ul>';
foreach ( $value as $designer ) {
// Set thumbnail
$thumbnail_id = get_woocommerce_term_meta( $designer->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
/* List designers */
echo '<li>' . $designer->name . '<img src=" ' .$image.' "" alt="" /></li>';
}
/* end Wrap around designer */
echo '</ul></div>';
}?>
}

Trouble getting taxonomy image with Taxonomy Meta plugin in Wordpress

I am using the Taxonomy Meta plugin and have followed all instrauctions but feel like something is wrong with what I am doing. I just want to pull the image that is assigned to each category is a custom taxonomy and display it on a page template..
the github repo of the plugin can be found here: https://github.com/rilwis/taxonomy-meta
please help, I get everything to show but the image and when I look in the source the image path isnt there just an empty img tag.
I can get an image to print using this:
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['5']) ? $meta['5'] : array();
$images = $meta['community-image'];
echo '<ul>';
foreach ($images as $categories) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($categories, 'thumbnail');
$src = $src[0];
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'properties_community'
);
$categories=get_categories($args);
// show image
foreach($categories as $category) {
echo '<li><a href="' . home_url() . '/?property_communities='. $category->slug .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '><img src="'.$src.'"/>' . $category->name.'</a> </li> ';
}
}
echo '</ul>';
but I cannot figure out how to make it where the image matches the category being displayed.. I just put the number 5 in there to see if it worked which it does but if I try using $category->term_id it doesnt work and I am a bit lost from here..
UPDATE:
I am now able to pull the images assigned to each category and print the names but I am getting this error "Warning: Invalid argument supplied for foreach() in /home/../themes/../my-template.php on line 39"
Here is my code
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<img src="'.$src.'" />' . $term->name . '';
}}
}
Anyone see why I may be getting this error? I dont see anything wrong with the foreach :/
Line 39 would be this: foreach ($images as $att) {
Got it
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
if (empty($images)) {
echo '<p style="text-align:center;">' . $term->name . '</p>';
} else {
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<img src="'.$src.'" /><p style="text-align:center;">' . $term->name . '</p>';
}}
}
}

Resources