I am working on a online shop project. I this i created a post type i.e Products and taxonomy products_categories Men Wearing, Women wearing and kids etc. In sidebar i created a widget and display all the custom taxonomies. Now i want to display all products related to that particular category. So when i click on any category suppose men then my url looks like "htp://mydomain/mysite/product_categories/kids-clothing/". I created a taxonomy.php but still 404.php is running. Can somebody tell me whats wrong in ht code.
'
<?php
$taxonomy = 'product_categories';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<h4 class="title1">Show By Category</h4>
<ul id="list">
<?php
wp_list_categories( $args );
?>
</ul>'
Thanks.
Related
Been banging my head for a few hours trying to sort this. Finally got a function together that will list all the terms in a custom taxonomy I created in Woocommerce > Products, which works.
What I want to do:
Get/Display the image for each term next to the title (Created an Image field with ACF for that taxonomy item) and display the description underneath.
I can probs figure out the description part, but having a hard time with getting the image to render.
Here's what I've got so far:
//---------Start ACF code
//
// Define taxonomy prefix
// Replace NULL with the name of the taxonomy eg 'category'
$taxonomy_prefix = 'item';
// Define term ID
// Replace NULL with ID of term to be queried eg '123'
$term_id = NULL;
// Define prefixed term ID
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
//----------End ACF Code
$taxonomy = 'item';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$image = get_field('image', $taxonomy . '_' . $term_id);
echo ' <img src="'.the_field( 'image', $term_id ) .'" /> ';
echo ''. $cat->name .'<br />';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<a class="subcat" href="'. get_term_link($sub_category->slug, 'item') .'">'. $sub_category->name .'</a><br/>';
}
}
}
}
You can see the output here:
https://doorsdev.wpengine.com/individual-flower-essence-descriptions/
When I inspect I can see the field rendering but a blank src attribute, and with no errors on the page, I'm not sure what to do next.
I'm guessing it's something to do with the $term_id, becuase it tells me to define it, but since I don't want one specific term, I want it for each term that gets pulled into the loop....I'm not sure how to define that.
Any help appreciated.
Thanks!
as shown in the code your $term_id is null. Try replacing $term_id with $category_id.
Replace this code(EDITED).
$image = get_field('image', 'term_' . $category_id);
echo ' <img src="'.$image .'" /> ';
Reference: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I am developing a custom theme for a client of mine selling mainly mobile devices. I am using WP All Import Pro to import products 4 times a day with a cron job and I am updating the stock levels every hour also using cron jobs both from an external xml link.
All that works fine but my client wants a new menu to display all the products by Brand Name.
There are plenty of plugins like WooCommerce brands, yith and perfect brands but they all require me to add the brands manually.
When there are 550+ products and there updated at least 4 times a day this becomes impossible.
I have added the brand in custom fields as you can see in this screengrab...
I have added a menu called brands but there just custom links and go nowhere. I am unsure how to code the brand custom field into this menu. Here is the website I am developing so you have an idea of what's needed..
Many thanks for any pointers you may have.
Instead of a default WordPress sidebar you can use your own custom sidebar with a new custom navigation.
Please, check the following code and you'll see the categories list:
<nav>
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<li>'. $cat->name .'</li>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>
</ul>
</nav>
Just set the Category (as a brand) for the products and you'll see a simple result.
This is what I have:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'directory-category',
'pad_counts' => false
);
Which gets me the categories.
What I want is to get the child categories of this directory-category taxonomy.
Any ideas on how to do that?
I'm not asking for a solution, just an advice or someone to show me the road.
Googling didn't help :/
Here is a screenshot HERE
You said you didn't want a direct answer, but essentially you want to use get_terms found here:
https://developer.wordpress.org/reference/functions/get_terms/
SELECT * from prod_term_taxonomy WHERE parent = 0;
UPDATE:
// Using your specific parent taxonomy id of 214 the query is below
global $wpdb;
$results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");
// then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...
$child_cat_array = array();
foreach ($results as $result) {
$term = get_term( $result->term_id, $taxonomy );
$name = $term->name;
// the slug will be used for querying for posts
$slug = $term->slug;
// this will push the slug of the child category into the array for querying posts later
array_push($child_cat_array, $slug);
}
You can then modify your get_posts query like this:
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'directory-category',
'field' => 'slug',
'terms' => $child_cat_array
)
)
);
$postslist = get_posts( $args );
If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:
function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
$category = get_term_by( 'slug', $term, $taxonomy );
$cat_id = $category->term_id;
// Get all subcategories related to the provided $category ($term)
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id,
'orderby' => 'term_id',
'hide_empty' => true
)
);
?>
<div>
<?php
// Iterate through all subcategories to display each individual subcategory
foreach ( $subcategories as $subcategory ) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
// Get all posts that belong to this specific subcategory
$posts = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ( $posts->have_posts() ):
?>
<ul>
<?php
while ( $posts->have_posts() ): $posts->the_post();
//Show the title of each post with the Post ID
?>
<li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
else:
echo 'No posts found';
endif;
wp_reset_query();
}
?>
</div>
<?php
}
ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );
'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'
'name-of-your-post-type' is the name of your post type. e.g.: 'victual'
'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'
So if I call the function:
ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:
Subcategory: Appetizers - ID: 35 - Slug: appetizers
Post: Chips & Salsa - ID: 464
Post: Queso - ID: 465
Subcategory: Tacos - ID: 36 - Slug: tacos
Post: Al Pastor - ID: 466
Post: Fish Al Pastor - ID: 467
I'm having a really hard time with this... essentially I just want to display breadcrumbs on the product category pages in woocommerce so that it shows the main category and sub categories. Hoping some smart kind soul can help me :)
This normally could be done easily with the following code:
<?php woocommerce_breadcrumb(); ?>
However, I'm using a purchased theme with woocommerce integrated, so it doesn't have that function and I tried to integrate it but no luck.
So, I found someone who had the same issue and made my own breadcrumbs for prordu. Which works, but I just want the product category to show ONLY the main category and sub category each product have. The code shows ALL categories and sub categories, not just the one I want to display.
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php $all_categories = get_categories( $args );
//print_r($all_categories);
foreach ($all_categories as $cat) {
//print_r($cat);
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
?>
<?php
echo '<br />'. $cat->name .''; ?>
<?php
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
} ?>
<?php }
}
?>
I have 2 custom post types called 'project' and 'client' that share a taxonomy called 'sector'.
if (!is_taxonomy('sector')) {
register_taxonomy(
'sector', array('project', 'client'), array(
'hierarchical' => true,
'label' => 'Sector',
'query_var' => true,
'rewrite' => array( 'slug' => 'sector' ),
'with_front' => false
) );
wp_insert_term('Health', 'sector');
wp_insert_term('Clubs', 'sector');
wp_insert_term('Commercial', 'sector');
}
I have created a taxonomy archive template with a sidebar nav that lists links to my taxonomy archives using:
//list terms in a given taxonomy using wp_list_categories
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$show_option_none='';
$title = '';
$args_sector = array(
'taxonomy' => 'sector',
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
<ul id="sideNav" class="rightSubMenu">
<h3 class="rightSubNav">SECTOR</h3>
<ul id="sideNav" class="rightSubMenu">
<?php wp_list_categories( $args_sector ); ?>
</ul>
</ul>
The problem is if I have a project that is linked to 'clubs' and a client that is linked to 'clubs' the output count shows 2. Also the archive page shows 2 posts - 1 for project and one for client. But there is only one project.
I am mainly concerned with the project page and would like to filter the results by my 'project' post type. I looked through the codex and the wp_list_categories function doesn't seem to accept a parameter to do this.
Can anyone help? Is there a better way to do this?
I have had a similar problem. I did this by cloning the wp_list_categories function, giving it a different name and putting in this code after the line: $categories=get_categories($r):
foreach ($categories as $key => $category){
$temp = array ( 'post_type'=>$r['type'], 'tax_query' => array(
array (
'taxonomy' => $category->taxonomy,
'field' => 'slug',
'terms' => $category->slug
)
)
);
$pauli = new wp_query($temp);
if($pauli->post_count==0){
unset($categories[$key]);
}
}
As you can see, it removes categories that do not have any of the post type you need, and then continues the process as wp_list_categories does normally.