如果 foreach 返回为空,则显示消息

用户18294188

所以我似乎无法弄清楚这个逻辑,所以我想看看是否有人可以帮助我。

所以我有以下内容foreach

echo "Page: " . $page_placement . "/" . $total_pages . "\n\n";
foreach ($posts as $post) {
    $current_post_id = post_exists(
        $post->title->rendered,
        '',
        '',
        '',
        ''
    );
    if ($current_post_id === 0) {
        $my_post = [
            'post_type'     => 'post',
            'post_status'   => 'pending',
            'post_title'    => wp_strip_all_tags($post->title->rendered),
            'post_content'  => wp_strip_all_tags($post->content->rendered),
            'post_excerpt'  => wp_strip_all_tags($post->excerpt->rendered),
            'post_author'   => 1,
            'post_date'     => $post->date,
        ];

        // Insert post.
        $post_id = wp_insert_post($my_post);
        wp_set_object_terms($post_id, 'Global', 'category');
        wp_set_object_terms($post_id, 'Global', 'post_tag');
        echo "ID: " . $post->id . " - Title: " . $post->title->rendered . " has been imported.\n";
    }
}

它抓取 50$posts然后循环并调用post_exists()以检查帖子的标题,如果它不存在,它将导入该帖子,否则它将跳过它。


如果未导入所有 50 个帖子,如何显示回显消息?

我无法if (!$current_post_id) {在 foreach 内部调用,因为它会显示 50 次消息,我尝试在外部播放变量foreach然后调用$my_post[] = [ .... ],但随后wp_import_post不起作用。

肯·李

只需使用一个变量(例如$imported,初始值=0)来检查计数。如果导入,则递增它。

所以最后如果变量还是0,调用echo语句

因此:

<?php
echo "Page: " . $page_placement . "/" . $total_pages . "\n\n";

$imported=0;

foreach ($posts as $post) {


    $current_post_id = post_exists(
        $post->title->rendered,
        '',
        '',
        '',
        ''
    );
    if ($current_post_id === 0) {
        $my_post = [
            'post_type'     => 'post',
            'post_status'   => 'pending',
            'post_title'    => wp_strip_all_tags($post->title->rendered),
            'post_content'  => wp_strip_all_tags($post->content->rendered),
            'post_excerpt'  => wp_strip_all_tags($post->excerpt->rendered),
            'post_author'   => 1,
            'post_date'     => $post->date,
        ];

        // Insert post.
        $post_id = wp_insert_post($my_post);
        wp_set_object_terms($post_id, 'Global', 'category');
        wp_set_object_terms($post_id, 'Global', 'post_tag');
        echo "ID: " . $post->id . " - Title: " . $post->title->rendered . " has been imported.\n";
        $imported++;
    }

}

if ($imported==0){
echo "No record imported";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果记录为空,则返回所有记录并显示消息

* ng如果检查为空,则显示消息

如果数组为空,则使用foreach进行单循环

如果源为空,Parallel.ForEach是否启动线程?

如果变量在foreach循环内为true

如果$ row ['gate']为空,则显示消息“ N / A”

页面加载后显示消息(如果组合框为空)

如果jsf数据表为空,如何显示消息?

如果搜索结果为空,如何显示消息?

如果下拉列表在MVC中为空,如何显示消息?

如果使用react对象数组为空,如何显示消息?

如果显示消息为空,则提交ACF数值

如果图表数据为空,如何显示消息?

如果输入为空,如何将 java 7 迁移到 java 8 - forEach 并返回 List<Object> 或 null?

如果返回的数据在CI中为空,则显示不同的div

如果文本框为空,则返回 true 或显示确认

如果数组为空,则打印空消息

如果值为空,则显示“-”?

如果输入字段为空则显示错误消息,否则没有消息 Vue.js

如果为空,则使用先前的结果在 Foreach 中进行空计数

如果RecyclerView中的列表为空,我想显示带有空消息的吐司

foreach-object如果return,则返回所有值

如果空查询在php中显示消息

如果为空,则返回计数为零

如果foreach循环中的数字,则PHP JSON显示数字

PHP foreach如果isset

如果范围内的单元格为空,则VBA返回消息

如果结果为0,则显示消息

MouseClickEvent如果输入为空,请在继续之前显示消息框