php 使用“自定义帖子类型”填充表单域

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用“自定义帖子类型”填充表单域相关的知识,希望对你有一定的参考价值。

<?php

// The 4 on these add_filters is the ID of the form you want to populate
add_filter( 'gform_pre_render_4', 'populate_posts' );
add_filter( 'gform_pre_validation_4', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_4', 'populate_posts' );
add_filter( 'gform_admin_pre_render_4', 'populate_posts' );

function populate_posts( $form ) {

    foreach ( $form['fields'] as &$field ) {
	
				// Change the field type to the desired type and 'poppulate-posts' needs to be the class
				// of the field you want populated.
        if ( $field->type != 'multiselect' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retrieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts
        // The post_type should be the CPT slug. You can add other parameters if you need to narrow down
        $posts = get_posts( 'post_type=job_listings' );

        $choices = array();

        foreach ( $posts as $post ) {
            $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
        }

        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select a Position';
        $field->choices = $choices;

    }

    return $form;
}

以上是关于php 使用“自定义帖子类型”填充表单域的主要内容,如果未能解决你的问题,请参考以下文章

php 自定义帖子类型的搜索表单

php 使用自定义帖子类型获取自定义WordPress菜单

php 使用自定义帖子类型获取自定义WordPress菜单

php 使用WP状态自定义帖子类型。

php 如何使用自定义帖子类型存档作为首页

php 使用模板注册新的自定义帖子类型