Space between custom taxonomy terms in wordpress - php

I am using the following to list the terms within my custom taxonomy
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
foreach($application_terms as $application){
echo $application->slug;
}
}
}
?>
The code works fine and it displays the terms however, if a post type has two "application" terms assigned to it, the two terms appear without space in betweeen them e.g ig apple and banana it appears applebanana. How do you change the code so that there is space in between the terms?
Thanks

Create an array of slugs and implode it with a blank space
$application_terms = wp_get_object_terms($post->ID, 'application');
if ( $application_terms && !is_wp_error( $application_terms ) {
$slugs = wp_list_pluck( $application_terms, 'slug' );
$string = implode( ' ', $slugs );
echo $string;
}

It should work in the way, you want it to be
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
$i = 0;
foreach($application_terms as $application){
if($i>0)
echo ', ';
echo $application->slug;
$i++;
}
}
}
?>
so if a post have two category assigned, it will display like apple, banana

Related

Manipulate and sort the term names of a product attribute in Woocommerce

I'm trying to sort names in alphabetical order after a reverse array.
It's a code done for ordering last name/first name in right order.
A few bugs, (like with names with middle names) but it works except the sorting.
Here is the code:
<?php
$terms = get_terms( 'pa_artist' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="artists">';
foreach ( $terms as $term ) {
$array = explode(" ", $term->name);
if ($array[3]) {
$array[3] = strtoupper($array[3]);
$array[3] = "<strong>".$array[3]."</strong>";
}
elseif ($array[2]) {
$array[2] = strtoupper($array[2]);
$array[2] = "<strong>".$array[2]."</strong>";
} elseif ($array[1]) {
$array[1] = strtoupper($array[1]);
$array[1] = "<strong>".$array[1]."</strong>";
} else {
$array[0] = strtoupper($array[0]);
$array[0] = "<strong>".$array[0]."</strong>";
}
$rarray = array_reverse($array);
sort($rarray);
echo '<li>' . implode(" ", $rarray) . '</li>';
}
echo '</ul>';
}
For now the names are ordered as if the reverse was never done.
Some examples, at first it showed like this:
Auguste Renoir
Pablo Picasso
Paul Gauguin
After the reverse and If strings, it's like this:
RENOIR Auguste
PICASSO Pablo
GAUGUIN Paul
When i need it:
GAUGUIN Paul
PICASSO Pablo
RENOIR Auguste
I tried every sort fonction, can't make it work… I can't find a way to sort after a reverse array, is it even possible?
It's for a list of names builded with attributes on wordpress/woocommerce.
I already asked that question, got answers that didn't work unfortunately…
There is Something like 150 names that needs ordering.
I'm willing to pay for this, but no-one is interested because it doesn't require much time so won't pay much! (Only got request to redo the whole website…)
Try the following:
$terms = get_terms( [ 'taxonomy' => 'pa_artist', 'hide_empty' => false ] );
$names_html = $terms_data = [];
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
// 1st Loop: Loop through the terms
foreach ( $terms as $term ) {
$fragments = explode( ' ', $term->name );
if( sizeof($fragments) > 1 ) {
// Manipulate and format the term name
$fragment = '<strong>' . strtoupper($fragments[1]) .'</strong>';
$new_term = $fragment . ' ' . $fragments[0];
// 1st array: We set each formatted term name
$names_html[] = $new_term;
// 2nd array: We set the related data as the Url and the original term name
$terms_data[$new_term] = array(
'link' => get_term_link( $term ),
'name' => $term->name,
);
}
}
// Sort the formatted term names
sort($names_html);
// Output
echo '<ul class="artists">';
// 2nd Loop: Loop through the sorted formatted term names
foreach ( $names_html as $name_html ) {
$link = $terms_data[$name_html]['link'];
$title = sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $terms_data[$name_html]['name'] );
echo '<li>' . $name_html . '</li>';
}
echo '</ul>';
}
Tested and works.

Woocommerce | Display text on a single brand page

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 ;)

Comma separate custom taxonomies (no links)

Here's just a simple little question I can't seem to figure out. I have one custom taxonomy which has multiple options. But I want to show the taxonomies without links. So I use this code:
<li>
<?php
$terms_as_text = get_the_term_list($post->ID, 'opties');
if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) , '';
?>
</li>
This display's only the selected custom taxonomies of opties. Because this taxonomy has multiple options I would like to comma separate them. But it won't let me.
Normally you would use:
<?php
echo get_the_term_list( $post->ID, 'opties', '<ul><li>', '</li><li>', '</li></ul>' ); ?>
The first part = before.
The middle part = separate.
The last part = after.
But this makes links of the custom taxonomy terms, and I don't want that to happen.
But because of the strip_tags($terms_as_text) I can't comma separate them.
How can I get them to separate with a comma?
You can try the following:
global $post;
$opties = wp_get_post_terms($post->ID, 'opties', array("fields" => "names"));
if (count($opties) > 0)
{
echo implode(', ', $opties);
}
try this
$list = get_the_term_list( $post->ID, 'opties');
$terms ="";
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $list, $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$terms .= $match[3].","; // Get Text Only //nolinks
}
}
echo rtrim($terms,","); // display and remove extra ,
Try this:
if( is_singular( 'custom_post_type' ) ) {
get_the_term_list( get_the_ID(), 'custom_post_type_name', '', ', ' );
}
elseif( is_singular( 'post' ) ) {
get_the_category_list( ', ', '', get_the_ID() ) . '';
}
The above will return categories or custom post taxonomies with comma separated and a link to the response category or taxonomy.
The simple way to get the category or term without link is to use strip_tags() function as follow:
<?php echo strip_tags(get_the_term_list( get_the_ID(), 'category_name' ));?>

If attribute value equals to... show something

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">';
}

List only child categories a post is in, of a specific parent category

Either this is harder than it needs to be or I am just not understanding WordPress/PHP very well :( All I want to do is show the child/sub categories of a specific parent category...but only if the post is in those subcategories. Specific example:
I am building a wine reviews website and these are the categories:
BrandSubcategory 1Subcategory 2etc.
RegionSubcategory 1Subcategory 2etc.
GrapeSubcategory 1Subcategory 2etc.
The parent categories will never change, and every post will have at least 1 subcategory selected under each parent, so in the LOOP I can just list the parents by name. But I am needing to dynamically output the subcategories, something like this:
Brand: <?php list_post_subcategories('brand'); ?>
Region: <?php list_post_subcategories('region'); ?>
Grape: <?php list_post_subcategories('grape'); ?>
Is there any easy way like this? It seems like this should be a basic function in Wordpress? I've looked at the functions 'get_categories' and 'in_category' but they don't seem to be able to do this.
<?php $post_child_cat = array();
foreach((get_the_category()) as $cats) {
$args = array( 'child_of' => $cats->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo $category->cat_name; }
} ?>
try this
I posted to Wordpress Answers to get more help and #Milo gave a great code solution:
// get top level terms
$parents = get_terms( 'category', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '' . $parent->name . ': ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
if( $parent->term_id == $category->parent ):
// put link in array
$links[] = '' . $category->name . '';
endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;
You can use array_map so you can return only the categories that you want. For example:
array_map( function( $cat ) {
if ( $cat->parent != 0 ) {
return $cat;
}
},
get_the_category()
);

Categories