联系人表单7-使用分类填写选择列表

时间:2013-09-27 作者:Howdy_McGee

使用联系人表单7,是否可以使用自定义分类法值填充DDL(选择列表)?我希望用户能够从我的自定义帖子类型中单击他们想要引用的类别。

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

以下是动态填充内置选项的最新方法,可以轻松扩展以支持更多选项。

/**
 * Dynamic Select List for Contact Form 7
 * @usage [select name taxonomy:{$taxonomy} ...]
 * 
 * @param Array $tag
 * 
 * @return Array $tag
 */
function dynamic_select_list( $tag ) {

    // Only run on select lists
    if( \'select\' !== $tag[\'type\'] && (\'select*\' !== $tag[\'type\']) ) {
        return $tag;
    } else if ( empty( $tag[\'options\'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Loop thorugh options to look for our custom options
    foreach( $tag[\'options\'] as $option ) {

        $matches = explode( \':\', $option );

        if( ! empty( $matches ) ) {

            switch( $matches[0] ) {

                case \'taxonomy\':
                    $term_args[\'taxonomy\'] = $matches[1];
                    break;

                case \'parent\':
                    $term_args[\'parent\'] = intval( $matches[1] );
                    break;

            }
        }

    }

    // Ensure we have a term arguments to work with
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Merge dynamic arguments with static arguments
    $term_args = array_merge( $term_args, array(
        \'hide_empty\' => false,
    ) );

    $terms = get_terms( $term_args );

    // Add terms to values
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

        foreach( $terms as $term ) {

            $tag[\'values\'][] = $term->name;

        }

    }

    return $tag;

}
add_filter( \'wpcf7_form_tag\', \'dynamic_select_list\', 10 );
以下是一种较旧/原始的方法,应被视为过时。

/** Dynamic List for Contact Form 7 **/
/** Usage: [select name term:taxonomy_name] **/
function dynamic_select_list($tag, $unused){ 
    $options = (array)$tag[\'options\'];

    foreach ($options as $option) 
        if (preg_match(\'%^term:([-0-9a-zA-Z_]+)$%\', $option, $matches)) 
            $term = $matches[1];

    //check if post_type is set
    if(!isset($term))
        return $tag;

    $taxonomy = get_terms($term, array(\'hide_empty\' => 0));

    if (!$taxonomy)  
        return $tag;

    foreach ($taxonomy as $cat) {  
        $tag[\'raw_values\'][] = $cat->name;  
        $tag[\'values\'][] = $cat->name;  
        $tag[\'labels\'][] = $cat->name;
    }

    $tag[\'raw_values\'][] = \'Other\';  
    $tag[\'values\'][] = \'Other\';  
    $tag[\'labels\'][] = \'Other - Please Specify Below\';

    return $tag; 
}
add_filter( \'wpcf7_form_tag\', \'dynamic_select_list\', 10, 2);

结束

相关推荐

Echo custom taxonomy slug

我觉得我已经很接近了,但我不知道这是一个synax还是一个可变的东西。我尝试回显slug以自动填充相应的图像。<?php $terms = get_the_terms( $post->id, \'series\', array( \'parent\' => 0 ) ); $terms_slugs = array(); foreach( $terms as $term ) { $terms_slugs[]

联系人表单7-使用分类填写选择列表 - 小码农CODE - 行之有效找到问题解决它

联系人表单7-使用分类填写选择列表

时间:2013-09-27 作者:Howdy_McGee

使用联系人表单7,是否可以使用自定义分类法值填充DDL(选择列表)?我希望用户能够从我的自定义帖子类型中单击他们想要引用的类别。

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

以下是动态填充内置选项的最新方法,可以轻松扩展以支持更多选项。

/**
 * Dynamic Select List for Contact Form 7
 * @usage [select name taxonomy:{$taxonomy} ...]
 * 
 * @param Array $tag
 * 
 * @return Array $tag
 */
function dynamic_select_list( $tag ) {

    // Only run on select lists
    if( \'select\' !== $tag[\'type\'] && (\'select*\' !== $tag[\'type\']) ) {
        return $tag;
    } else if ( empty( $tag[\'options\'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Loop thorugh options to look for our custom options
    foreach( $tag[\'options\'] as $option ) {

        $matches = explode( \':\', $option );

        if( ! empty( $matches ) ) {

            switch( $matches[0] ) {

                case \'taxonomy\':
                    $term_args[\'taxonomy\'] = $matches[1];
                    break;

                case \'parent\':
                    $term_args[\'parent\'] = intval( $matches[1] );
                    break;

            }
        }

    }

    // Ensure we have a term arguments to work with
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Merge dynamic arguments with static arguments
    $term_args = array_merge( $term_args, array(
        \'hide_empty\' => false,
    ) );

    $terms = get_terms( $term_args );

    // Add terms to values
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

        foreach( $terms as $term ) {

            $tag[\'values\'][] = $term->name;

        }

    }

    return $tag;

}
add_filter( \'wpcf7_form_tag\', \'dynamic_select_list\', 10 );
以下是一种较旧/原始的方法,应被视为过时。

/** Dynamic List for Contact Form 7 **/
/** Usage: [select name term:taxonomy_name] **/
function dynamic_select_list($tag, $unused){ 
    $options = (array)$tag[\'options\'];

    foreach ($options as $option) 
        if (preg_match(\'%^term:([-0-9a-zA-Z_]+)$%\', $option, $matches)) 
            $term = $matches[1];

    //check if post_type is set
    if(!isset($term))
        return $tag;

    $taxonomy = get_terms($term, array(\'hide_empty\' => 0));

    if (!$taxonomy)  
        return $tag;

    foreach ($taxonomy as $cat) {  
        $tag[\'raw_values\'][] = $cat->name;  
        $tag[\'values\'][] = $cat->name;  
        $tag[\'labels\'][] = $cat->name;
    }

    $tag[\'raw_values\'][] = \'Other\';  
    $tag[\'values\'][] = \'Other\';  
    $tag[\'labels\'][] = \'Other - Please Specify Below\';

    return $tag; 
}
add_filter( \'wpcf7_form_tag\', \'dynamic_select_list\', 10, 2);

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的