我正在尝试准备一个带有自定义帖子类型帖子的数组,以填充TinyMCE中的select选项。
我想去拿帖子,拿到他们的身份证和头衔,然后把它们都放进去$data
变量,我想将其发送给TinyMCEwp_localize_script
. 此代码的问题是无法加载。看来我不能打电话了WP_Query
nor使用if(myquery->have_posts()
或while(myquery->have_posts())
, 该页太长,无法完成Allowed memory size of 134217728 bytes exhausted
. 目前,我在CTP中只有2个帖子用于测试,获取数据应该很快。
我通过在插件中创建一个类来实现这一点。
public function __construct() {
add_action(\'admin_head\', array($this, \'prepare_for_tinymce\'));
//add_action(\'admin_head\', array($this, \'gavickpro_add_my_tc_button\'));
}
public function prepare_for_tinymce(){
wp_register_script(\'literatura_reference_js\', plugins_url(\'tinymce.js\', __FILE__ ), array( ), 1.0, true );
// get custom data from WP_QUERY - THIS DOESN\'T WORK
$data = $this->pl_get_books();
/* $data = array( array(
* \'id\' => 1,
* \'title\' => \'asfsdf\'
* ) );
*/
// send data to javascript - THIS WORKS OK (I have tried with $data array above)
wp_localize_script( \'literatura_reference_js\', \'literatura_tinymce\', $data );
wp_enqueue_script(\'literatura_reference_js\');
}
// get data from custom post type
public function pl_get_books(){
$myquery= new WP_Query(
array(
\'post_type\' => \'knjige\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1
));
$data = array();
if($myquery->have_posts()) :
while($myquery->have_posts()) :
$data[] = array(
\'id\' => get_the_ID(),
\'title\' => get_the_title()
);
endwhile;
endif;
wp_reset_query();
return $data;
}
你知道我错过了什么吗?