繁体   English   中英

如何在 Wordpress 中使用新的 wp_query 实例

[英]How to use new wp_query instance in Wordpress

如何在 Wordpress 的模板文件中使用新的 wp_query 实例。

试过了

$the_query = new WP_Query( $args );
 
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}

看起来您错过了循环中的 the_post() 。

正确的代码

$the_query = new WP_Query( $args );
 
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post(); 
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM