WordPress Custom Taxonomy display category - wordpress

Having problems solving a basic (but new to me) custom taxonomy issue--I have used the
get_the_category() function quite a bit in my theme. Namely as a "search and replace" if you will
foreach((get_the_category()) as $category) pulls the category of "state" and is used:
/images/States/' . $category->cat_name . '.png
(category= colorado, then it pulls colorado.png)
Easy. Great. but....I would like to do this same thing with a custom taxonomy but can't figure out how to get it to work.
I have a Custom Post Type of "City Pages" (city_pages)
For those pages I have created a taxonomy of "State" (state)
(these have to be different than the "categories" of state)
I have looked, I have researched and I cannot figure out how to do this. I know there are similar questions/problems out there but I have not been able to put 2 and 2 together in order to get this to work.

I think this is what you want: http://codex.wordpress.org/Function_Reference/get_terms
So it woud be something along the lines of:
<?php
$states = get_terms( 'state' );
foreach( $states as $state ) {
echo '<img src="./images/States/' . $state->name . '.png">';
}
?>

Related

where should i link my custom post type taxonomies?

I'm a newbie and trying to create my own Wordpress Theme. I have created Custom Post Types named 'Works' and a Taxonomy named 'Categories' (with tags as "Custom CMS", "Wordpress" ect)
I want each work to dislay his categories, and to link to a page with all simular Works.
This is my code:
$terms = get_the_terms( $post->ID , 'categories' );
foreach ( $terms as $term ) {
echo $term->name;
}
This works ok, but now i'm baffled:
How, and where should i link each taxonomy?
What's the proper template name, and code?
i appreciate your help and time,
Giannis
Here are some guide to your query.
How, and where should i link each taxonomy?
Answer: It will link to specific taxonomy page.
What's the proper template name, and code?
Answer: The template hierarchy is explained here Wordpress Taxonomy
To have taxonomy link on anchor tag inside your loop:
<?php echo $term->name; ?>
Please feel free to ask if you have any confusion.

Get a sidebar widget that show products of the same categories in Woocommerce

I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.

Wordpress Custom Taxonomy Template

found some help on here earlier today and hoping to do so again about creating a custom template. :-)
I have a Wordpress custom post type set up in my functions.php
I have a custom taxonomy called "Classifications"
Under that taxonomy I have the terms/categories:
Old Research (parent)
--- sub cat 1
--- sub cat 2
I have created the file "taxonomy-Classifications-oldresearch.php" which successfully changes the layout for me. However, it does not hold the layout in the sub categories. I can alter it by also creating "taxonomy-Classifications-subcat1-oldresearch.php" but I will have a lot of sub categories that I want to use one template for. This way I don't have to create a new template file for every new category. Does anyone have a good solution for me?
Thank you!
There is a great post on the Wordpress stack exchange that addresses exactly this issue. The problem is that Wordpress uses a template hierarchy to load php templates based on the slugs of each term.
You can do some custom coding inside your taxonomy template to show proper sub categories as you want. For example, let suppose your current parent taxonomy is oldresearch then you will be taken to specific taxonomy template as WordPress does, in your case you will be taken to oldresearch template page. Inside that template you can get id of your current term and then can get children data from that category. In code it will look something like shown below:
Note: this below code is for reference you can use it according to your needs.
<?php
$term_id = 10;
$taxonomy_name = 'Classifications';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
In this way you can play with your sub-categories data. $term_id is just for reference purposes, you will be getting id of your parent taxonomy term and so will be getting sub categories from that term.
For further reading check Get Children terms Codex

Show posts in Category with Tag (Wordpress)

I am trying to display a lists of posts on a custom post template that are in a 'category' and that have a specific 'tag' that matches the title of the post.
For example I have posts by city name,
New York
Chicago
Texas
...etc
I then have several categories,
News
Events
Classifieds
...etc
For every post I make to one of those categories I then assign a post tag that matches the name of the city.
For example if I have a news item for New York, I select the 'News' category and assign the post the 'New York' tag.
When I am on the custom post template page for New York I want to retrieve posts associated with the News category that have the tag name = New York (the title of the post).
However my dilema is that I can not figure out how to dynamically generate the tag name from the title of the post.
Eg.
<?php
$tag = wp_title('', FALSE);
query_posts( 'tag=' . $tag . '' );
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
wp_reset_query(); ?>
But when getting the tag name using wp_title() it would print 'New York' with a space between the words 'New' and 'York'. This doesn't work.
To get it to work it would need to be 'new-york' with the hyphen in place. But I can not figure out how to generate the tag name based on title with hyphens in place of spaces.
I hope this makes sense.
I appreciate any and all efforts in helping.
Thank you.
In the query_posts funciton, the tag parameter refers to the slug, not the name. Unfortunately, the query_posts function cannot accept the tag name; however, there is a wonderful WP function that can act as an intermediary for us. To get the tag information based on the name, you can use this:
$term = get_term_by('name', $name_of_tag, 'post_tag');
$term will be an object with the following information:
term_id
name
slug
term_group
term_taxonomy_id
taxonomy
description
parent
count
For instance, you can get the id with $term->term_id. So putting this all together, you should be able to accomplish what you need with:
$tag_name = wp_title('', FALSE);
$term = get_term_by('name', $tag_name, 'post_tag');
query_posts( 'tag_id=' . $term->term_id);
I've not tested what wp_title actually returns, so I don't know if this properly returns the title, but I'm trusting you on this ;)
Sources:
http://codex.wordpress.org/Function_Reference/WP_Query
http://codex.wordpress.org/Function_Reference/get_term_by

wordpress sidebar issue with wp_list_categories showing NO Categories

So on my wordpress install. I am trying to display category list in the sidebar but having issue with wp_list_categories function. There are handful of categories in the system but this function just prints "NO Categories".
Can't figure out why.
Any ideas?
Make sure you have at least 1 Post in every category you want to display
wp_list_categories should be outside of the wordpress LOOP. You'll probably need to provide the sidebar's code before the LOOP's code.
You're using the right function, but you need to adjust a parameter for it. You are getting
No Categories
simply because the categories defined in the WordPress taxonomy have no posts assigned to them.
Try passing the hide_empty argument to wp_list_categories( $args ); 1 for true and 0 for false.
wp_list_categories('hide_empty=0');
This example will show all categories regardless of their post count.
Reference the wp_list_categories Codex page for more help.
to use inside loop, use get_posts() instead of get_categories();
$cat_ID = 239;
$array = get_posts('child_of'=> $cat_ID , 'post_type'=> 'post'); $out='';
foreach ($array as $key=> $value) {
$out .= '<li class="manual_posts">'.$value->post_title.'</li>';
}
echo $out;

Resources