【问题标题】:PHP WordPress the_content()PHP WordPress the_content()
【发布时间】:2016-03-04 07:28:35
【问题描述】:

我在哪里可以找到数据库中the_content()post_content 获取值的文件?

我想做这个概念,但使用数据库中的其他内容, 到目前为止,我已经向数据库 ex 添加了一个新列。 post_content2 我怎样才能通过使用类似的东西来获得它的价值

 <?php the_content2(); ?>

我尝试查看 post-template.php 并找到了该函数,但没有找到它如何从数据库 post_content 中获取值。

【问题讨论】:

  • the_content() 从数据库中的 posts 表中获取值(来自标题为 content 的列)。
  • 我不确定您在问什么,但我会注意到修改核心 wordpress 代码或核心 wordpress 表几乎总是是个坏主意。对wordpress的任何修改都应该通过插件或主题来完成,否则你要么无法升级wordpress,要么升级会破坏你的代码。
  • 编辑 wordpress 表架构是一个非常糟糕的主意 - 而是使用元字段
  • en-gb.wordpress.org/plugins/advanced-custom-fields 之类的东西可能会为您节省很多精力

标签: php wordpress


【解决方案1】:

从上面的cmets,还有一个更完整的答案:

  1. 不要不要修改核心 WP 表。要么使用post_meta 函数,要么添加一个新表。

  2. the_content() 只是一个方便的函数,直接访问posts 表,content 列。您可以随时通过从数据库中获取值并将其传递给apply_filters('the_content', $my_custom_content); 来执行相同的操作。如果您想编写自己的the_content2() 函数,可以/应该这样做(使用上面提到的post_meta 方法):

    function the_content2() {
        // Global in the current post so we can get the ID
        global $post;   
        // Load your custom content
        $my_content = get_post_meta($post->ID, 'my_custom_content'); 
        // Echo it out (like the_content()) applying 'the_content' filters
        echo apply_filters('the_content', $my_content);
    }
    

如果你想知道如何保存这个内容,那么你可以阅读这篇文章:

How to Create Custom Meta Boxes in WordPress

编辑
值得注意的是,您可以安装的几个不同插件中已经提供了此功能。根据我的经验,Custom Field Suite 是最可靠且易于使用的(但这只是我的看法)。

【讨论】:

  • 非常感谢,但是关于我所做的修改,删除我添加的列/行会完全恢复表吗?
  • 如果这是您对表格所做的唯一修改,那么应该这样做。
猜你喜欢
  • 2015-06-14
  • 1970-01-01
  • 2019-07-06
  • 2015-05-09
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多