【问题标题】:Wordpress - How to get tag_id working with post_type?Wordpress - 如何让 tag_id 与 post_type 一起使用?
【发布时间】:2019-10-03 16:10:59
【问题描述】:

这里的代码可以工作,但是当我添加 category_name 或 tag_id(见下文)时 - 它不工作。

$query = new WP_Query( array( 'post_type' => 'project', 'posts_per_page' => 6 ) );

if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>   
        <div>
            <?php the_content(); ?>
            <div style="clear:both; height: 0px; margin: -20px; padding: -20px;">&nbsp;</div>
        </div>
    <?php endwhile; wp_reset_postdata(); ?>

    <!-- show pagination here -->
    <?php else : ?>

    <!-- show 404 error here -->

<?php endif; ?>

一旦我添加了这个 -

$query = new WP_Query( array( 'post_type' => 'project', 'category_name' => 'featured', 'posts_per_page' => 6 ) );

$query = new WP_Query( array( 'post_type' => 'project', 'tag_id' => 34, 'posts_per_page' => 6 ) );

失败了。知道如何解决这个问题吗?

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    全部修复--

    <?php
    // WP_Query arguments
    $args = array(
        'post_type'              => array( 'project' ),
        'posts_per_page'         => '3',
        'order'                  => 'ASC',
        'orderby'                => 'menu_order',
        'tax_query'              => array(
            array(
                'taxonomy'         => 'project_category',
                'terms'            => array( 'featured'),
                'field'            => 'slug'
            ),
        ),
    );
    
    // The Query
    $portfolio_grid_loop = new WP_Query( $args );
    
    // The Loop
    if ( $portfolio_grid_loop->have_posts() ) {
        while ( $portfolio_grid_loop->have_posts() ) {
            $portfolio_grid_loop->the_post();
            the_content();
        }
    } else {
        // no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    

    【讨论】:

      【解决方案2】:
      $args = array( 'post_type' => 'project' , 'orderby' => 'date' ,  'order' => 'DESC' 
                    ,'posts_per_page' => 6,'cat' => '3',  'paged' => query_var('paged'),
      
      ); 
      $q = new WP_Query($args); //'cat' your category ID
      if ( $q->have_posts() ) { 
        while ( $q->have_posts() ) {
          $q->the_post();
          // your loop
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        • 2011-03-25
        • 2018-07-10
        • 2015-12-10
        • 2017-03-25
        相关资源
        最近更新 更多