我想知道如何在使用PHP单击按钮时动态地更改div的内容。
通过下面的代码,我可以使用自定义HTML打印出该类别(视频)的帖子,这要感谢"echo“。
foreach ($posts as $post) {
$image = get_post_meta($post->ID, 'thumbnail', true);
$imageData = base64_encode(file_get_contents($image));
$post_date = get_the_date('', $post->ID);
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo '';
echo '';
echo ' ';
echo '' . the_title( "", "" ) . '';
echo '' . $content . '';
echo '' . $post_date . '';
echo '';
echo '';
}
如果我不清楚,请在下面评论。谢谢
更新
我正在尝试从Ajax调用中得到响应,但它不起作用。它返回“错误500”。
$.ajax({
url: '../../wp-content/themes/medical-cure/create_filters.php',
type: "POST",
data: {
action: 'create_posts',
a: a
},
success:function(response) {
console.log(response);
$(".sub-tabs-a").append(response);
},
error: function(e, xhr, opt){
alert("Error requesting " + opt.url + ": " + xhr.status + " " + xhr.statusText);
}
})
<#>UPDATE 2- D.Dan -提示
下面的代码显示了"View“及其”显示“Ajax调用所需的所有帖子:
term_id;
}
$custom_category_id = get_category_id('Video Gallery - HPB');*/
//echo "string";
function create_posts() {
//echo "appena";
try {
$posts = get_posts( [
'numberposts' => 5,
'category' => 81,
'orderby' => 'date',
'order' => 'DESC'
]
);
} catch (Exception $e) {
echo $e;
}
//echo $posts;
$response = '';
//echo "entrato";
foreach ($posts as $post) {
// the_content();
$image = get_post_meta($post->ID, 'thumbnail', true);
$imageData = base64_encode(file_get_contents($image));
$post_date = get_the_date('', $post->ID);
// $post_content = the_post();
$post_content = get_post($post_id);
$content = $post_content->post_content;
$response .= '';
$response .= '';
$response .= ' ';
$response .= '' . the_title( "", "" ) . '';
$response .= '' . $content . '';
$response .= '' . $post_date . '';
$response .= '';
$response .= '';
//$response[] = ['img' => $imageData, 'content' => $content, 'date' => $date];
}
echo $response;
}
?>
下面的代码显示了Ajax调用:
$('.cat-item').click(function(){
// var tab_id = $(this).attr('data-tab');
$('.cat-item').removeClass('current_selected');
// $('.tab-content').removeClass('current');
$(this).addClass('current_selected');
// $("#"+tab_id).addClass('current');
var a = $(".current_selected")[0].innerText;
console.log(a);
// $('.cat-item').click(function() {
// // var search_filter = $(this).find('current_selected').text();
// // console.log(search_filter);
// $(this).load("/wp-content/themes/medical-cure/test.php");
// })
/*$.ajax({
url: '../../wp-content/themes/medical-cure/create_filters.php',
type: "POST",
data: {
action: 'create_posts',
a: a
},
success:function(response) {
console.log(response);
$(".sub-tabs-a").append(response);
},
error: function(e, xhr, opt){
alert("Error requesting " + opt.url + ": " + xhr.status + " " + xhr.statusText);
}
})*/
//JQuery function
var ajax_url = ajax_params.ajax_url;
var data = {
'action': 'create_posts',
'a': a
};
$.post(ajax_url, data, function(response) {
$(".sub-tabs-a").append(response);
});
下面的代码显示了我添加脚本并位于"functions.php“中的方法
function add_our_script() {
wp_register_script( 'memberlist-js', get_template_directory_uri() . '../../wp-content/themes/medical-cure/video-filters.php', array( 'jquery' ), '', true );
wp_localize_script( 'memberlist-js', 'ajax_params', array( 'ajax_url' => '/wp-content/themes/medical-cure/create_filters.php' ) );
wp_enqueue_script( 'memberlist-js' );
}
add_action( 'wp_enqueue_scripts', 'add_our_script' );
发布于 2018-01-25 03:00:20
如果要本地化admin-ajax.php,那么如果将这些函数添加到functions.php中:
add_action( 'wp_ajax_nopriv_create_posts', 'my_create_posts' );
add_action( 'wp_ajax_create_posts', 'my_create_posts' );
function my_create_posts(){
if (is_string($_POST['a'])) {
$name = $_POST['a'];
//echo $name; and soo on...
}
}
ajax调用中的“action”用于定义在add_action中定义的对函数的ajax调用。
另一件事是,我不明白你在用a做什么,因为$name在任何地方都不使用。
如果您想检查是否正在传递a,为什么不使用isset($_POST['a'])
?
您需要将本地脚本ajax url更改为:
'ajax_url' => admin_url( 'admin-ajax.php' )
https://wordpress.stackexchange.com/questions/292079
复制相似问题