当我的插件更新时,我正在尝试向数据库写入新值。我正在浏览帖子类型,如果是页面产品或帖子,我想将索引放在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\' );
运行更新时,我得到以下结果:
数据库还显示与这些图片所代表的数据相同的数据。我还添加了其他帖子类型,以检查和在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);
}
}
任何帮助都将不胜感激,在这方面已经有一段时间了。我不太擅长循环和数组,这在这里可能很明显。非常感谢。