首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按字母顺序排列填充的GravityForms列表

按字母顺序排列填充的GravityForms列表
EN

WordPress Development用户
提问于 2019-04-02 02:07:06
回答 1查看 427关注 0票数 0

希望有人能帮助新手,因为我不知道该怎么做。

我的functions.php中有下面的代码,它填充了产品的GravityForms表单“复选框列表”。用户使用复选框在网站上选择GF表单中的产品。每个被拖到复选框列表中的产品都是一个单独的WordPress帖子。

我很难按字母顺序对复选框进行排序,这样用户就可以很容易地在100+不同的产品(帖子)中找到产品。

我找不到任何其他论坛的帖子看起来有点相似,这样我就可以自己想出如何分类了。

第一部分是按字母顺序排列复选框,

我需要的第二部分是排除两个贴上“系统”标签的员额,或者排除这两个员额,因为它们被归类为“系统”员额。

非常感谢,

安德烈

代码语言:javascript
复制
/** 
 * Add Gravity Forms function to dynamically populate customer 
 * letter checkboxes
 * applicious
 */
//NOTE: update the '1' to the ID of your form
add_filter( 'gform_pre_render_1', 'populate_checkbox' );
add_filter( 'gform_pre_validation_1', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_1', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_1', 'populate_checkbox' );
function populate_checkbox( $form ) {

    foreach( $form['fields'] as &$field )  {

        //NOTE: replace 3 with your checkbox field id
        $field_id = 3;
        if ( $field->id != $field_id ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retrieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts
        $posts = get_posts( 'numberposts=-1&post_status=publish' );

        $input_id = 1;
        foreach( $posts as $post ) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }

            $choices[] = array( 'text' => $post->post_title, 'value' => $post->ID );
            $inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );

            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;

    }

    return $form;
}
EN

回答 1

WordPress Development用户

发布于 2019-04-02 03:39:24

我想你想看看这句话,除非我弄错了:

代码语言:javascript
复制
$posts = get_posts( 'numberposts=-1&post_status=publish' );

您应该能够添加orderbyorder参数:

代码语言:javascript
复制
$posts = get_posts( 'numberposts=-1&post_status=publish&order=asc&orderby=title' );
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/333173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档