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

Posted

技术标签:

【中文标题】如何通过 query() 获取 wordpress 帖子并以 json 格式输出?【英文标题】:How to fetch wordpress posts through query() and output them in json? 【发布时间】:2021-08-06 18:44:02 【问题描述】:

这段代码工作正常并拉动博客文章,但唯一的问题是我希望数据位于 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(); ?>

【问题讨论】:

【参考方案1】:

Wordpress 带有一个返回 JSON 的内置 API。你可以按如下方式钩住它

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

API Documentation

如果您想制作自定义代码,我建议使用wp_send_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);

【讨论】:

【参考方案2】:

将此代码添加到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>

【讨论】:

以上是关于如何通过 query() 获取 wordpress 帖子并以 json 格式输出?的主要内容,如果未能解决你的问题,请参考以下文章

WordPress:如何使用 $wp_query 按类别过滤帖子?

在 query_posts 中获取 WordPress 中的所有帖子类型

获取/设置变量重写URL Wordpress

在Wordpress循环中获取迭代计数

如何在 wordpress 中使用 wp_Query 输出 JSON?

如何加快 WordPress 中的 GEO 搜索