如何在JSON中获取Wordpress自定义帖子数据? [英] How to get wordpress custom post data in json?

查看:84
本文介绍了如何在JSON中获取Wordpress自定义帖子数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模板文件中,我这样获取自定义帖子数据

In my template file i fetch the custom post data like this ,

最初我要获取7个帖子,我需要在该帖子下面添加一个更多的阅读按钮,当有人单击它时,它将从wp数据库表中获取更多7个帖子。

Initially I fetch 7 post, I need to make a read more button bellow the post, which will fetch more 7 posts from the wp database table, when someone click on it.

但是我不知道该怎么做,

But I don't know how to do it, I want to know about,


  • 在jquery get方法中,我调用哪个php文件作为数据,

  • 我将如何或确切地在该php文件中编写什么脚本。

推荐答案

在这里,我添加了一个粗略的想法,即如何编写代码:

Here i have added a rough idea how you can write your code :

在function.php中创建一个ajax函数,并通过ajax调用将偏移值传递给它,然后将接收到的数据追加到显示部分中。

Create a ajax function in function.php , and pass offset value to it through ajax call and just append the received data in your disply section.

下面是创建ajax函数的示例:

here is an example of creating ajax function :

add_action('wp_ajax_nopriv_cyt_ajax_search','cyt_ajax_search');
add_action('wp_ajax_cyt_ajax_search','cyt_ajax_search');
function cyt_ajax_search(){


    $offset = $_POST['offset'];

    $args = array (
    'post_type' => 'post',
    'posts_per_page' =>7
    'offset'=>$offset,

    'meta_query' =>..........

    );
$query = new WP_Query($args);
if($query->have_posts() ) : 
        while ( $query->have_posts() ) : $query->the_post(); 


endwhile; 

wp_reset_postdata();
endif; 


}

//前端代码,单击按钮它将校准ajax函数并传递偏移量值,每次单击时,您都需要将其值增加7(如果您只想加载7个帖子),并检查剩余的帖子数,以及偏移量值是否超过总数计算要通过wp查询显示的数据,然后只需隐藏按钮

//Frontend code , on button click it will cal the ajax function and pass the offset value, on each click you need to increase the value by 7 (in case you want to load 7 post only ) and check how many post are left and if offset value exceedded the no of total counted data to be display by wp query then simply hide the button

<div id ="esiSection"></div>

<span click="loadmore" data-offset='0'>Click here</span>  

jQuery('.loadmore').click(function(){ 
var offset = parseInt(jQuery(this).attr('data-offset'));
jQuery.ajax({				
			url: '<?php echo admin_url('admin-ajax.php'); ?>',
			type: 'POST',			 
			data: { 
				'action' : 'cyt_ajax_search',  
				'offset' : offset ,
			},
		success: function(response) {
		
			jQuery('#resiSection').append(response);
			offset = offset + 7;
	
			
		},
		error: function(error){
			console.log(error);
			
		}
				
	});	
	
});	

这篇关于如何在JSON中获取Wordpress自定义帖子数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆