带有嵌套If Else的POST类型的Foreach循环未完成进程

时间:2020-03-25 作者:Robert L

当我的插件更新时,我正在尝试向数据库写入新值。我正在浏览帖子类型,如果是页面产品或帖子,我想将索引放在DB中。否则,我想在数据库中放置NO-INDEX。索引部分正确循环,或者至少是我能说的最好的循环,因为值存储在DB中。但当它到达NO-INDEX时,它处理第一个帖子类型,即附件,然后不获取或处理其余的帖子类型。我尝试了代码的几种变体,但似乎无法使其正常工作。我做错了什么?

我正在使用add\\u选项和register\\u设置

代码如下:

function set_activation_value(){
    // Sets Field Defaults
    $option = get_option(\'ews_index_option_name\');
    if (empty($option)) {
        $args = array (
            \'public\' => true
        ); 
        $post_types = get_post_types( $args, \'names\' ); 
        $my_options = get_option(\'ews_index_option_name\');
        $post_type_output = \'\';
        foreach ( $post_types as $post_type ) {
            if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
                $my_options = get_option(\'ews_index_option_name\');
                $my_options["$post_type"] = \'index\';
                update_option(\'ews_index_option_name\', $my_options);
            }
            else if (($post_type != "page") && ($post_type != "product") && ($post_type != "post")) {
                $my_options = get_option(\'ews_index_option_name\');
                $my_options["$post_type"] = \'no-index\';
                update_option(\'ews_index_option_name\', $my_options);
            }
            wp_reset_postdata();
            $post_type_output .= $post_type;
        }
        update_option(\'ews_index_my_types\', $post_types);
        update_option(\'ews_index_option_var\', $post_type_output);
    }
}
register_activation_hook( __FILE__, \'set_activation_value\' );
运行更新时,我得到以下结果:enter image description here

我希望得到的是:enter image description here

数据库还显示与这些图片所代表的数据相同的数据。我还添加了其他帖子类型,以检查和在else上,或者如果else无法通过附件。

我还尝试了以下代码,但没有成功:

foreach ( $post_types as $post_type ) {
    if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
        $my_options = get_option(\'ews_index_option_name\');
        $my_options["$post_type"] = \'index\';
        update_option(\'ews_index_option_name\', $my_options);
    } else {
        $my_options = get_option(\'ews_index_option_name\');
        $my_options["$post_type"] = \'no-index\';
        update_option(\'ews_index_option_name\', $my_options);
    }
}
任何帮助都将不胜感激,在这方面已经有一段时间了。我不太擅长循环和数组,这在这里可能很明显。非常感谢。

1 个回复
SO网友:disinfor

让我们看看这是否有效。我无法真正测试这一点,但您的代码中有一些东西需要更新:

function set_activation_value(){
    // Sets Field Defaults
    $option = get_option(\'ews_index_option_name\');
    // get_option returns FALSE if it doesn\'t exist, so we check that here.
    if ( $option === FALSE ) {

        $args = array (
            \'public\' => true
        );

        $post_types = get_post_types( $args );
        // Since the option doesn\'t exist, we should add it, then update it in the loop.
        add_option(\'ews_index_option_name\');
        // now we have an empty array.
        $my_options = [];
        $post_type_output = \'\';
        foreach ( $post_types as $post_type ) {
            // no need to wrap in parenthesis for each condition.
            if ( $post_type === \'page\' || $post_type === \'product\' || $post_type === \'post\' ) {
                $my_options[ $post_type ] = \'index\';
                update_option(\'ews_index_option_name\', $my_options);
            // Skip the else if, since every other post type is no index.    
            } else {
                $my_options[ $post_type ] = \'no-index\';
                update_option(\'ews_index_option_name\', $my_options);
            }
            $post_type_output .= $post_type;
        }
        // These look to be holding the exact same data on one in an array and the other a string.
        update_option(\'ews_index_my_types\', $post_types);
        update_option(\'ews_index_option_var\', $post_type_output);
    }
}
register_activation_hook( __FILE__, \'set_activation_value\' );
我把它整理了一下并添加了评论。而且,没有理由wp_reset_postdata().

如前所述,我无法测试这一点,但这可能更适合您。