因此,我正在制作一个短代码,并尝试集成vue+wp\\u查询,当我在同一页上使用具有不同属性的多个短代码时,很难使其正常工作。我正在使用插件“简单即时搜索”的代码作为基础,并且仍在学习。
我想我可以对div id和wp\\u localize\\u脚本使用uniqid(),然后在脚本文件中使用它。然而,div中生成的id与js中生成的id不同-vfm_gallery.block_id
.
<?php
if (!class_exists(\'myclass\')){
class myclass {
/**
* Class Constractor
*/
function __construct() {
//add shortcode
add_shortcode(\'gallery-grid\', array($this,\'display_gallery_shortcode\'));
//ajax handler
add_action( \'wp_ajax_nopriv_vfm_gallery\', array($this, \'ajax_search\') );
add_action( \'wp_ajax_vfm_gallery\', array($this, \'ajax_search\') );
add_action(\'wp_enqueue_scripts\',array($this,\'add_scripts_and_styles\'));
}
/**
* shortcode to display search form and results Div
* @return string - search form and results Div
*/
public function display_gallery_shortcode(){
return \'<div id="\' . $this->gallery_block_id() . \'">
<transition name="fade">
<div class="loader-overlay" v-if="loading">
<div class="loader"></div>
<h2>Fetching data</h2>
</div>
</transition>
<transition name="slide-fade" appear>
<div v-if="!loading">
<ul>
<li v-for="post in posts">
{{ post.title }}
</li>
</ul>
</div>
</transition>
</div>\';
}
/**
* ajax search function
* @return array() with title,content JSON
*/
public function ajax_search(){
$latest = new WP_Query( array (
\'post_type\' => \'gallery\',
\'posts_per_page\' => \'9\',
\'cat\' => \'\',
));
$posts = $latest->posts;
// Check if any posts were found.
if ( ! $latest->post_count ){
echo json_encode(array());
die();
}
//Create an array with the results
$results=array();
foreach ( $posts as $post ) {
$video_url = $post->post_content;
$title = trim( strip_tags( $post->post_title ) );
$image = get_the_post_thumbnail( $post->ID, \'thumbnail\' );
$url = get_permalink($post->ID);
$results[] = array(
\'title\' => $title,
\'video_url\' => $video_url,
\'image\' => $image,
\'url\' => $url
);
}
//using JSON to encode the array
echo json_encode($results);
die();
}
/**
* Void function to add needed scripts and styles.
*/
public function add_scripts_and_styles(){
wp_enqueue_script( \'vfm-gallery\', plugin_dir_url(dirname(dirname(__FILE__))) . \'js/gallery.js\', array( \'jquery\' ), null, true );
wp_localize_script( \'vfm-gallery\', \'vfm_gallery\', array(
\'ajaxUrl\' => admin_url( \'admin-ajax.php\' ),
\'nonce\' => wp_create_nonce(\'vfmg\'),
\'block_id\' => $this->gallery_block_id()
));
}
static function gallery_block_id(){
return \'vfm_gallery_\' . uniqid();
}
}//end class
}//end if
new myclass();
var postsList = new Vue({
el: "#" + vfm_gallery.block_id,
data: {
posts: [],
loading: true,
},
created: function () {
this.fetchData();
},
methods: {
fetchData: function () {
this.loading = true; //start loading
axios.get(vfm_gallery.ajaxUrl, {
params: {
action: \'vfm_gallery\',
nonce: vfm_gallery.nonce
}
})
.then((response) => {
this.loading = false; //stop loading
this.posts = response.data;
})
.catch((error) => {
this.loading = false; //stop loading
console.error(error)
})
}
}
})