将自定义管理字段回显到is_Single()

时间:2018-01-24 作者:user2115227

我希望你能帮忙!我创建了一个相对简单的管理设置页面,该页面有3个字段:

页面ID、帖子ID、文本框

我想做的是显示HTML 不在页面ID或帖子ID字段中的任何页面或帖子内容顶部的文本框内容。我的代码允许我使用get\\u选项(“field UID”),所以我想我应该使用get\\u选项将post ID定义为一个变量,然后将其插入is\\u single()复选框中,以定义是否回显文本框。

对于单个ID来说,这很好,但是如果我将多个ID放入post ID字段中,并用逗号分隔(例如4519、4133、201),那么它将停止工作,并再次显示所有帖子上的文本框内容。。。我做错了什么?!

下面是我的代码:(add\\u操作位于底部)。

<?php
            class Codeable_Fields_Plugin {
                public function __construct() {
                    // Hook into the admin menu
                    add_action( \'admin_menu\', array( $this, \'create_plugin_settings_page\' ) );
                    // Add Settings and Fields
                    add_action( \'admin_init\', array( $this, \'setup_sections\' ) );
                    add_action( \'admin_init\', array( $this, \'setup_fields\' ) );
                }
                public function create_plugin_settings_page() {
                    // Add the menu item and page
                    $page_title = \'Custom HTML\';
                    $menu_title = \'Custom HTML\';
                    $capability = \'manage_options\';
                    $slug = \'codeable_fields\';
                    $callback = array( $this, \'plugin_settings_page_content\' );
                    $position = 100;
                    add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
                }
                public function plugin_settings_page_content() {?>
                    <div class="wrap">
                        <?php
                        if ( isset( $_GET[\'settings-updated\'] ) && $_GET[\'settings-updated\'] ){
                              $this->admin_notice();
                        } ?>
                        <form method="POST" action="options.php">
                            <?php
                                settings_fields( \'codeable_fields\' );
                                do_settings_sections( \'codeable_fields\' );
                                submit_button();
                            ?>
                        </form>
                    </div> <?php
                }

                public function admin_notice() { ?>
                    <div class="notice notice-success is-dismissible">
                        <p>Your settings have been updated!</p>
                    </div><?php
                }
                public function setup_sections() {
                    add_settings_section( \'our_first_section\', \'Custom HTML\', array( $this, \'section_callback\' ), \'codeable_fields\' );
                }
                public function section_callback( $arguments ) {
                    switch( $arguments[\'id\'] ){
                        case \'our_first_section\':
                            echo \'\';
                            break;
                    }
                }
                public function setup_fields() {
                    $fields = array(
                        array(
                            \'uid\' => \'codeable_pages_field\',
                            \'label\' => \'Page IDs to Exclude\',
                            \'section\' => \'our_first_section\',
                            \'type\' => \'text\',
                            \'supplimental\' => \'Seperate by comma\',
                        ),
                         array(
                            \'uid\' => \'codeable_posts_field\',
                            \'label\' => \'Post IDs to Exclude\',
                            \'section\' => \'our_first_section\',
                            \'type\' => \'text\',
                            \'supplimental\' => \'Seperate by comma\',
                        ),

                        array(
                            \'uid\' => \'codeable_textarea\',
                            \'label\' => \'Enter Text here\',
                            \'section\' => \'our_first_section\',
                            \'type\' => \'textarea\',
                        )

                    );
                    foreach( $fields as $field ){
                        add_settings_field( $field[\'uid\'], $field[\'label\'], array( $this, \'field_callback\' ), \'codeable_fields\', $field[\'section\'], $field );
                        register_setting( \'codeable_fields\', $field[\'uid\'] );
                    }
                }
                public function field_callback( $arguments ) {
                    $value = get_option( $arguments[\'uid\'] );
                    if( ! $value ) {
                        $value = $arguments[\'default\'];
                    }
                    switch( $arguments[\'type\'] ){
                        case \'text\':
                        case \'password\':
                        case \'number\':
                            printf( \'<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />\', $arguments[\'uid\'], $arguments[\'type\'], $arguments[\'placeholder\'], $value );
                            break;
                        case \'textarea\':
                            printf( \'<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>\', $arguments[\'uid\'], $arguments[\'placeholder\'], $value );
                            break;
                        case \'select\':
                        case \'multiselect\':
                            if( ! empty ( $arguments[\'options\'] ) && is_array( $arguments[\'options\'] ) ){
                                $attributes = \'\';
                                $options_markup = \'\';
                                foreach( $arguments[\'options\'] as $key => $label ){
                                    $options_markup .= sprintf( \'<option value="%s" %s>%s</option>\', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
                                }
                                if( $arguments[\'type\'] === \'multiselect\' ){
                                    $attributes = \' multiple="multiple" \';
                                }
                                printf( \'<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>\', $arguments[\'uid\'], $attributes, $options_markup );
                            }
                            break;
                        case \'radio\':
                        case \'checkbox\':
                            if( ! empty ( $arguments[\'options\'] ) && is_array( $arguments[\'options\'] ) ){
                                $options_markup = \'\';
                                $iterator = 0;
                                foreach( $arguments[\'options\'] as $key => $label ){
                                    $iterator++;
                                    $options_markup .= sprintf( \'<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>\', $arguments[\'uid\'], $arguments[\'type\'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator );
                                }
                                printf( \'<fieldset>%s</fieldset>\', $options_markup );
                            }
                            break;
                    }
                    if( $helper = $arguments[\'helper\'] ){
                        printf( \'<span class="helper"> %s</span>\', $helper );
                    }
                    if( $supplimental = $arguments[\'supplimental\'] ){
                        printf( \'<p class="description">%s</p>\', $supplimental );
                    }
                }
            }
            new Codeable_Fields_Plugin();


            add_action (\'the_content\', \'add_to_header\');
            function add_to_header(){

                $postids = get_option(\'codeable_posts_field\');

                if (is_single(array( $postids ))) {
                    echo \'\';
                }
                elseif (is_single()) {
                   echo get_option(\'codeable_textarea\');
                }  

            } 
它似乎还去掉了我添加到文本框中的任何HTML标记,我想让它们作为HTML阅读并相应地显示在前端。(例如,如果我添加文本,它将在前端显示为文本,并用div包装。)。

1 个回复
最合适的回答,由SO网友:janh 整理而成

$postids = get_option(\'codeable_posts_field\');
这将为您提供一个逗号分隔的post ID字符串(根据您的代码,如果用户遵循说明)。

array( $postids )
将其转换为一个数组,其中该字符串是唯一的元素。这对我没用is_single(), 显然,因为is_single 不进行任何进一步的拆分。

下面是如何使用preg_split. 代替

$postids = get_option(\'codeable_posts_field\');
使用

$postids = preg_split("/\\s*,\\s*/", get_option(\'codeable_posts_field\'));
preg_split 使用正则表达式将字符串拆分为数组。我添加了\\s* 逗号前后,以便123,456 以及123, 456123 , 456.

具有$postids 作为一个数组,您的检查应该可以正常工作。如果不行,试试看

var_export($postids);
查看$postids真正包含的内容。

结束

相关推荐

Admin-Ajax Error

我在本地主机上制作主页。我需要wp_query 使用Ajax。但有一些错误。我不知道为什么。你能帮帮我吗?---->这是load_more_ajax.jsvar page = 2; var date_pass = \"<?php echo($date_filter);?>\"; var compare_pass = \"<?php echo($compare);?>\"; var ajaxurl = \"<?php echo admin_url(