我在自定义分类法中有此代码。php文件。
if ( have_posts() ) :
//do_action( \'generate_archive_title\' );
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_terms( array(
\'taxonomy\' => $taxonomy_name,
\'parent\' => $term_id
));
$secondimg = get_field(\'secondary_img\', $term);
?>
<header class="page-header">
<h1 class="page-title"><?php single_cat_title(); ?></h1>
<?php
if ( ! empty( term_description() ) ) :
printf( \'<div class="taxonomy-description">%s</div>\', term_description() );
endif;
if ( $secondimg ) echo \'<img src="\'.$secondimg[\'url\'].\'" alt="our image">\';
?>
</header>
<?php
if ( $termchildren ) {
// we have kids...just show the terms.
$howmany = count($termchildren);
$i=1;
foreach ( $termchildren as $child ) {
if ($i % 2 == 0) {
echo \'<div class="tax-entry flex \'.$howmany.\'">\';
} else {
echo \'<div class="tax-entry flex \'.$howmany.\'" 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>
<a class="button" href="<?php echo get_term_link( $child, $taxonomy_name ) ;?>">See this Series</a>
</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 = the_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>\';
}
/**
* generate_after_loop hook.
*
* @since 2.3
*/
do_action( \'generate_after_loop\' );
generate_content_nav( \'nav-below\' );
else :
get_template_part( \'no-results\', \'archive\' );
endif;
我想知道是否有一种方法可以更改此行的链接:
<a class="button" href="<?php echo get_term_link( $child, $taxonomy_name ) ;?>">See this Series</a>
因此,如果术语只有一种产品,它将链接到该产品本身。
这可能吗?
最合适的回答,由SO网友:Sally CJ 整理而成
这样做怎么样:
<?php
// Get (at most) 2 "product" posts in the child term.
$posts = get_posts( array(
\'post_type\' => \'product\', // just change the post type if it is not "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; ?>