正在尝试获取自定义分类的自定义帖子

时间:2020-02-04 作者:monkeyman905

我试图从一篇带有自定义分类法的自定义帖子中获取链接,但在获取链接时遇到了一些问题。如果分类计数为1,我想直接将用户发送到帖子。如果大于一篇文章,则会转到显示分类法的所有文章的页面。我有这个第二部分工作,但我不能让第一部分工作,我不能让分类法返回帖子。

 <?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;?>

3 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

我无法获得第一点工作,我无法获得分类法以返回帖子。

因此,以下是在单个分类术语中查询帖子的第一位:

$posts_array = get_posts(
    array( \'showposts\' => -1,
        \'post_type\' => \'treatment\',
        \'tax_query\' => array(
            array(
            \'taxonomy\' => \'treatment_type\',
            \'field\' => \'term_id\',
            \'terms\' => $term->slug,
            )
        )
    )
);
问题是tax_query\'sfieldterms 值不匹配-您设置fieldterm_id (术语ID),但terms 是一个术语slug,而不是ID。

因此,请确保它们匹配:

更改\'field\' => \'term_id\'\'field\' => \'slug\'.

或更改\'terms\' => $term->slug\'terms\' => $term->term_id.

实际上,您应该使用参数numberposts 或更好posts_per_page 而不是showposts (这不是get_posts()WP_Query).

SO网友:rajat.gite

这是我的工作代码兄弟,你可以试试这个。

$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
}
谢谢

SO网友:richerimage

这应该有用。。。

$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

}

相关推荐

JQuery未调用php函数

我似乎无法让jquery在插件中调用php处理函数。请帮忙。TQ公司更新:我更新了代码。它现在正在工作。我的php文件// INIT function nizam_scripts() { wp_enqueue_script( \'nizam_ajax\', plugin_dir_url( __FILE__ ) . \'/nizam_plugin.js\', array( \'jquery\' ) ); wp_localize_script(\'nizam_ajax\