admin管理员组文章数量:1392101
I'm trying to get a link from a custom post with a custom taxonomy but I'm running into issues trying to get it. I'm wanting to send the user straight to the post if the taxonomy count is 1. If it's greater than one post, it goes to a page showing all the posts of the taxonomy. I have this second part working, but I can't get the first bit to work, I can't get the taxonomy to return the post.
<?php
$taxonomy = 'treatment_type';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
//print_r($terms );
if ( $terms && !is_wp_error( $terms ) ) :?>
<?php foreach ( $terms as $term ) {
if('trending-treatment' !== $term->slug && 'skin-care' !== $term->slug){ ?>
<?php if($term->count == 1){?>
<?php
$posts_array = get_posts(
array( 'showposts' => -1,
'post_type' => 'treatment',
'tax_query' => array(
array(
'taxonomy' => 'treatment_type',
'field' => 'term_id',
'terms' => $term->slug,
)
)
)
);
print_r( $posts_array );
?>
<h1>only 1</h1>
<?php print_r($term); ?>
<article class="portfolio-item pf-rejuv">
<div class="portfolio-image">
<a href="POST LINK TO GO HERE">
<img src="<?php the_field('image', $term); ?>" alt="<?php echo $term->name; ?>">
</a>
</div>
<div class="portfolio-desc">
<div class="team-title"><h4><?php echo $term->name; ?></h4><span> <?php the_field('types_of_treatments', $term); ?></span></div>
</div>
</article>
<?php }else{ ?>
<article class="portfolio-item pf-rejuv">
<div class="portfolio-image">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<img src="<?php the_field('image', $term); ?>" alt="<?php echo $term->name; ?>">
</a>
</div>
<div class="portfolio-desc">
<div class="team-title"><h4><?php echo $term->name; ?></h4><span> <?php the_field('types_of_treatments', $term); ?></span></div>
</div>
</article>
<?php } ?>
<?php }
} ?>
<?php endif;?>
I'm trying to get a link from a custom post with a custom taxonomy but I'm running into issues trying to get it. I'm wanting to send the user straight to the post if the taxonomy count is 1. If it's greater than one post, it goes to a page showing all the posts of the taxonomy. I have this second part working, but I can't get the first bit to work, I can't get the taxonomy to return the post.
<?php
$taxonomy = 'treatment_type';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
//print_r($terms );
if ( $terms && !is_wp_error( $terms ) ) :?>
<?php foreach ( $terms as $term ) {
if('trending-treatment' !== $term->slug && 'skin-care' !== $term->slug){ ?>
<?php if($term->count == 1){?>
<?php
$posts_array = get_posts(
array( 'showposts' => -1,
'post_type' => 'treatment',
'tax_query' => array(
array(
'taxonomy' => 'treatment_type',
'field' => 'term_id',
'terms' => $term->slug,
)
)
)
);
print_r( $posts_array );
?>
<h1>only 1</h1>
<?php print_r($term); ?>
<article class="portfolio-item pf-rejuv">
<div class="portfolio-image">
<a href="POST LINK TO GO HERE">
<img src="<?php the_field('image', $term); ?>" alt="<?php echo $term->name; ?>">
</a>
</div>
<div class="portfolio-desc">
<div class="team-title"><h4><?php echo $term->name; ?></h4><span> <?php the_field('types_of_treatments', $term); ?></span></div>
</div>
</article>
<?php }else{ ?>
<article class="portfolio-item pf-rejuv">
<div class="portfolio-image">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<img src="<?php the_field('image', $term); ?>" alt="<?php echo $term->name; ?>">
</a>
</div>
<div class="portfolio-desc">
<div class="team-title"><h4><?php echo $term->name; ?></h4><span> <?php the_field('types_of_treatments', $term); ?></span></div>
</div>
</article>
<?php } ?>
<?php }
} ?>
<?php endif;?>
Share
Improve this question
edited Feb 5, 2020 at 11:50
Sally CJ
40.3k2 gold badges29 silver badges50 bronze badges
asked Feb 4, 2020 at 16:53
monkeyman905monkeyman905
398 bronze badges
2
|
3 Answers
Reset to default 2I can't get the first bit to work, I can't get the taxonomy to return the post.
So the following is the first bit which queries posts in a single taxonomy term:
$posts_array = get_posts(
array( 'showposts' => -1,
'post_type' => 'treatment',
'tax_query' => array(
array(
'taxonomy' => 'treatment_type',
'field' => 'term_id',
'terms' => $term->slug,
)
)
)
);
And the problem there is that the tax_query
's field
and terms
values don't match — you set the field
to term_id
(term ID), but then the terms
is a term slug and not ID.
So make sure they match:
Change the
'field' => 'term_id'
to'field' => 'slug'
.Or change the
'terms' => $term->slug
to'terms' => $term->term_id
.
And actually, you should use the parameter numberposts
or better posts_per_page
and not showposts
(which is not a standard parameter for get_posts()
or WP_Query
).
Here is my working code brother you can try this.
$terms = get_terms( array(
'taxonomy' => 'market-place',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
?>
<label class="cont"><?php echo $terms_row->name;?>
<input type="checkbox" <?php if(in_array($terms_row->slug, $market_place_array)){ echo "checked"; } ?> class="filter" value = "<?php echo $terms_row->slug;?>" id="<?php
echo $terms_row->slug;?>" name="products_market">
<span class="checkmark"></span>
</label>
<?php
}
Thanks
This should work...
$query = new WP_Query( $args );
$count = $query->found_posts; // will return number of posts i your query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
if($count == 1) { // if there's just one post, let's redirect to it
$url = get_the_permalink();
wp_redirect( $url );
exit;
} else { // more than one post
// normal loop stuff here
}
}
} else {
// Do this is there are no posts at all
}
本文标签: phpTrying to get custom post of a custom taxonomy
版权声明:本文标题:php - Trying to get custom post of a custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775062a2624574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'field' => 'term_id',
should be'field' => 'slug',
. Or that the'terms' => $term->slug
should be'terms' => $term->term_id
. – Sally CJ Commented Feb 4, 2020 at 22:39