简体   繁体   English

Wordpress - 如何在我的页面中显示帖子内容 html?

[英]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 代码片段插件。

  1. Install the plugin.安装插件。
  2. Then you got a sidebar menu like XYZ PHP Code.然后你会得到一个侧边栏菜单,比如 XYZ PHP 代码。
  3. Add a new snippet and write your code.添加一个新片段并编写您的代码。
  4. Insert this snippet to your page post and publish it.将此片段插入您的页面帖子并发布。

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.

相关问题 如何在点击,wordpress时显示页面内容 - How do I display content of a post to a page on click, wordpress 如何显示一个 wordpress 的页面内容? - How do I display a wordpress page content? 如何在Wordpress中的特定帖子页面上显示内容 - How to display content if on a certain post page in wordpress 如何在我的静态html网站上显示来自Wordpress的一篇(精选)帖子? - How do I display one (featured) post from Wordpress on my static html website? 如何在Wordpress网站的主页上显示HTML标签? - How do I display HTML tags on the main page of my Wordpress site? 如何在wordpress页面上的html表单中包含PHP? - How do I include PHP in my html form on a wordpress page? 如何从帖子页面中的内容显示实际的“搜索词”并在wordpress中显示? - How can I show the actual “search term” from the content in the post page and display it in wordpress? 如何将SVG文件内容直接放在HTML中以在WordPress页面中显示 - How to directly place SVG file content in the HTML to display it in a WordPress page 如何从插件中获取内容以在Wordpress模板中显示我想要的位置 - How do I take content from inside a plugin to display where I want in my Wordpress template WordPress-如何在使用category.php的页面中显示自定义帖子类型 - WordPress - How do I display custom post type in page that uses category.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM