因此,我能够将每篇帖子的内容保存在一个数组中,这要归功于一个非常好的答案:How to localize value of posts
我只是在这里复制了解决方案:
function register_and_enqueue_script()
{
if(\'myPostType\' == get_post_type() && have_posts()) {
wp_register_script( \'js_script\', plugin_dir_url(__FILE__).\'js/script.js\', array(), \'1.8.5\' );
wp_enqueue_script(\'js_script\');
$myCustomValue = array();
while(have_posts()) {
the_post();
$mypostid = get_the_ID();
$myCustomValue[\'\'+$mypostid] = nl2br(get_post_meta($mypostid, \'custom_value\', true));
} // end while
rewind_posts();
wp_localize_script(\'js_script\', \'myCustomValue\', $myCustomValue);
} // end if
但现在我有一个新问题:我如何确保只有我输入的实际帖子的内容在改变?我需要能够将post\\u id传递给我的JS函数。但是怎么做呢?这是Javascript函数的调用:
function modify_custom_value()
{
return \'<div>
<div>
<input type="input" onkeyup="doFunction();" "/>
</div>
</div>\' ;
}
这是JS中的函数:function doFunction(){
//alter the content of the post
}
这里我确实调用了somefunction():function extend_posts( $content )
{
if ( \'myPostType\' == get_post_type() )
{
if( $meta = get_post_meta( get_the_ID(), \'custom_value\', true ) ) {
$content = $content . implement_custom_value(); //this works fine
$content = $content . modify_custom_value(); //this just dont
}
}
return $content;
}
add_filter( \'the_content\', \'extend_posts\' );
有什么想法吗?很抱歉,我提出了很多问题:/