Php 如何通过query()获取wordpress帖子并以json格式输出?

Php 如何通过query()获取wordpress帖子并以json格式输出?,php,android,arrays,wordpress,jsonp,Php,Android,Arrays,Wordpress,Jsonp,这段代码很好地工作,并拉博客文章,但唯一的问题是,我希望数据在一个json数组中,这样我就可以创建一个api,并在外部显示文章 <?php require('/home/dealicopter/public_html/wp-load.php'); function wpdocs_custom_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_le

这段代码很好地工作,并拉博客文章,但唯一的问题是,我希望数据在一个json数组中,这样我就可以创建一个api,并在外部显示文章

<?php
require('/home/dealicopter/public_html/wp-load.php');
function wpdocs_custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
$args = array(
    'posts_per_page' => 10,
    'cat' => 7,
);
query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php 
    $permalink = the_permalink();
    $title = the_title();
    $age = array("id"=>$permalink, "title"=>$title); 
    ?>
    <?php echo json_encode($age); ?>
<?php endwhile; else: echo "[]"; endif; ?>

<?php wp_reset_query(); ?>

Wordpress附带了一个返回JSON的内置API。你可以这样做

https://example.com/wp-json/wp/v2/posts/?filter%5Bposts_per_page%5D=-1

如果您想要定制代码,我建议您使用
wp\u send\u json()
在主题/子主题中使用以下代码创建一个新的模板文件,然后创建一个新页面,并在页面属性中选择临时文件

<?php
/*
Template Name: My Template Name
*/

add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
$args = array(
    'posts_per_page' => 10,
    'cat' => 7,
);

$output = array();
query_posts($args);

if (have_posts()) :
  while (have_posts()) : the_post();
    $permalink = get_the_permalink();
    $title = get_the_title();
    $output[] = array("id"=>$permalink, "title"=>$title); 
  endwhile;
endif;
wp_send_json($output);

将此代码添加到function.php主题文件中

<?php
function get_post()
{
    $args_query = array(
        'post_type' => array('post'),
        'post_status' => array('publish'),
        'nopaging' => true,
        'order' => 'DESC',
    );

    $query = new WP_Query($args_query);
    $data = null;
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $data[] = array('title' => get_the_title(), 'content' => get_the_content(), 'link' => get_the_permalink());
        }
    }
    wp_reset_postdata();

    if ($data != null) {
        echo json_encode(array('success' => true, 'data' => $data));
    } else {
        echo json_encode(array('success' => false, 'msg' => 'post not found'));
    }


    wp_die();
}
add_action('wp_ajax_get_post', 'get_post');
add_action('wp_ajax_nopriv_get_post', 'get_post');
?>

Javascript文件代码:

<script>
    (function($) {
        $('button').click(function(e) {
            e.preventDefault();
            $.ajax({
                type: "post",
                url: "https://********.com/wp-admin/admin-ajax.php",
                data: {
                    'action': 'get_post'
                },
                dataType: "json",
                success: function(response) {
                    console.log(response)
                }
            });
        });
    })(jQuery)
</script>

(函数($){
$(“按钮”)。单击(函数(e){
e、 预防默认值();
$.ajax({
类型:“post”,
url:“https://*********.com/wp admin/admin ajax.php”,
数据:{
“操作”:“获取帖子”
},
数据类型:“json”,
成功:功能(响应){
console.log(响应)
}
});
});
})(jQuery)