这里的代码有效,但是当我添加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;"> </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 ) );
OR
$query = new WP_Query( array( 'post_type' => 'project', 'tag_id' => 34, 'posts_per_page' => 6 ) );
失败。知道如何解决此问题吗?
全部固定-
<?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();
$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
}
}