我有WordPress任务,似乎没有任何答案可用。
我有什么: 我有一个名为“product_taxonomies”的自定义分类法和名为“products”的自定义帖子类型。在名为“taxonomy-product_taxonomies.php”的分类法文件中,我获取了属于当前分类的所有产品,并且我也使用分页打印它们。因此,当我点击名为“产品类别1”的分类法时,我会将所有产品分页。
我需要什么: 我需要修改我的代码,对于当前的分类法,它打印的产品和子分类法都混合在一起并且也是分页。因此,当我点击名为“产品类别1”的分类法时,我应该将所有产品和子类别“产品子类别1”,“产品子类别2”等分页。分页应该像产品和子分类法是一样的,它们看起来也应该是同一个东西:
Here is the image showing how I want it to be like
我设法做了什么: 我使用了get_categories()来获取当前分类的所有子分类并打印它们。问题在于,所有子分类法都不与产品混合,分页只考虑产品而不考虑子分类。
这是我的代码:
<ul>
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args1 = array(
'type' => 'post',
'child_of' => $current_id,
'parent' => '',
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => 0,
'include' => '',
'number' => '',
'taxonomy' => 'product_taxonomies',
'paged' => $paged,
'pad_counts' => false );
$sub_terms = get_categories($args1);
foreach($sub_terms as $sub_term){
//var_dump($sub_term);
$sub_term_link = get_term_link($sub_term->slug, 'product_taxonomies');
$sub_term_image = get_field('category_image', 'product_taxonomies_' . $sub_term->term_id );
?>
<li class="mix">
<a href="<?php the_permalink(); ?>">
<div class="product_bg"><img src="<?php echo $sub_term_image['sizes']['product_images']; ?>" /></div>
<div class="product-caption"><?php echo $sub_term->name; ?></div>
<div class="overlay-div">
<div class="overlay-text">
<?php echo $sub_term->category_description; ?>
</div>
</div>
</a>
</li>
<?php
}
wp_reset_query();
$args = array(
'post_type' => 'products',
'posts_per_page' => get_option('posts_per_page'),
'tax_query' => array(
array(
'taxonomy' => 'product_taxonomies',
'field' => 'id',
'terms' => $current_id,
),
),
'paged' => $paged
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$i=1;
while ( $query->have_posts() ) { $query->the_post();
$terms = wp_get_post_terms(get_the_ID(), 'product_taxonomies');
$tags = wp_get_post_terms( get_the_ID(), 'product_tag');
$all_terms = "";
foreach($terms as $single_term){
$all_terms .= $single_term->slug . " ";
}
foreach($tags as $single_tag){
$all_terms .= $single_tag->slug . " ";
} ?>
<li class="mix <?php echo trim($all_terms); ?>">
<a href="<?php the_permalink(); ?>">
<div class="product_bg"><?php the_post_thumbnail('product_images'); ?></div>
<div class="product-caption"><?php the_title(); ?></div>
<div class="overlay-div">
<div class="overlay-text">
<?php echo get_the_excerpt(); ?>
</div>
</div>
</a>
</li>
<?php $i++;
}
wp_reset_query();
}else{
_e('Sorry, no available products under this category.','site');
} ?>
</ul>
<div class="pagination_area">
<?php
//global $query;
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $query->max_num_pages,
'prev_text' => __('<i class="fa fa-chevron-left"> </i>'),
'next_text' => __('<i class="fa fa-chevron-right"> </i>'),
'type' => 'list'
));
wp_reset_query();
?>
<?php wp_pagenavi(); ?>
</div>