我使用短代码处理wordpress自定义帖子类型滑块。我正在使用ajax代码,当我从任何其他页面获取数据时它工作正常但是当我从自定义帖子类型的短代码中获取相同的数据时会产生问题,这些代码是用functions.php写的
there is my functions.php code
add_shortcode( 'speaker-shortcode', 'speakers_shortcode');
function speakers_shortcode( $atts ) { ?>
<section class="sp_slider slider">
<?php
$args = array( 'post_type' => 'speakers', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="slide_div">
<div id="flip_wrap">
<div class="flip_outer">
<div class="vid_flip">
<div class="ab_flpr_inner">
<div class="front_flip">
<div class="sp_thumb">
<?php the_post_thumbnail('full'); ?>
</div>
</div>
<div class="back_flip">
<h2>Watch Video</h2>
</div>
</div>
</div>
</div>
</div>
<div class="slide_cntnt">
<h2><?php the_title(); ?></h2>
<a class="pop_btn" href="<?php the_permalink(); ?>" onClick="popup('<?php echo $post->ID; ?>')">Click Me</a>
<div class="entry-content">
<?php //the_content(); ?>
</div>
</div>
</div>
<?php endwhile;
endif; ?>
</section>
<?php
}
这是我的ajax脚本和弹出窗口
<div class="popup" id="page_content">
<div class="popup_container black_bg">
<a href="javascript:;" onClick="javascript:jQuery('#page_content').hide();" id="close">×</a>
<div id="page_content_sec">
</div>
</div>
</div>
我用于调用ajax的界面页面。代码在这里
<?php
require_once("../../../wp-config.php");
if(isset($_GET['pageid'])) {
$pageid = $_GET['pageid'];
} else {
$pageid = "";
}
//echo $pageid;
$content_post = get_post($pageid);
$content = $content_post->post_content; ?>
<div class="pop_hd">
<h2 style="color: #fff; text-align: center;"> Hafiz<?php echo get_the_title($pageid); ?></span></h2>
<?php
$key_name = get_post_custom_values($key = 'Video_Link');
echo $key_name[0];
?>
<?php get_post_custom_values('Video_Link', $post_id); ?>
</div>
<?php
die;
?>
答案 0 :(得分:1)
你的完整代码在wordpress中使用ajax是不合适的
在worpress functions.php&amp;中使用ajax需要js脚本,无需创建任何新文件来运行ajax响应&amp;不需要在那里包含配置文件
检查以下基本代码以使用ajax。
add_action('wp_ajax_FUNCTION-NAME', 'FUNCTION-NAME'); // Logged-in users
add_action('wp_ajax_nopriv_FUNCTION-NAME', 'FUNCTION-NAME'); // Guest users
function FUNCTION-NAME(){
// your ajax response code.
}
<script> jQuery.ajax({ type: "POST", url: ("<?php echo admin_url( 'admin-ajax.php' ); ?>"), data: ({ action: FUNCTION-NAME, data : data }), success: function (response) { jQuery(".class-name").html(response); // "show/hide" event on response received. } }); </script>