【问题标题】:Wordpress query with two custom taxonomies具有两个自定义分类法的 Wordpress 查询
【发布时间】:2015-05-11 11:56:37
【问题描述】:

被困在这个问题上好几天了,所以我想我会伸出援手。

我创建了两个自定义分类法,单客户端和双客户端。这些通过高级自定义字段填充特殊内容。

我要做的是创建一个自定义页面模板,它将这两个分类法连接起来并打印出两个自定义分类法的内容。

这适用于我的分类法之一:

<?php 
                $args=array(
                  'post_type' => 'singleclients',
                  'posts_per_page' => -1,
                  'caller_get_posts'=> 1
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <?php the_field('client1_img'); ?>
                    <h3><?php the_field('client1_name'); ?></h3>

                <?php
                  endwhile;
                }
                wp_reset_query();  // Restore global post data stomped by the_post().

            ?>

我已尝试按照 Wordpress Codex 的说明进行操作,但似乎无法正常工作。这是一个我认为可行的示例,但根本没有显示任何内容。

<?php 

                $args=array(
                    'post_type' => 'post',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'singleclients',
                            'field'    => 'slug',
                            'terms'    => 'singleclients'
                        ),
                        array(
                            'taxonomy' => 'dualclients',
                            'field'    => 'slug',
                            'terms'    => 'dualclients'
                        )
                    )
                );
                $posts = get_posts( $args );

                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {

                  while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <?php the_field('client1_img'); ?>
                    <p>print test content</p>
                    <h3><?php the_field('client1_name'); ?></h3>

                <?php
                  endwhile;
                }
                wp_reset_query();  // Restore global post data stomped by the_post().

            ?>

在这一点上真的很沮丧。任何人都知道我做错了什么以及为什么它不会打印出两种分类法?

【问题讨论】:

    标签: php wordpress taxonomy custom-taxonomy


    【解决方案1】:

    不久前我在一个网站上做过这个:

    //queries
    $query1 = new WP_Query($arg1);
    $query2 = new WP_Query($arg2);
    
    //create new empty query
    $wp_query = new WP_Query();
    $wp_query->posts = array_merge( $query1->posts, $query2->posts );
    

    您现在可以像往常一样访问您的帖子。 我不太擅长解释东西,所以如果您需要更多信息,请告诉我...

    【讨论】:

    • 这很昂贵而且效率很低,而且是正确分页的噩梦。
    【解决方案2】:

    很遗憾,我无法让它工作。

    我最终做的是创建一个称为“客户”的单一分类,并为该分类启用类别。这让我可以将“孩子”分配给“单客户端”和“双客户端”类型的客户端。

    当我调用循环时,我调用了一个分类法,然后将输出帖子分成两种类型。我的代码如下。我希望这对将来的某人有所帮助。

    <?php
                    $args=array(
                      'post_type' => 'clients',
                      'posts_per_page' => -1,
                      'caller_get_posts'=> 1
                    );
                    $my_query = null;
                    $my_query = new WP_Query($args);
                    if( $my_query->have_posts() ) {
    
                      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
                        <?php if ( in_category( 'dualclient' )) { ?>
                            <!-- show dual client -->
                            <section class="two columns padding10">
                                <a href="<?php the_permalink() ?>">
                                    <section class="four columns client1 dualclient_first" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
                                    <section class="four columns client1" style="background-image:url('<?php the_field("dualclient2_img") ?>')"></section>
                                </a>
                            </section>
                        <?php } elseif ( in_category( 'singleclient' )) { ?>
                            <!-- show single client -->
                            <section class="two columns padding10">
                                <a href="<?php the_permalink() ?>">
                                    <section class="eight columns client1" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>        
                                </a>
                            </section>
                        <?php } else { ?>
                            <p>Woops.</p>
                        <?php } ?>
    
                    <?php
                      endwhile;
                    }
                    wp_reset_query();  // Restore global post data stomped by the_post().
                ?>
    

    【讨论】:

      【解决方案3】:

      你试过这种方法吗?

      //First Query
      $args=array(
         'taxonomy' => 'singleclients',
         'posts_per_page' => 1,
      );
      query_posts($args);
      
      //Get the Posts
      get_template_part( 'loop', 'index' );
      wp_reset_query();
      
      //Second Query
      $args=array(
         'taxonomy' => 'dualclients',
         'posts_per_page' => 1,
      );
      query_posts($args);
      
      wp_reset_query();
      

      【讨论】:

      • 我想过这样做,但我需要将两个结果混合在一个列表中并按日期排序。
      • 然后您可以存储结果并稍后使用 array_merge 合并/排序它们。您可能必须先创建一个空的 WP_Query...
      • 介意帮我举个例子吗?
      • 永远不要使用query_posts。它破坏了许多插件和其他代码所依赖的主要查询对象。它还破坏了分页
      • 据我所知,query_posts - 虽然不完美,但不会破坏主查询并且不会干扰任何插件。但是,我可能弄错了。介意分享您的经验吗?
      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多