Gravity prerender taxonomy

时间:2013-10-15 作者:Rizzo

我试图在重力表单中预填充一个类别字段或发布下拉列表。以下是我所拥有的:

<?php

add_filter("gform_pre_render", "gform_prepopluate_populate_books");

//Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_books");

function gform_prepopluate_populate_books($form){
    $posttype = \'books\';
    $taxtype = \'genres\';
    $formid = 5;
    $fieldid = 14);

    //only populating drop down for form id 5
    if($form["id"] != $formid)
       return $form;

    //Reading posts for "Business" category;
    //$posts = query_posts(array(\'post_type\' => array(\'post\', \'movies\')));
   // $posts = query_posts( array( \'post_type\' => $querytype ) );


    $taxonomies=get_taxonomies(array(\'name\' => $taxtype ), \'names\'); 
   // if ( have_posts() ) : while ( have_posts() ) : the_post();

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding post titles to the items array
    if ( $taxonomies ) {
        foreach($taxonomies  as $taxonomy) {
            $items[] = array("value" => $taxonomy->slug, "text" => $taxonomy->name);
        }
    }

    //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
        if($field["id"] == $fieldid ) {            
            $field["choices"] = $items;
    }

}
似乎无法将分类法值填充到下拉列表中。有什么想法吗?

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

浏览网页并修改片段,以防有人感兴趣:

<?php

add_filter("gform_pre_render", "gform_prepopluate_populate_books");
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_books");

function gform_prepopluate_populate_books($form){

    //Grab all Terms Associated with a Specific Taxonomy;
    global $post;
    $taxonomy = \'genres\';
    $formid = 5;
    $fieldid = 14;

    if($form["id"] != $formid)
        return $form;
    $terms = get_terms($taxonomy, \'hide_empty=0&orderby=none\');

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding term names to the items array
    foreach($terms as $term){
        $is_selected = $term->name == "testing" ? true : false;
        $items[] = array("value" => $term->name, "text" => $term->name, "isSelected"=> $is_selected);
    }

    //Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
        if($field["id"] == $fieldid){
            $field["type"] = "select";
            $field["choices"] = $items;
        }

    return $form;
}

结束

相关推荐