在wp_query上没有显示帖子

时间:2015-12-23 16:07:20

标签: php wordpress custom-post-type

我正在为wordpress设置自定义帖子类型,现在我正在尝试在主页上显示帖子。我想隐藏帖子,直到通过has_tag调用它我使用以下代码,但它显示所有帖子。

$args = array( 'post_type' => 'homepage', 'posts_per_page' => -1 );
$loop = new WP_Query( $args ); 

while ( $loop->have_posts() ) : 
    $loop->the_post();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;

1 个答案:

答案 0 :(得分:0)

因此,我可以使用以下代码完成此任务。

<?php

$args = array(
'post_type' => 'homepage',
);
$the_query = new WP_Query( $args ); ?>

<?php
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (has_tag('footer')) {
echo '<li>' . get_the_title() . '</li>';
}
}
echo '</ul>';
} rewind_posts();




// The Loop
if ( $the_query->have_posts() ) {

while ( $the_query->have_posts() ) {
$the_query->the_post();
if (has_tag('cta')) {
echo '<div class="cta-style">' . get_the_title() . '</div>';
}
}

}  ?>
相关问题