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');