Gform_After_Submit内容紧跟在<Body>之后,而不是在帖子正文中

时间:2013-04-15 作者:Madebymartin

我有以下函数,它成功地将重力表单(ID 18)中的表单字段(ID 13和18)用作wp\\u查询中的参数。这将显示具有特定分类术语的帖子列表,这些术语与表单中选择的术语相匹配:

add_action("gform_after_submission_18", "set_post_content", 10, 2);
function set_post_content($entry, $form){

    //getting post
    $post = get_post($entry["post_id"]);
    $concern = $entry[13];
    $sensitivity = $entry[18];

    $loop = new WP_Query( array( 
        \'post_type\' => \'recommended\',    
        \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'concern\',
            \'field\' => \'slug\',
            \'terms\' => $concern
        ),
        array(
            \'taxonomy\' => \'sensitivity\',
            \'field\' => \'slug\',
            \'terms\' => $sensitivity
        )
        ),

        \'orderby\' => \'title\',
        \'posts_per_page\' => \'-1\', 
        \'order\' => \'ASC\'
      ) 
   ); ?>

//THIS BIT DISPLAYS THE CORRECT LOOP BUT IT APPEARS IMMEDIATELY AFTER <BODY> RATHER THAN IN THE POST CONTENT
   <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
   <li><?php the_title(); ?></li>
   <?php endwhile; ?></ul>
   <?php 


//THIS BIT DISPLAYS THE CHOSEN FORM FIELD INSIDE MY POST CONTENT - WRONG CONTENT, RTIGHT POSITION
 $post->post_content = 
"<ul>" . 
$concern . 
"<br/> sensitivity: " . 
$sensitivity . 
" </ul>"
;

 }
问题是,这会立即插入到“body”标记之后,而不是插入到post body中。我该怎么把它移到应该在的地方?!我很可能把这一切都搞错了,我被难住了

2 个回复
SO网友:webaware

只是想在周五晚上(如此未经测试),但是:

您正在将结果直接输出到页面。当然,它将直接显示在body标记之后。您需要做的是存储该HTML片段,并按照s\\u ha\\u dum的建议使用\\u内容过滤器将其附加/插入到内容中。

这是未经测试的,并且在几个Pernod之后键入,但是:

class wpse_95891 {

    protected $post_content;

    public function __construct() {
        add_action("gform_after_submission_18", array($this, "set_post_content"), 10, 2);
        add_filter("the_content", array($this, "the_content"));
    }

    /**
    * @param array $entry the Gravity Forms entry/lead "object"
    * @param array $form the Gravity Forms form "object"
    */
    public function set_post_content($entry, $form) {
        // insert all of your query logic here...

        // grab the output and store in a field for later
        ob_start();
        ?>
        <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li><?php the_title(); ?></li>
        <?php endwhile; ?>
        </ul>
        <?php
        $this->post_content = ob_get_clean();
    }

    /**
    * @param string $the_content post/page content
    * @return string
    */
    public function the_content($content) {
        if (!empty($this->post_content)) {
            $content .= post_content;
        }

        return $content;
    }
}

new wpse_95891();

SO网友:Madebymartin

感谢您的所有意见。。我只是设法实现了我所需要的(非常感谢WPMU的Alex!),使用全局声明-

可能不是最优雅的解决方案,但它满足了我的需要。这实际上是一个非常简化的版本,我正在做什么,这样我知道如何使它工作!

如果有任何问题,请指出我所做的任何问题!?

add_action("gform_after_submission_18", "recommendations", 10, 2);
function recommendations($entry, $form){

global $concern;
global $sensitivity;

$post = get_post($entry["post_id"]);
$concern = $entry[13];
$sensitivity = $entry[18];

add_filter("the_content", "recommended");
}





function recommended() {

global $concern;
global $sensitivity;

echo \'Here are your recommendations, based on your chosen options: <br>\';
echo \'Sensitivity: \';
echo $sensitivity;

echo \'<br>Concern: \';
echo $concern;
echo \'<br><br>\';


$loop = new wp_query( array( 
    \'post_type\' => \'recommended\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'concern\',
            \'field\' => \'slug\',
            \'terms\' => $concern
        ),
        array(
            \'taxonomy\' => \'sensitivity\',
            \'field\' => \'slug\',
            \'terms\' => $sensitivity
        )
    ),
    \'orderby\' => \'title\',
    \'posts_per_page\' => \'-1\', 
    \'order\' => \'ASC\'
) );



while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="">
<?php the_title(); ?><br>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail("thumb"); } ?>
</div>

<?php endwhile; 
} 

结束