我正在为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;
因此,我可以通过使用以下代码来完成此任务。
<?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>';
}
}
} ?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句