I'm using woocommerce on my theme, and it comes with a product_cat term as a default category on the shop product. I need to show on every product_cat archive its direct child and the sub-tems, but not the sub terms child.
I tried:
<?php $thispage = $wp_query->post; wp_list_categories("taxonomy=product_cat&term=". $term->slug."&title_li=&child_of=".$thispage->slug);?>
and it returned all product_cat as ul and the sub-term of the archive im in, but not the archive's children.
I tried:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat');
foreach ( $terms as $term )
$currentID = get_the_ID();
$args=array(
'taxonomy'=>'product_cat',
'term' => $term->slug,
'child_of'=>$currentID
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ): ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li> <?php the_title();?></li>
<?php endwhile; ?>
<?php endif; ?>
and it returned all the the current archive's children and the sub-terms children without the sub-term it self.
So I tried :
$term_id = $terms;
$taxonomy_name = 'product_cat';
$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>';
and it returned nothing.
Got an idea... if this returns only the sub term:
wp_list_categories( array('child_of' => get_queried_object_id(),'taxonomy' => 'product_cat','title_li' => '','depth' => 1, 'show_option_none'=> ''));
maybe i can exacude all of its children in a query args and then call the wp_list_categories. tried:
$terms = get_the_terms( $post->ID, 'product_cat'); foreach ( $terms as $term ) $currentID = get_the_ID(); $args=array( 'taxonomy'=>'product_cat', 'term' => $term->slug, 'exclude '=> array('child_of' => get_queried_object_id()) ); $my_query = new WP_Query( $args );?>
but it returns only the sub-term children, not the the proudct_cat children...
Anyone?
I'm Speechless. anyone have a clue?
*sorry for my poor English
I think you could do something like
<?php
$taxonomy = 'product_cat';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $product->id, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$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&child_of=' .get_queried_object_id() . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
Related
i have problem in wordpress
i need way to make category in catgory in category in category
example
i have category have name bmw have sub catgory xll in xll catgory 1999 in catgory 1999 have sub catgory parts and others catgory like engine , etc
so we have 4 level catgory
how we do that this is my code
<?php
$args = array(
'orderby' => 'name',
'taxonomy'=>'brand_category',
'parent' => 0
);
$categories = get_categories( $args );
if($categories->parent > 0){ ?>
<?php foreach ( $categories as $category ) { ?>
<li><?php echo $category->name ;?></li>
<?php } ?>
<?php } else { ?>
<?php $queried_object = get_queried_object();
$term_ids = $queried_object->term_id;
$term_id = $term_ids;
$taxonomy_name = 'brand_category';
$term_children = get_term_children( $term_id, $taxonomy_name, $parent = $term->parent);
$parent = $term->parent;
?>
<?php foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}?>
<?php }?>
I need to display all the product tags as a dropdown on main page. I tried the following code but it did not work.
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
It always returns an empty array. Any suggestions? Thanks.
I'm not certain what you mean by dropdown, but will use <select> for this answer.
In your functions.php
function get_some_tags_man(){
$terms = get_terms( array(
'hide_empty' => false, // only if you want to hide false
'taxonomy' => 'product_tag',
)
);
$html = '';
if($terms){
$html .= '<select name="terms" id="someID">';
foreach($terms as $term){
$html .= "<option name='$term->name'>$term->name</option>";
}
$html .= '</select>';
}
return $html;
}
in your theme file:
<?php echo get_some_tags_man(); ?>
Try this, it will also show empty tags.
$terms = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false ) );
I just tried the code (in the product page and Shop page) and it worked.
Main Page you mean Shop Page? The first page that appear when you enter the site.
The theme i'm using is StoreFront.
Can you show the hook you are using? or the code? Look. This is the complete code I use.
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_product_loop_tags', 15 );
function woocommerce_product_loop_tags() {
global $post, $product;
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
$idProducto=$product->id;
$product->get_title();
echo $product->get_tags( ', ', '<span class="tagged">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo " ".$term_array[] = $term->name;
}
}
}
I am fighting to get what should be a very simple Wordpress custom taxonomy archive template to work.
I'm getting all the information and the posts are listed alphabetically but the terms are in order of id and need them in alphabetical order.
I'm fudging my way through this and am currently using the code from this post. I have tried a multitude of solutions from around the net, with no luck. I see this post has a solution but don't know how to implement it in the code below.
Perhaps there's an easier way of doing what I need?
The query needs to get the current parent term, then the child terms, and the posts in the child terms. The following code is on my taxonomy-business-categories-(parent term).php, for example my taxonomy-business-categories-bars.php. I need to output the child terms grouped with their posts. All must be in alphabetical order.
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<?php the_title();?>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
Here's a link to the taxonomy template as .txt. file.
UPDATE: As I'm hardcoding the taxonomy templates by parent term, could I use something like this with my code above:
<?php
$term_id = 32;
$taxonomy_name = 'business-categories';
$termchildren = get_term_children( $term_id, $taxonomy_name );
$children = array();
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$children[$term->name] = $term;
}
ksort($children);
Replace this code with your above provided code
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'name', $child, $taxonomyName );
$wpq = array (
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc',
'orderby'=>'title');
$query = new WP_Query ($wpq);
echo "$term->name:<br />";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<?php the_title();?>,
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "<br />";
}
?>
Let me know if that works for you ...
The solution in my case was to install the plugin Custom Taxonomy Order NE and (as directed by the plugin's author) adjusted the query as follows:
Replace this:
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
With this
$termchildren = get_terms( array(
'taxonomy' => $taxonomyName,
'child_of' => $current_term->term_id,
) );
foreach ($termchildren as $term) {
$wpq = array (
I have a page that is creating a WP Loop that creates a list of pages, and I'd like to change the default sort to be alphabetical. This is the WordPress loop.
<?php
$i = 1;
while ($wp_query->have_posts()) : $wp_query->the_post();
$postid = get_the_ID();
$terms = get_the_terms($postid, 'program_categories' );
$terms2 = get_the_terms($postid, 'program_type' );
$permalink = get_permalink( $postid );
$title = get_the_title( $postid );
$raw_date = the_modified_date('F j, Y','','',false);
$d = strtotime($raw_date);
$all_terms = '';
if ( !empty( $terms ) ) { foreach ($terms as $term) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } }
if ( !empty( $terms2 ) ) { foreach ($terms2 as $term2) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term2->name)). ' '; } }
?>
These are the buttons for changing the sort after the page loads, in case this helps provide info.
<li><?php _e('Default','swmtranslate'); ?></li>
<li><?php _e('Alphabetical','swmtranslate'); ?></li>
<li><?php _e('Last Updated','swmtranslate'); ?></li>
Is it as simple as adding something like this to the beginning of the loop?
while ($wp_query->have_posts()) : $wp_query->the_post("SELECT * FROM $title ORDER BY ASC");
This is covered right in the Codex. You need to define an orderby and order parameter in your query args:
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$wp_query = new WP_Query( $args );
I think this link will be helpfull for your question. You need to set your custom query to short by title something like this.
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
(Alphabetizing Posts)
Your full code will be like this:
<?php
$i = 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$wp_query = new WP_Query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
$postid = get_the_ID();
$terms = get_the_terms($postid, 'program_categories' );
$terms2 = get_the_terms($postid, 'program_type' );
$permalink = get_permalink( $postid );
$title = get_the_title( $postid );
$raw_date = the_modified_date('F j, Y','','',false);
$d = strtotime($raw_date);
$all_terms = '';
if ( !empty( $terms ) ) { foreach ($terms as $term) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } }
if ( !empty( $terms2 ) ) { foreach ($terms2 as $term2) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term2->name)). ' '; } }
?>
I have this code that works great to show the subcategory name and link of a specific parent category of my choosing:
<?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$categories = get_the_category();
$parentid = '6';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = strtr( wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids), array( '<br />' => ' <br /> ' ) );
echo preg_replace( '#\s<br />\s\n$#', '', $terms );
}
?>
Now I want to be able to do the same thing but WITHOUT the code above generating the link automatically. Any ideas?
Use get_categories() instead of wp_list_categories()
$terms = get_categories("child_of={$parentid}&include={$term_ids}");
foreach($terms as $term)
echo $term->name;