admin管理员组文章数量:1025217
I'm working on a project where I use the default posts as "Products", default category as "Application" and a custom taxonomy called "Groups".
I want to list "Products" by "Application" (category.php) but group them together into their respective "Groups":
- Automotive (Application)
- Silver (Group)
- Product 01
- Product 04
- Product 05
- Flake (Group)
- Product 02
- Product 03
- Product 06
- Silver (Group)
I’ve tried different attempts but with no successful result.
Is there an easy way to go about this? Thanks!
I'm working on a project where I use the default posts as "Products", default category as "Application" and a custom taxonomy called "Groups".
I want to list "Products" by "Application" (category.php) but group them together into their respective "Groups":
- Automotive (Application)
- Silver (Group)
- Product 01
- Product 04
- Product 05
- Flake (Group)
- Product 02
- Product 03
- Product 06
- Silver (Group)
I’ve tried different attempts but with no successful result.
Is there an easy way to go about this? Thanks!
Share Improve this question asked Jul 24, 2012 at 14:10 rafawhsrafawhs 3083 silver badges14 bronze badges1 Answer
Reset to default 8I've found a solution!
<?php
// Get current Category
$get_current_cat = get_term_by('name', single_cat_title('',false), 'category');
$current_cat = $get_current_cat->term_id;
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'myposttype';
$tax = 'mytaxonomy';
$tax_terms = get_terms( $tax, 'orderby=name&order=ASC');
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'category__in' => $current_cat // Only posts in current category (category.php)
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2><?php echo $tax_term->name; // Group name (taxonomy) ?></h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "ids")); // Get post categories IDs?>
<?php if (in_array($current_cat, $term_list) ): // Display only posts that have current category ID ?>
<h3><?php the_title(); ?></h3>
<?php endif; // if in_array ?>
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #tax_terms
} // end if tax_terms
?>
I'm working on a project where I use the default posts as "Products", default category as "Application" and a custom taxonomy called "Groups".
I want to list "Products" by "Application" (category.php) but group them together into their respective "Groups":
- Automotive (Application)
- Silver (Group)
- Product 01
- Product 04
- Product 05
- Flake (Group)
- Product 02
- Product 03
- Product 06
- Silver (Group)
I’ve tried different attempts but with no successful result.
Is there an easy way to go about this? Thanks!
I'm working on a project where I use the default posts as "Products", default category as "Application" and a custom taxonomy called "Groups".
I want to list "Products" by "Application" (category.php) but group them together into their respective "Groups":
- Automotive (Application)
- Silver (Group)
- Product 01
- Product 04
- Product 05
- Flake (Group)
- Product 02
- Product 03
- Product 06
- Silver (Group)
I’ve tried different attempts but with no successful result.
Is there an easy way to go about this? Thanks!
Share Improve this question asked Jul 24, 2012 at 14:10 rafawhsrafawhs 3083 silver badges14 bronze badges1 Answer
Reset to default 8I've found a solution!
<?php
// Get current Category
$get_current_cat = get_term_by('name', single_cat_title('',false), 'category');
$current_cat = $get_current_cat->term_id;
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'myposttype';
$tax = 'mytaxonomy';
$tax_terms = get_terms( $tax, 'orderby=name&order=ASC');
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'category__in' => $current_cat // Only posts in current category (category.php)
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2><?php echo $tax_term->name; // Group name (taxonomy) ?></h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "ids")); // Get post categories IDs?>
<?php if (in_array($current_cat, $term_list) ): // Display only posts that have current category ID ?>
<h3><?php the_title(); ?></h3>
<?php endif; // if in_array ?>
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #tax_terms
} // end if tax_terms
?>
本文标签: loopDisplay category posts grouped by taxonomy
版权声明:本文标题:loop - Display category posts grouped by taxonomy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745613006a2159116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论