admin管理员组文章数量:1122832
This code uses Taxonomy Images plugin to display images for categories. This code display subcategories of main category in parent category page (category.php
), But I want to display only one level of subcategory in the page for example
hardware (parent category)
monitor (first level subcategory of harware)
samsung (second level subcategory)
- lcd (third level subcategory )
When the user clicks on the hardware link, he should see only monitor subcategory link and in second time when he clicks on the monitor, he should see the samsung category link, and when he click on the samsung the subcategory LCD will displays. How should I change this code to do my word
My code:
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<div class="categoryoverview clearfix">';
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
foreach( (array) $terms as $term ) {
if($term->term_id == $categories_item->term_id) {
print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
echo '</a>';
}
}
echo '<p>'. $categories_item->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
?>
This code uses Taxonomy Images plugin to display images for categories. This code display subcategories of main category in parent category page (category.php
), But I want to display only one level of subcategory in the page for example
hardware (parent category)
monitor (first level subcategory of harware)
samsung (second level subcategory)
- lcd (third level subcategory )
When the user clicks on the hardware link, he should see only monitor subcategory link and in second time when he clicks on the monitor, he should see the samsung category link, and when he click on the samsung the subcategory LCD will displays. How should I change this code to do my word
My code:
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<div class="categoryoverview clearfix">';
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
foreach( (array) $terms as $term ) {
if($term->term_id == $categories_item->term_id) {
print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
echo '</a>';
}
}
echo '<p>'. $categories_item->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
?>
Share
Improve this question
edited Apr 5, 2018 at 13:33
Andy Macaulay-Brook
3,8593 gold badges19 silver badges44 bronze badges
asked Mar 1, 2013 at 8:15
user28212user28212
111 silver badge4 bronze badges
1
|
4 Answers
Reset to default 0I'm going to guess here. I assume you are using the Taxonomy Images plugin?
This:
if($term->term_id == $categories_item->term_id) {
Can be changed to:
if($term->term_id == $categories_item->term_id && $term->parent == $categories_item->cat_ID) {
Hereby, you check if the parent ID of the term
is the same as the ID of the categories_item
.
The parent category (hardware) you should display this category in any page by getting all the parent categoty using
$parent_category = get_categories(array('orderby' => 'name','parent' => 0));
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}
When you click on the category then it will go to category.php
page. In category.php page you will get all the category whose parent is hardware.
get_categories(array('parent' => 'hardware_category_id/its_sub_category_id',
'hide_empty' => 1,));
Then You should display all the child category of hardware. display these category in <a></a>
tag as we display earlier. Whenever you click on child category link then it will go again to category.php
page ant it will the take the category id as child category id and it will show the sub-child category of child category. this all go in loop.
The Taxonomy Images plugin you're using can handle all this. The filter taxonomy-images-get-terms
can take a third argument which can include an array of arguments for the underlying WP function get_terms().
Anyone not using this plugin can use a call to get_terms
instead.
If you specify the current category as the parent argument, you should get all of the child categories returned. Just the children, one level, not all the deeper descendants too.
So you can use this in your theme's category.php
to generate a single depth display of the current category's immediate sub-categories.
$cat_id = get_query_var('cat');
$catlist = apply_filters(
'taxonomy-images-get-terms',
'',
array(
'parent' => $cat_id,
)
);
foreach ( $catlist as $cat ) {
echo '<a href="';
echo esc_url( get_term_link( $cat->term_id, $cat->taxonomy ) );
echo '">';
echo wp_get_attachment_image( $cat->image_id, 'thumbnail' );
echo '<p>';
echo $cat->description;
echo '</p>';
echo '</a>';
}
You could use wp_list_categories
instead. It has an ability to set depth.
本文标签: categoriesDisplay only one level subcategory in wordpress
版权声明:本文标题:categories - Display only one level subcategory in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288100a1928023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<ul>
and</ul>
should be used in combination with<li>
. – user26607 Commented Mar 1, 2013 at 8:43