具有高级自定义字段的显示类别多选

时间:2013-03-20 作者:Loxzibit

我正在尝试多选,用户可以在模板中选择他们想要的类别。我想要的功能类似于“选择”字段类型,但仅通过类别来显示这些类别的帖子。

我已经获得了该页面所选选项的多选工作良好和高亮度,但在模板中,值不会使用以下方式显示:

<小时>

<?php var_dump(get_field(\'featured_tags\')); ?>
下面是自定义字段类型的完整代码:

<?php

class acf_Section extends acf_Field
{

    /*--------------------------------------------------------------------------------------
    *
    *   Constructor
    *
    *   @author Elliot Condon
    *   @since 1.0.0
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function __construct($parent)
    {
        parent::__construct($parent);

        $this->name = \'section\';
        $this->title = __("Section",\'acf\');

        // filters (for all fields with choices)
        add_filter(\'acf_save_field-select\', array($this, \'acf_save_field\'));

    }



    /*--------------------------------------------------------------------------------------
    *
    *   create_field
    *
    *   @author Elliot Condon
    *   @since 2.0.5
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function create_field($field)
    {
        // vars
        $defaults = array(
            \'value\'         =>  array(),
            \'multiple\'      =>  0,
            \'allow_null\'    =>  0,
            \'choices\'       =>  array(),
            \'optgroup\'      =>  0,
        );

        $field = array_merge($defaults, $field);


        // no choices
        if(empty($field[\'choices\']))
        {
            echo \'<p>\' . __("No choices to choose from",\'acf\') . \'</p>\';
            return false;
        }


        // multiple select
        $multiple = \'\';
        if( $field[\'multiple\'] )
        {
            // create a hidden field to allow for no selections
            echo \'<input type="hidden" name="\' . $field[\'name\'] . \'" />\';

            $multiple = \' multiple="multiple" size="5" \';
            $field[\'name\'] .= \'[]\';
        }


        // html
        echo \'<select id="\' . $field[\'id\'] . \'" class="\' . $field[\'class\'] . \'" name="\' . $field[\'name\'] . \'" \' . $multiple . \' >\';

        $args = array(
            \'orderby\' => \'name\',
            \'parent\' => 0,
            \'taxonomy\' => \'sections\',
        );

        $categories = get_categories($args);

        $field[\'choices\'] = \'\';

        foreach($categories as $cat) {

            $selected = \'\';
            if(is_array($field[\'value\']) && in_array($cat->term_id, $field[\'value\']))
            {
                // 2. If the value is an array (multiple select), loop through values and check if it is selected
                $selected = \'selected="selected"\';
            }

            echo \'<option value="\'.$cat->term_id.\'" \'.$selected.\'>\'.$cat->name.\'</option>\';
        }

        echo \'</select>\';
    }


    /*--------------------------------------------------------------------------------------
    *
    *   create_options
    *
    *   @author Elliot Condon
    *   @since 2.0.6
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function create_options($key, $field)
    {

    }

    function update_value($post_id, $field, $value)
    {
        // save value
        parent::update_value($post_id, $field, $value);
    }


    /*--------------------------------------------------------------------------------------
    *
    *   pre_save_field
    *   - called just before saving the field to the database.
    *
    *   @author Elliot Condon
    *   @since 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function acf_save_field( $field )
    {
        // vars
        $defaults = array(
            \'choices\'   =>  \'\',
        );

        $args = array(
            \'orderby\' => \'name\',
            \'parent\' => 0,
            \'taxonomy\' => \'sections\',
        );

        $categories = get_categories($args);

        $field = array_merge($defaults, $field);

        foreach($categories as $cat) {
            $field[\'choices\'] .= $cat->term_id.\' : \'.$cat->name."\\n";
        }


        // check if is array. Normal back end edit posts a textarea, but a user might use update_field from the front end
        if( is_array( $field[\'choices\'] ))
        {
            return $field;
        }


        // vars
        $new_choices = array();


        // explode choices from each line
        if( $field[\'choices\'] )
        {
            // stripslashes ("")
            $field[\'choices\'] = stripslashes_deep($field[\'choices\']);

            if(strpos($field[\'choices\'], "\\n") !== false)
            {
                // found multiple lines, explode it
                $field[\'choices\'] = explode("\\n", $field[\'choices\']);
            }
            else
            {
                // no multiple lines!
                $field[\'choices\'] = array($field[\'choices\']);
            }


            // key => value
            foreach($field[\'choices\'] as $choice)
            {
                if(strpos($choice, \' : \') !== false)
                {
                    $choice = explode(\' : \', $choice);
                    $new_choices[trim($choice[0])] = trim($choice[1]);
                }
                else
                {
                    $new_choices[trim($choice)] = trim($choice);
                }
            }
        }



        // update choices
        $field[\'choices\'] = $new_choices;


        // return updated field
        return $field;

    }


    /*--------------------------------------------------------------------------------------
    *
    *   get_value_for_api
    *
    *   @author Elliot Condon
    *   @since 3.1.2
    *
    *-------------------------------------------------------------------------------------*/

    function get_value_for_api($post_id, $field)
    {
        $value = parent::get_value($post_id, $field);

        if($value == \'null\')
        {
            $value = false;
        }

        return $value;
    }

}

?>

1 个回复
SO网友:Andrew Hendrie

我认为Multiple Category Selection 插件将执行您希望执行的操作。无论如何值得一看。。。

结束

相关推荐

具有高级自定义字段的显示类别多选 - 小码农CODE - 行之有效找到问题解决它

具有高级自定义字段的显示类别多选

时间:2013-03-20 作者:Loxzibit

我正在尝试多选,用户可以在模板中选择他们想要的类别。我想要的功能类似于“选择”字段类型,但仅通过类别来显示这些类别的帖子。

我已经获得了该页面所选选项的多选工作良好和高亮度,但在模板中,值不会使用以下方式显示:

<小时>

<?php var_dump(get_field(\'featured_tags\')); ?>
下面是自定义字段类型的完整代码:

<?php

class acf_Section extends acf_Field
{

    /*--------------------------------------------------------------------------------------
    *
    *   Constructor
    *
    *   @author Elliot Condon
    *   @since 1.0.0
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function __construct($parent)
    {
        parent::__construct($parent);

        $this->name = \'section\';
        $this->title = __("Section",\'acf\');

        // filters (for all fields with choices)
        add_filter(\'acf_save_field-select\', array($this, \'acf_save_field\'));

    }



    /*--------------------------------------------------------------------------------------
    *
    *   create_field
    *
    *   @author Elliot Condon
    *   @since 2.0.5
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function create_field($field)
    {
        // vars
        $defaults = array(
            \'value\'         =>  array(),
            \'multiple\'      =>  0,
            \'allow_null\'    =>  0,
            \'choices\'       =>  array(),
            \'optgroup\'      =>  0,
        );

        $field = array_merge($defaults, $field);


        // no choices
        if(empty($field[\'choices\']))
        {
            echo \'<p>\' . __("No choices to choose from",\'acf\') . \'</p>\';
            return false;
        }


        // multiple select
        $multiple = \'\';
        if( $field[\'multiple\'] )
        {
            // create a hidden field to allow for no selections
            echo \'<input type="hidden" name="\' . $field[\'name\'] . \'" />\';

            $multiple = \' multiple="multiple" size="5" \';
            $field[\'name\'] .= \'[]\';
        }


        // html
        echo \'<select id="\' . $field[\'id\'] . \'" class="\' . $field[\'class\'] . \'" name="\' . $field[\'name\'] . \'" \' . $multiple . \' >\';

        $args = array(
            \'orderby\' => \'name\',
            \'parent\' => 0,
            \'taxonomy\' => \'sections\',
        );

        $categories = get_categories($args);

        $field[\'choices\'] = \'\';

        foreach($categories as $cat) {

            $selected = \'\';
            if(is_array($field[\'value\']) && in_array($cat->term_id, $field[\'value\']))
            {
                // 2. If the value is an array (multiple select), loop through values and check if it is selected
                $selected = \'selected="selected"\';
            }

            echo \'<option value="\'.$cat->term_id.\'" \'.$selected.\'>\'.$cat->name.\'</option>\';
        }

        echo \'</select>\';
    }


    /*--------------------------------------------------------------------------------------
    *
    *   create_options
    *
    *   @author Elliot Condon
    *   @since 2.0.6
    *   @updated 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function create_options($key, $field)
    {

    }

    function update_value($post_id, $field, $value)
    {
        // save value
        parent::update_value($post_id, $field, $value);
    }


    /*--------------------------------------------------------------------------------------
    *
    *   pre_save_field
    *   - called just before saving the field to the database.
    *
    *   @author Elliot Condon
    *   @since 2.2.0
    *
    *-------------------------------------------------------------------------------------*/

    function acf_save_field( $field )
    {
        // vars
        $defaults = array(
            \'choices\'   =>  \'\',
        );

        $args = array(
            \'orderby\' => \'name\',
            \'parent\' => 0,
            \'taxonomy\' => \'sections\',
        );

        $categories = get_categories($args);

        $field = array_merge($defaults, $field);

        foreach($categories as $cat) {
            $field[\'choices\'] .= $cat->term_id.\' : \'.$cat->name."\\n";
        }


        // check if is array. Normal back end edit posts a textarea, but a user might use update_field from the front end
        if( is_array( $field[\'choices\'] ))
        {
            return $field;
        }


        // vars
        $new_choices = array();


        // explode choices from each line
        if( $field[\'choices\'] )
        {
            // stripslashes ("")
            $field[\'choices\'] = stripslashes_deep($field[\'choices\']);

            if(strpos($field[\'choices\'], "\\n") !== false)
            {
                // found multiple lines, explode it
                $field[\'choices\'] = explode("\\n", $field[\'choices\']);
            }
            else
            {
                // no multiple lines!
                $field[\'choices\'] = array($field[\'choices\']);
            }


            // key => value
            foreach($field[\'choices\'] as $choice)
            {
                if(strpos($choice, \' : \') !== false)
                {
                    $choice = explode(\' : \', $choice);
                    $new_choices[trim($choice[0])] = trim($choice[1]);
                }
                else
                {
                    $new_choices[trim($choice)] = trim($choice);
                }
            }
        }



        // update choices
        $field[\'choices\'] = $new_choices;


        // return updated field
        return $field;

    }


    /*--------------------------------------------------------------------------------------
    *
    *   get_value_for_api
    *
    *   @author Elliot Condon
    *   @since 3.1.2
    *
    *-------------------------------------------------------------------------------------*/

    function get_value_for_api($post_id, $field)
    {
        $value = parent::get_value($post_id, $field);

        if($value == \'null\')
        {
            $value = false;
        }

        return $value;
    }

}

?>

1 个回复
SO网友:Andrew Hendrie

我认为Multiple Category Selection 插件将执行您希望执行的操作。无论如何值得一看。。。

相关推荐