Gform_After_Submit在帖子内容之后添加表单标题列表

时间:2013-06-15 作者:Daniel Foltynek

你能帮我编写代码吗?我只想在每个帖子页面的帖子内容之后添加提交GF表单的所有提交人的列表?每个帖子的页面底部都有一个小的CPT表单,提交的表单会转到自定义帖子类型的公文包页面。我还想显示表单的可点击标题列表(提交者的名称,其中一些字段已通过GF表单合并在一起),以及该创建页面的URL。

提交前的图1:enter image description here

5次成功提交后的帖子,也应该在帖子内容/div之后

图2之后:enter image description here

我想使用的代码如下:

<?php
    add_action("gform_after_submission_7", "set_post_content", 10, 2);
    function set_post_content($entry, $form){

        //getting post
        $post = get_post($entry["post_id"]);

        //adding after the post content - how can I get something like this ?:

       ul 
         li  
             a submitted portfolio URL 
                   div class"some class" 
                   submitted form title 
                  /div 
                /a 
              /li 
            /ul 


        //updating post
        wp_update_post($post);
    }
    ?>

1 个回复
SO网友:GhostToast

为了在表单所在的页面上执行此操作,最好知道该页面的ID。我们假设您有这些信息,并且只想将这些信息嵌入到此页面中。所以在你的gform_after_submission 胡克,你会想要这样的东西:

$id_of_page = 36; // change this

// false means we want all values as array, not single
$array_of_submitters = get_post_meta($id_of_page, \'my_custom_field\', false); 

if(empty($array_of_submitters)){
    // has not yet been made, setup variable
    $array_of_submitters = array();
}

// add new value to array
$array_of_submitters[] = $entry[12]; // whatever field you need

// update!
update_post_meta($id_of_page, \'my_custom_field\', $array_of_submitters);
然后在您的页面中,您只需像这样重复这些内容:

$array_of_submitters = get_post_meta($post->ID, \'my_custom_field\', false);

// make sure it has values first so we don\'t look foolish
if(!empty($array_of_submitters)){
    echo \'<h4>People who did something</h4>\';
    echo \'<ul>\';
        // echo each value out as list
        foreach($array_of_submitters as $submitter){
            echo \'<li>\'.$submitter.\'</li>\'; 
        }
    echo \'</ul>\';
}

结束

相关推荐