【问题标题】:Wordpress - use comment-system outside of pages and postsWordpress - 在页面和帖子之外使用评论系统
【发布时间】:2011-02-24 05:27:01
【问题描述】:

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

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

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

这是 不是 使用 wordpress 创建的页面,因此仅添加 <?php comments_template(); ?> 是行不通的。

任何想法如何解决这个问题? 提前致谢

如果有不清楚的地方请发表评论:)

【问题讨论】:

  • 据我所知,这是无法做到的。它与 Wordpress 在数据库中存储内容的方式有关。您为这项工作使用了错误的工具。尝试类似 Disqus 或 Echo。
  • 没有机会。我在这个博客中的评论系统被修改了,所以我必须使用 wordpress 的普通评论系统
  • 如果这些是博客不同页面上的 cmets,我认为没有任何理由坚持使用 wordpress 的评论系统。如果您想针对 WP 用户数据库进行身份验证,这是完全不同的问题,可以轻松完成。您可以订购新的 cmets 系统或自行编程。
  • 当前的 cmets 系统具有特殊字段,通过 WP 用户和与其链接的论坛系统进行身份验证。这就是我使用 Wp-Comment-System 的原因。我还想选择管理 WP 后端的评论。
  • 破解 WP 真的值得付出所有努力吗?是否没有用于 WP 的 Disqus 插件将 cmets 保存到数据库中?这不是 WP 3.0 中的一个功能吗?

标签: php wordpress comments podscms


【解决方案1】:

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

问题是,您正在尝试使用 WordPress 保存 cmets,但是对于一个它实际上并不知道的页面。

那么,我们如何为每个真实页面创建一个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 我认为使用 query_var name 而不是 pagename 更适合 - 它查询 slug,而不是 slug 的“路径”。

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

【讨论】:

  • 所以添加页面有效,但评论模板不显示。我认为 wp(array('pagename' => $pagename));仍然没有效果:(
  • 好的,转储 $wp 表示页面名称已设置,但 cmets 仍然没有显示,还有其他东西(如永久链接)现在应该可以工作,但他们没有,或者?
  • 'post_type' => 'post' 效果更好,但将它们作为页面会好 1000 倍
  • 好的,坚持我最初发布的内容,但将 wp() 调用替换为 $wp_query = new WP_Query(array('pagename' => $pagename))。如果失败,请在之前使用 global $wp_query; 全球化 $wp_query
  • 不,它说“cmets 已关闭”。越来越近。转储查询给了我正确的东西。但仍然没有设置全球$post,但我认为它需要,不是吗?任何想法 ? :(
【解决方案2】:

只需为 wordpress-comment-part 提供一个新 ID - 从您通常的帖子永远无法到达的内容开始(即 100.000+ 是您的页面)

我不确切知道在 wordpress 中它是否是一个函数(即 saveComment ),但如果是这样,只需在您的页面中使用自定义 ID 即可。 不过,您必须自己插入评论表单。

并且不要忘记修改获取消息的查询,即超过 100.000 的 ID 不是条目。

或者您可以编写自己的模板来显示 ID

总结一下,应该不是很难。

p.s.:如果您只想使用 wordpress-login,请使用任何评论系统或自己制作(这是一个 1 小时的事情)并验证/使用 worpress-session。

【讨论】:

    【解决方案3】:

    您需要为此使用 WordPress 吗?如果没有,也许这个 SO 问题中的某些内容会有所帮助:Unobtrusive, self-hosted comments function to put onto existing web pages

    【讨论】:

      【解决方案4】:

      您可以在 wordpress 中创建显示您的日志数据的页面吗?为此,您可能需要一个新模板。然后 WordPress 将有一些东西可以将 cmets 连接到。

      【讨论】:

        【解决方案5】:

        添加

        require('/path/to/wp-blog-header.php');
        

        包含 wp 文件。这应该为您提供所需的所有功能/数据。

        【讨论】:

        • 是的,我做到了。但是 cmets_template();通常只在单个博客或页面帖子中工作。
        • true 是的,您可能需要将每个页面插入到您的帖子表中并调整 cmets_table 函数以为其提供页面的 id
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-13
        • 1970-01-01
        • 2019-04-08
        • 1970-01-01
        • 2018-07-17
        • 2019-07-05
        • 1970-01-01
        相关资源
        最近更新 更多