Wp-theme Development

时间:2016-08-28 作者:Sajidur Rahman

<?php

        query_posts( array(
        \'category_name\'  => \'notice\',
        \'posts_per_page\' => 1
        ) );

        while ( have_posts() ) : the_post();
           echo \'<h1>\',\'<a href="<?php the_permalink(); ?>">\';
            the_permalink();     
            the_title();
           echo \'</a>\',\'</h1>\';

            echo \'<p>\';
             the_content();
           echo \'</p>\';    
        endwhile;

        wp_reset_query();

    ?>
当我点击这个标题时,我的帖子不会显示在单条上。PHP有什么问题?对不起,我英语不好。

1 个回复
最合适的回答,由SO网友:user101775 整理而成

请检查Loop ,然后以正确的方式实例化新Query.

尝试以下操作:

<?php
        // The correct way to create a new Query
        $newquery = new WP_Query( array(
        \'category_name\'  => \'notice\',
        \'posts_per_page\' => 1
        ));

        // Check if query found posts, then start the Loop
        if($newquery->have_posts) : while ($newquery->have_posts() ) : the_post();
           echo \'<h1><a href="\'.get_the_permalink().\'">\'.get_the_title().\'</a></h1>\';

             the_content();

        // End post iteration
        endwhile;

        // If query didnt find any posts
        else:

       echo \'No posts in the query\';

        // End the Loop and reset the query
        endif; wp_reset_query();
?>
如果没有显示任何内容,则应使用以下工具调试查询:

var_dump($newquery);

相关推荐

Wp-query Order By problem

我试图根据他们写入查询的顺序对查询进行排序,但它需要按ID排序。可变的$idpaginas打印:20659206262058520618464197481999118520205882126818753205621277188291855121132但查询后,它会打印:1852018520185511875318829。。。。。function LoopMode($atts, $output = null, $my_query = null, $query_args = null) { /