gpt4 book ai didi

php - Wordpress - 在页面和帖子之外使用评论系统

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:01 25 4
gpt4 key购买 nike

所以目前我正在使用 pods为日志创建一些单独的页面,填充自定义内容。

现在我想为每个页面使用评论系统,例如:

mydomain.com/podpages/page1
mydomain.com/podpages/page2
mydomain.com/podpages/page3

这不是用 wordpress 创建的页面,所以只需添加 <?php comments_template(); ?>不工作。

有什么办法解决这个问题吗?提前致谢

如有不明之处,请发表评论:)

最佳答案

当评论存储在 WordPress 数据库中时,评论相关的帖子(或页面)的 ID 也被存储。

问题是,您正尝试使用 WordPress 保存评论,但对于它实际上不知道的页面。

那么,我们如何为每个 真实 页面创建一个 WordPress 页面,但只是作为一个表示,以便您的真实页面和WordPress 有相互合作的共同点。

所以,这里的计划是;

  • 在每个“真实”页面的后台加载 WordPress。
  • 查看“真实”页面是否已经存在 WordPress 页面表示
  • 如果没有,就立即创建
  • 让 WordPress 认为我们实际上正在查看表示
  • 继续像往常一样使用 WP 的所有功能和“模板标签”

此代码应位于用于呈现“真实”页面的模板文件开头的某个位置;

include ('../path/to/wp-load.php');

// remove query string from request
$request = preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);

// try and get the page name from the URI
preg_match('#podpages/([a-z0-9_-]+)#', $matches);

if ($matches && isset($matches[1])) {
$pagename = $matches[1];

// try and find the WP representation page
$query = new WP_Query(array('pagename' => $pagename));

if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename
));

if (!$id)
do_something(); // something went wrong
}

// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
}

更新

我不敢相信我是这么傻。下面应该替换外部 if;

中的当前代码
// try and find the WP representation page - post_type IS required
$query = new WP_Query(array('name' => $pagename, 'post_type' => 'page'));

if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename,
'post_author' => 1, // failsafe
'post_content' => 'wp_insert_post needs content to complete'
));
}

// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
// post_type is a must!
wp(array('name' => $pagename, 'post_type' => 'page'));

// set up post
the_post();

P.S 我认为在 pagename 上使用 query_var name 更适合 - 它查询 slug,而不是 slug“路径”。

您还需要在表单中输入名称 redirect_to 和您要重定向到的 URL 的值,,过滤器带有连接到 comment_post_redirect 的函数的重定向,返回正确的 URL。

关于php - Wordpress - 在页面和帖子之外使用评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2913885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com