Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查询以短代码显示最新帖子_Php_Wordpress - Fatal编程技术网

Php 查询以短代码显示最新帖子

Php 查询以短代码显示最新帖子,php,wordpress,Php,Wordpress,我正试图编写一个查询来显示我的博客的最新文章,然后作为短代码调用,但我遇到了一些语法错误 代码: function\u post\u query(){ $the_query=new WP_query('posts_per_page=1'); while($the_query->have_posts()):$the_query->the_post(); 回声'; wp_reset_postdata(); } 添加快捷码(“最新发布”、“最新发布”查询); 一旦查询开始工作,我将编辑输出标记。任何

我正试图编写一个查询来显示我的博客的最新文章,然后作为短代码调用,但我遇到了一些语法错误

代码:

function\u post\u query(){
$the_query=new WP_query('posts_per_page=1');
while($the_query->have_posts()):$the_query->the_post();
回声';
wp_reset_postdata();
}
添加快捷码(“最新发布”、“最新发布”查询);

一旦查询开始工作,我将编辑输出标记。任何帮助都将不胜感激

您在执行
语句时启动
,但永远不会结束它。链接标记中也存在一些语法问题。最后,
WP\u Query
接受一个参数数组,而不是字符串:

function newest_post_query() {
    $the_query = new WP_Query( array('posts_per_page' => 1,) );
    while ($the_query -> have_posts()) : $the_query -> the_post();
        echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
    endwhile;  // This was missing
    wp_reset_postdata();
}
add_shortcode('newest_post', 'newest_post_query');
function\u post\u query(){
$the_query=new WP_query(数组('posts_per_page'=>1,);
while($the_query->have_posts()):$the_query->the_post();
回声';
endwhile;//缺少这个
wp_reset_postdata();
}
添加快捷码(“最新发布”、“最新发布”查询);

语法错误是什么?
解析错误:语法错误,在第108行的/home/ourcore/public\html/wp content/themes/portfolio/functions.php中出现意外“}”(代码段中的第5行)。除此之外,我不确定查询是否有效。该错误与查询无关,functions.php的第108行是什么?不,我提到functions.php中的第108行是上面代码段中的第5行。当我删除查询时,没有错误。您在第5行时缺少endwhile。谢谢!这是有道理的。我在这里发布后注意到while循环是打开的。这正如预期的那样工作,但是回显的内容没有被插入到我在页面中调用短代码的地方。你知道为什么吗?这是因为最新的帖子内容需要返回,而不是回应。更新和工作。再次感谢!
function newest_post_query() {
    $the_query = new WP_Query( array('posts_per_page' => 1,) );
    while ($the_query -> have_posts()) : $the_query -> the_post();
        echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
    endwhile;  // This was missing
    wp_reset_postdata();
}
add_shortcode('newest_post', 'newest_post_query');