admin管理员组文章数量:1295962
I have created a custom post type called "al_product" and attached to it a custom taxonomy called "product_category".
In my archive-product_category.php I have this (relevant) code:
if ( $termchildren ) {
// we have kids...just show the terms.
$i=1;
foreach ( $termchildren as $child ) {
if ($i % 2 == 0) {
echo '<div class="tax-entry flex">';
} else {
echo '<div class="tax-entry flex" style="flex-direction: row-reverse;">';
}
?>
<figure class="wp-block-media-text__media">
<?php if ( get_field('product_category_img', $child) ) echo wp_get_attachment_image( get_field('product_category_img', $child), 'full' ) ;?>
</figure>
<div style="flex:60% 0 0;" class="wp-block-media-text__content">
<h2><?php echo $child->name ;?></h2>
<p>
<?php
if (get_field('short_description', $child )){
echo get_field('short_description', $child );
}else{
echo $child->description;
}?>
</p>
<?php
// Get (at most) 2 "product" posts in the child term.
$posts = get_posts( array(
'post_type' => 'al_product',
'posts_per_page' => 2,
$taxonomy_name => $child->slug,
) );
// If there's exactly 1 post, use the post permalink.
if ( 1 === count( $posts ) ) : ?>
<a class="button" href="<?php the_permalink( $posts[0] ); ?>">See this Product</a>
<?php // Else, use the term link.
else : ?>
<a class="button" href="<?php echo esc_url( get_term_link( $child, $taxonomy_name ) ); ?>">See this Series</a>
<?php endif; ?>
</div>
</div> <!-- END FLEX -->
<?php
$i++;
}
} else {
// no kids...show the products
echo '<div class="productCategories grid">';
while ( have_posts() ) : the_post();
?>
<div class="product_cat">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail('small');?>
<h2><?php the_title();?></h2>
<?php
$subtitle = get_field('subtitle');
if ( $subtitle ) {
echo '<h3 class="entry-subtitle">'.$subtitle.'</h3>';
}
?>
</a>
<?php //if (get_field('specs')['part_number']) echo '<span>'.get_field('specs')['part_number'].'</span>';?>
</div>
<?php
endwhile;
echo '</div>';
}
The section I'm questioning is when there are no kids so we show products.
Currently this is returning the products correctly but sorting by title. I would like to sort by menu_order.
I added this to my functions.php:
add_action( 'pre_get_posts', 'rt_change_product_sort_order');
function rt_change_product_sort_order($query){
if(get_query_var( 'product_category' )):
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
$query->set( 'orderby', 'menu_order' );
endif;
};
To no avail. I've also tried replacing the "If" statement with each of these:
if(is_post_type_archive( 'product_category' )):
if(get_query_var( 'product_category' )):
if(get_query_var( 'al_product' )):
What am I missing here?
I have created a custom post type called "al_product" and attached to it a custom taxonomy called "product_category".
In my archive-product_category.php I have this (relevant) code:
if ( $termchildren ) {
// we have kids...just show the terms.
$i=1;
foreach ( $termchildren as $child ) {
if ($i % 2 == 0) {
echo '<div class="tax-entry flex">';
} else {
echo '<div class="tax-entry flex" style="flex-direction: row-reverse;">';
}
?>
<figure class="wp-block-media-text__media">
<?php if ( get_field('product_category_img', $child) ) echo wp_get_attachment_image( get_field('product_category_img', $child), 'full' ) ;?>
</figure>
<div style="flex:60% 0 0;" class="wp-block-media-text__content">
<h2><?php echo $child->name ;?></h2>
<p>
<?php
if (get_field('short_description', $child )){
echo get_field('short_description', $child );
}else{
echo $child->description;
}?>
</p>
<?php
// Get (at most) 2 "product" posts in the child term.
$posts = get_posts( array(
'post_type' => 'al_product',
'posts_per_page' => 2,
$taxonomy_name => $child->slug,
) );
// If there's exactly 1 post, use the post permalink.
if ( 1 === count( $posts ) ) : ?>
<a class="button" href="<?php the_permalink( $posts[0] ); ?>">See this Product</a>
<?php // Else, use the term link.
else : ?>
<a class="button" href="<?php echo esc_url( get_term_link( $child, $taxonomy_name ) ); ?>">See this Series</a>
<?php endif; ?>
</div>
</div> <!-- END FLEX -->
<?php
$i++;
}
} else {
// no kids...show the products
echo '<div class="productCategories grid">';
while ( have_posts() ) : the_post();
?>
<div class="product_cat">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail('small');?>
<h2><?php the_title();?></h2>
<?php
$subtitle = get_field('subtitle');
if ( $subtitle ) {
echo '<h3 class="entry-subtitle">'.$subtitle.'</h3>';
}
?>
</a>
<?php //if (get_field('specs')['part_number']) echo '<span>'.get_field('specs')['part_number'].'</span>';?>
</div>
<?php
endwhile;
echo '</div>';
}
The section I'm questioning is when there are no kids so we show products.
Currently this is returning the products correctly but sorting by title. I would like to sort by menu_order.
I added this to my functions.php:
add_action( 'pre_get_posts', 'rt_change_product_sort_order');
function rt_change_product_sort_order($query){
if(get_query_var( 'product_category' )):
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
$query->set( 'orderby', 'menu_order' );
endif;
};
To no avail. I've also tried replacing the "If" statement with each of these:
if(is_post_type_archive( 'product_category' )):
if(get_query_var( 'product_category' )):
if(get_query_var( 'al_product' )):
What am I missing here?
Share Improve this question asked Apr 8, 2021 at 0:08 rudtekrudtek 6,3535 gold badges30 silver badges52 bronze badges 3 |1 Answer
Reset to default 1In my archive-product_category.php I have this (relevant) code
Are you sure that's the correct name? Because for a post type archive, it should be archive-al_product.php
and for a taxonomy archive page, it would be taxonomy-product_category.php
.
Anyway, (as I said in the comments) if you want to target the taxonomy archive page, you should use is_tax()
, or if it's the post type archive page, then you would use is_post_type_archive()
.
Note that the first (and only) parameter for
is_post_type_archive()
is one or more post type names/slugs likeal_product
in your case. So in your code/attempts, thatis_post_type_archive( 'product_category' )
is actually not correct.. =)Also, your post type should have the
has_archive
argument set totrue
or a slug likeal-products
. That way, when you visit the archive atexample/?post_type=al_product
orexample/al-products
,is_post_type_archive()
would return true.
So to correctly sort the posts (in the main WordPress query), you can try the following:
// In the rt_change_product_sort_order() function:
// Instead of using get_query_var(), you would use is_tax().
if ( is_tax( 'product_category' ) ) :
// ... your code.
endif;
// Or use is_post_type_archive() to target the CPT archive.
if ( is_post_type_archive( 'al_product' ) ) :
// ... your code.
endif;
// Or combine the conditional tags, if you want to:
if ( is_tax( 'product_category' ) || is_post_type_archive( 'al_product' ) ) :
// ... your code.
endif;
Additional Resources
- https://developer.wordpress/themes/basics/conditional-tags/
本文标签: sort CPT on Custom Taxonomy Archive page
版权声明:本文标题:sort CPT on Custom Taxonomy Archive page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741625763a2389070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_tax()
. If it's the post type archive page, then the post type would need to have thehas_archive
set to true, then useis_post_type_archive( 'al_product' )
. Is archive enabled for your CPT? – Sally CJ Commented Apr 8, 2021 at 4:24if(is_tax( 'product_category' )):
If you post as an answer. I'll check it! Thanks. @SallyCJ – rudtek Commented Apr 8, 2021 at 4:29