wordpress: “wp_insert_post()” - 断页

发布于 2024-11-04 13:52:45 字数 584 浏览 1 评论 0原文

当我添加函数“wp_insert_post()”时 - 中断页面。 在数据库“$pages”数组中多次插入相同的数据...仅在中断页面时停止。 为什么?

谢谢 ;]

add_action('save_post', 'save_data_all');

function save_data_all($post_id)
{
    $pages = array(
        'post_title' => 'title',
        'post_content' => 'This is my post.',
        'post_status' => 'publish',
        'post_type' => 'page'
    );

    wp_insert_post($pages);

    if(get_post_meta($post_id, 'l_news', true))
        update_post_meta($post_id, 'l_news', $ser);
    else
        add_post_meta($post_id, 'l_news', $ser, false); 

}

when i add function "wp_insert_post()" - break page.
In database "$pages" array insert many times the same data... stop only when break pages.
Why?

Thanks ;]

add_action('save_post', 'save_data_all');

function save_data_all($post_id)
{
    $pages = array(
        'post_title' => 'title',
        'post_content' => 'This is my post.',
        'post_status' => 'publish',
        'post_type' => 'page'
    );

    wp_insert_post($pages);

    if(get_post_meta($post_id, 'l_news', true))
        update_post_meta($post_id, 'l_news', $ser);
    else
        add_post_meta($post_id, 'l_news', $ser, false); 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-11-11 13:52:45

如果您的 PHP 应用程序出现空白页,则意味着很可能发生致命错误。有时,当抛出不在 try {} catch () {} 块中的异常时也会出现这种情况。

对于您的情况,您需要找出 PHP error_log 所在的位置。最简单的查找方法是使用以下代码创建一个 .php 页面:

如果指令为,请设置它。这是您在 php.ini 中执行的操作。有时,网络托管商还会为您提供进行这些设置的界面。但你并没有分享那么多信息。

因此无论如何,找到 error_log 设置,然后打开日志文件并调查发生的错误。它应该位于或接近文件末尾。

如果您需要更多帮助调试错误,请分享错误消息并发表评论。

If you get a blank page with a PHP application, it means that a most likely fatal error occurred. Sometimes it's also when an exception is thrown which is not in a try {} catch () {}-block.

In your case, you need to figure out where your PHP error_log is located. The easiest way to find out is to create a .php page with the following code: <?php phpinfo();.

If the directive is empty, set it. It's something you do in your php.ini. Sometimes webhosters also provide you with an interface to do these settings. But you didn't share that much information.

So anyway, find the error_log setting and then open the log file and investigate the error which occurred. It should be at or near the end of the file.

If you need more help debugging the error, share the error message and leave a comment.

十二 2024-11-11 13:52:45

在保存帖子之后, update_post_meta 还不知道 $post_id 。在条件中它总是返回 false。

您需要在插入帖子时定义新的 $post_id 。试试这个:

add_action('save_post', 'save_data_all');

function save_data_all()
{
    $pages = array(
        'post_title' => 'title',
        'post_content' => 'This is my post.',
        'post_status' => 'publish',
        'post_type' => 'page'
    );

    $post_id =wp_insert_post($pages);

    if(get_post_meta($post_id, 'l_news', true))
        update_post_meta($post_id, 'l_news', $ser);
    else
        add_post_meta($post_id, 'l_news', $ser, false); 

}

The update_post_meta doesn't know the $post_id yet until AFTER the post is saved. In the conditional it will always return false.

You need to define the new $post_id when your inserting the post. Try this:

add_action('save_post', 'save_data_all');

function save_data_all()
{
    $pages = array(
        'post_title' => 'title',
        'post_content' => 'This is my post.',
        'post_status' => 'publish',
        'post_type' => 'page'
    );

    $post_id =wp_insert_post($pages);

    if(get_post_meta($post_id, 'l_news', true))
        update_post_meta($post_id, 'l_news', $ser);
    else
        add_post_meta($post_id, 'l_news', $ser, false); 

}
痴梦一场 2024-11-11 13:52:45

““$pages”数组多次插入相同的数据...仅在分页时停止。为什么?”

这会导致递归,因为“save_post”钩子在 wp_insert_post() 函数中被触发。所以是的,它会一遍又一遍地发生!

也在 wp_update_post 中被调用(wp_update_post 实际上在内部使用 wp_insert_post )...

""$pages" array insert many times the same data... stop only when break pages. Why?"

This is causing recursion, as the 'save_post' hook is fired in the wp_insert_post() function..so yeah it will happen over and over again!

also gets called in wp_update_post as well (wp_update_post actually uses wp_insert_post internally) ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文