I am facing a bit complex situation with my taxonomy terms. I have list of taxonomy terms.
Taxonomy (property-status):
--2018
--2019
--2020
--2021
--Coming Soon
my taxonomy have multiple terms, usually i select one term from the taxonomy to display which i use this code to get:
$status_terms = wp_get_post_terms( get_the_ID(), 'property-status');
if($status_terms) {
foreach ( $status_terms as $term ) {
echo $term->name;
}
}
which is working perfect for me, but now i have selected two taxonomy terms 2019 and coming soon. If both are selected i want to show only 2019 i don't want to show coming soon alongside 2019, but if only coming soon is selected then i want to show coming soon.
You can count the terms and filter them accordingly. This might be a little bit too verbose, but may do the trick:
$status_terms = wp_get_post_terms( get_the_ID(), 'property-status');
if($status_terms) {
// Get the term names only
$term_names = array_map(function($term) { return $term->name; }, $status_terms);
if ((count($term_names) > 1) && in_array('coming-soon', $term_names)) {
// More than one term and coming-soon. Filter it out
foreach ( $status_terms as $term ) {
if ($term->name != 'coming-soon') {
echo $term->name;
}
}
} else {
// Show everything
foreach ( $status_terms as $term ) {
echo $term->name;
}
}
}
Shorter solution:
if($status_terms) {
$many_terms = (count($status_terms) > 1);
foreach ( $status_terms as $term ) {
if ($many_terms) {
if ($term->name != 'coming-soon') {
echo $term->name;
}
} else {
echo $term->name;
}
}
}
Related
I want to display text on a single brand page on Woocommerce.
I wrote this snippet on my functions.php but it doesn't seem to work:
function woo_brand_page_check() {
$brands = wp_get_post_terms( $post->ID, ‘product_brand’, array("fields" => "all") );
if (is_tax( 'product_brand' )) {
foreach( $brands as $brand ) {
if($brand == 'BRAND_NAME') {
echo 'Custom text ';
}
}
} else {
echo '';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
If you have any suggestions on how to implement this, please drop me a line!
Thanks in advance!
If your Brands are product attributes, the taxonomy should be automatically prefixed with 'pa_', so you should use pa_product_brand.
Double check if product_brand has a dash in place of the underscore (as usually happens), so it would become pa_product-brand
Also, you may parr the term in is_tax() instead of looping all terms, something like:
function woo_brand_page_check() {
if ( is_tax( 'pa_product_brand', 'BRAND_NAME' ) ) {
echo 'Custom text ';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
See: https://codex.wordpress.org/Function_Reference/is_tax
Cheers ;)
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! :)
I found this function to display the terms attached to a post but i can't manage to find a way to exclude specific ID's of some category terms i don't want to display in the list.
Could someone give me a clue were to start? I looked up all the functions used in this function but can't seem to find arguments to exclude id's.
Thanks in advance!
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<li>'.$term->name.'</li> ';
}
}
$out .= "</ul>";
return $out;
}
Add another conditional if statement within your second foreach() to check whether or not the $term should be ignored. For example:
// An array of IDs to ignore/exclude
$excluded_ids = array( 1, 2, 3, 4);
foreach ( $terms as $term ) {
// Only proceed if the term_id is NOT in the $excluded_ids array
if ( !in_array( $term->term_id, $excluded_ids ) ) {
$out .= '<li>'.$term->name.'</li> ';
}
}
I'm working on something on wordpress + woocommerce.
I've inputed some 'filter' attributes value for some products.
So if the value of the attribute for this product includes a "Wood Grains" value, i want to echo an Woodgrains.jpg on the front end.
Therefore if the value = Texture, i want to echo Texture.jpg icon.
the code below is what i've mustered so far, but this only echoes out all the values tagged to a product, i can't figure out what to change to get the 'if' statement in it.
$terms = get_the_terms( $product->id, 'pa_filter');
foreach ( $terms as $term ) {
echo $term->name;
}
here's a screenshot of what the code above does on the front end:
http://edleuro.com/new/wp-content/themes/mystile/img/1.png
If this returns an array of terms for the said product:
$terms = get_the_terms( $product->id, 'pa_filter');
You can check if the returned result array has what you are looking for by doing this:
if (in_array("Wood Grains", $terms))
{
// has it
}
else
{
// doesn't have it
}
Update
Based in your reply to my answer, I have came up with the following:
Create a helper function like this:
function termExists($myTerm, $terms)
{
if (is_array($terms)) {
foreach ($terms as $id => $data) {
if ($data->name == $myTerm)
return true;
}
}
return false;
}
Then you use it like this:
if (termExists('Wood Grains', get_the_terms($product->id, 'pa_filter')))
{
// term exists
}
else
{
// does not exists
}
i found a better way to resolve my problem. i had this in my content-product.php woocommerce template.
<?php
$terms = get_the_terms( $product->id, 'pa_filter');
foreach($terms as $term){
if($term->name == 'Wood Grains'){
echo '<img src="icon_woodgrains.gif">';
}
}
?>
do take note that the term name is case sensitive.
foreach prints Warning: invalid argument supplied for foreach()…
This works for me "on a purely":
if ( is_product() && has_term( 'Wood Grains', 'pa_filter' )) {
echo '<img src="Texture.jpg">';
}
This seems like a simple enough problem but I'm a novice at PHP and I've been working on this for hours. I'm looping through posts in an archive and showing a different logo for each post based on a certain attribute. Here's my existing function in functions.php:
function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
);
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->name == 'L1' ) {
// Show L1 Logo
}
elseif ( $term->name == 'M1' ) {
// Show M1 Logo
}
elseif ( $term->name == 'H1' ) {
// Show H1 Logo
}
else {
$full_line = '<span>'. $term->name . '</span>';
}
array_push( $terms_array, $full_line );
}
echo implode( $terms_array );
}
}
}
}
All I want to do is do show a different logo if the post matches multiple terms (e.g. 'L1' AND 'M1'). I have tried many very different things but I have no idea if I'm even on the right track. Any help would be greatly appreciated.
This should be fairly easy, I'm just not exactly sure the full context of all the data involved.
Assuming you are only showing one logo per post, here is one approach:
Right before foreach ( $terms as $term ) { create three boolean variables:
$hasL1 = false;
$hasM1 = false;
$hasH1 = false;
Then when one of your term names matches, instead of just showing the logo set the appropriate variable equal to true, ie $hasL1 = true;
After the foreach is complete, either before or after echo implode( $terms_array ); depending on what makes sense, setup a new if/elseif/else block to decide what logo to show as follows:
if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
elseif ($hasL1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1 && $hasH1) { // Pick logo for pair }
elseif ($hasH1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1) { // Pick logo }
elseif ($hasM1) { // Pick logo }
elseif ($hasH1) { // Pick logo }
else { // default logo }
Of course there are many other ways to implement this too.