[英]Wordpress - How do I display post content html in my page?
I'm new to WordPress I am trying to display the first 10 posts in the database with a shortcode with this code.我是 WordPress 的新手,我正在尝试使用带有此代码的简码显示数据库中的前 10 个帖子。 It's just an experiment to learn.
这只是一个学习的实验。
function do_hello_world()
{
global $wpdb;
$result = $wpdb->get_results('SELECT post_content FROM wp_posts LIMIT 10');
$content = "";
for ($i = 0; $i < count($result); $i++) {
$content = $content . $result[$i];
}
return $content;
}
add_shortcode('hello_world', 'do_hello_world');
But I get the following error on my page when the shortcode is added.但是当添加简码时,我的页面上出现以下错误。
Notice: Array to string conversion in D:\Work\DGS\Cam_Rent\Site\wp-includes\shortcodes.php on line 325 Array
注意:数组到字符串的转换在 D:\Work\DGS\Cam_Rent\Site\wp-includes\shortcodes.php 在第 325 行数组
I checked in the database and post_content is a long text filled with HTML.我检查了数据库,post_content 是一个用 HTML 填充的长文本。 Shouldn't this code make a string?
这段代码不应该是一个字符串吗? My goal is to display the HTML from those posts on my page, how would I do that?
我的目标是从我页面上的这些帖子中显示 HTML,我该怎么做?
As said Mohammad Ashique Ali , it's better to not use directly wpdb, there is a lot of wordpress functions like wp_posts
:正如Mohammad Ashique Ali所说,最好不要直接使用 wpdb,有很多 wordpress 函数,如
wp_posts
:
https://codex.wordpress.org/Function_Reference/get_posts https://codex.wordpress.org/Function_Reference/get_posts
try this:尝试这个:
add_shortcode("hello_world", function ($attr, $content, $tag) {
$posts = get_posts([
"post_type" => "post",
"posts_per_page" => 10,
]);
$result = "";
foreach ($posts as $post) {
$result .= $post->post_content . "<hr/>";
}
return $result;
});
You can use Insert PHP Code Snippet plugin.您可以使用插入 PHP 代码片段插件。
Plugin link: https://wordpress.org/plugins/insert-php-code-snippet/插件链接: https://wordpress.org/plugins/insert-php-code-snippet/
I checked in the database and post_content is a long text filled with HTML.
我检查了数据库,post_content 是一个用 HTML 填充的长文本。 Shouldn't this code make a string?
这段代码不应该是一个字符串吗?
No.不。
If you use the print_r() function to see what the value of $result
is, you'll get something like this:如果您使用print_r() function 来查看
$result
的值是什么,您将得到如下信息:
Array
(
[0] => stdClass Object
(
[post_content] => <!-- wp:paragraph -->
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>
<!-- /wp:paragraph -->
)
...
)
An array of objects .对象数组。
The reason why you're getting that PHP warning is because you're trying to concatenate a string ( $content
) with an object ( $result[$i]
, which is a stdClass Object
):您收到 PHP 警告的原因是因为您试图将字符串(
$content
)与 object ( $result[$i]
,这是一个stdClass Object
)连接起来:
$content = $content . $result[$i];
To access the actual content from your posts (and fix the problem), change that line to:要访问帖子中的实际内容(并解决问题),请将该行更改为:
$content = $content . $result[$i]->post_html;
Notice how now we're using the post_html
property of the object to retrieve the HTML of the post.请注意,现在我们如何使用 object 的
post_html
属性来检索帖子的 HTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.