Jump to content


Replying to Product Category And Tags


Post Options

  or Cancel


Topic Summary

dweuste

Posted 07 November 2014 - 11:53 AM

Well, that at least got me on to the right track! That thread showed me how to list all of my exchange categories, which isn't what I wanted, but that at least gave me a function to look up in the WP Codex, where I found the code I needed to list only the categories for the post. In case anyone else needs this answer, I'll put it here.

In the content-product.php template file in my child theme I added the following code:

<div class="product-categories">Related:
<?php
$taxonomy = 'it_exchange_category';
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array('fields' => 'ids'));
$separator = '; ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );

// display post categories
echo $terms;
}
?>
</div>

Nice thing about it working with core WordPress is that I didn't even need to add a function in functions.php.

Thanks for pointing me in the right direction!

Ronald

Posted 07 November 2014 - 10:42 AM

Hi,

you might want to look into this instead: https://wordpress.org/support/topic/list-categories-for-custom-post-type

Ronald

dweuste

Posted 07 November 2014 - 10:15 AM

In the function in my functions.php or in the content-product.php template part?

--Update--

Nevermind. Figured that part out. Any way you could point me to how to make them links?

Ronald

Posted 07 November 2014 - 10:03 AM

Hi,

the function returns a value. In order to make that visible, you need to echo (http://php.net/manual/en/function.echo.php) it

does that help?
Ronald

dweuste

Posted 05 November 2014 - 04:02 PM

Sorry if chiming in here isn't kosher, but I'm trying to use the above answer in order to list categories in a "footer" kind of area on the product page (much like a category list would appear on a standard blog post), and I'm getting lost somewhere. What I initially tried was simply doing a copy-paste of your code into the functions.php of the theme, and then calling get_product_categories(); in the content-product.php page, but it doesn't seem to be working. Any suggestions or am I way off track in order to accomplish what I'm trying?

Ronald

Posted 02 July 2014 - 04:14 PM

Hi,

yes it is. Glad it's working for you, I will mark this resolved

srumery

Posted 02 July 2014 - 02:52 PM

Looks like for tags it is actually 'it_exchange_tag'.

srumery

Posted 02 July 2014 - 02:49 PM

Yo Ronald,

That is exactly what I am looking for! Thank you.

For one site I am working on, each product is in one category and that category needs to be printed on top of the product title in a custom product grid I wrote. It needs different styling from the title so I can't just put the text there. The category is a perfect place to store this information, now I can retrive the information and put it where I want it. Thank you.

Another use in this site is to use tags to manage product icons. A tag of "pdf" will print out a icon. Multiple icons are possible but this is still a 1-to-1 relationship for now. I'm sure there is an equal 'it_exchange_tags' attribute like 'it_exchange_category' in your example. I'll create a new function for that and see how it works.

Thanks again, Ronald.

Ronald

Posted 27 June 2014 - 07:58 AM

Hi,

how exactly do you want to use the info? As part of you product template?

Here is a function that will return a space separated list of product categories wrapped in a div class:

function get_product_categories() {

	$all_prod_cats = "";

	if ( ! empty( $GLOBALS['it_exchange']['product']->ID ) ) :

		$terms = get_the_terms( $GLOBALS['it_exchange']['product']->ID, 'it_exchange_category' );

		if ( $terms && !is_wp_error( $terms ) ) :

			$prod_cats = array();

				foreach ( $terms as $term ) {
					$prod_cats[] = $term->name;
				}

			$all_prod_cats = join( " ", $prod_cats );

		endif;

	endif;

	return "<div class='my-exchange-product-categories" . $all_prod_cats . "'>";

}

that might be a start,

Ronald

srumery

Posted 27 June 2014 - 06:02 AM

Is there shortcode that we can use to write out the product category or product tags? Something like this?

[exchange-product-cat product-id=123]


If not, I can create it but I need to know how to retrieve the product category and tags. I am using a similar function, it_exchange_set_product, to get product details and writing them on my page. Here is what I am using.

// run iThemes Exchange function to return an array with product details
it_exchange_set_product( $id );

// write the product title
it_exchange( 'product', 'get-title' )


From the array that is returned, I am not seeing the product category or product tag information. It's easy to enter it into the product using Exchange but I can't seen to get that data back out. How can I get to this data?

Review the complete topic (launches new window)