在控制台中遇到GET错误-未找到wp-json的404

时间:2019-03-14 作者:Jaimee Page

我正在尝试使用ajax在自定义帖子类型上加载更多帖子按钮

在我的函数文件中,我有以下内容:

add_action(\'rest_api_init\', \'custom_api_get_news\');
function custom_api_get_news(){
  register_rest_route( \'news\', \'/news\', array(
    \'methods\' => \'GET\',
    \'callback\' => \'custom_api_get_news_callback\'
  ));
}

function custom_api_get_news_callback($request){
    $posts_data = array();
    $paged = $request->get_param(\'page\');
    $paged = (isset($paged) || !(empty($paged))) ? $paged : 1;
    $posts = get_posts( array(
        \'post_type\' => \'news\',
        \'status\' => \'published\',
        \'posts_per_page\' => 6,
        \'orderby\' => \'post_date\',
        \'order\' => \'DESC\',
        \'paged\' => $paged
    ));

    foreach($posts as $post){
        $id = $post->ID;
        $post_thumbnail = (has_post_thumbnail($id)) ? get_the_post_thumbnail_url($id) : null;
        $post_cat = get_the_category($id);

        $posts_data[] = (object)array(
            \'id\' => $id,
            \'slug\' => $post->post_name,
            \'type\' => $post->post_type,
            \'title\' => $post->post_title,
            \'featured_img_src\' => $post_thumbnail,
            \'category\' => $post_cat[0]->cat_name
        );
    }
    return $posts_data;
}
在我的js文件中,我有:

$(\'#loadButton\').click(function() {
let pull_page = 1;
let jsonFlag = true;

if(jsonFlag) {
    jsonFlag = false;
    pull_page++;

    $.getJSON("/wp-json/news/all-posts?page=" + pull_page, function(data){
        if(data.length){
            var items = [];
            $.each(data, function(key, val){
                const arr = $.map(val, function(el) {
                    return el
                });
                const post_url = arr[1];
                const post_title = arr[3];
                const post_img = arr[4];
                const post_cat = arr[5];
                const post_class = (class_counter == 2) ? \'post adjust\' : \'post\';

                let item_string = \'<div class="news__holder">\' + post_img + \'</div>\';
                items.push(item_string);
            });
            if(data.length >= 6){

                $("#news-archive").append(items);
            } else {
                $("#news-archive").append(items);
                $("#load-more").hide();
            }
        } else {
            $("#load-more").hide();
        }
    }).done(function(data){
        if(data.length){
            jsonFlag = true;
            }
        });
    }
});
在存档页上:

<?php
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $args = array(
            \'post_type\' => \'news\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => 6,
            \'order_by\' => \'post_date\',
            \'order\' => \'DESC\',
            \'paged\' => $paged
        );

        $news = new WP_Query( $args );

        if( $news->have_posts() ) :
     ?>

         <section id="news-archive" class="news">
            <?php while( $news->have_posts() ): $news->the_post(); ?>
                <div class="news__holder">
                    <?php the_post_thumbnail(); ?>
                    <p class="news__holder__date"><?php the_date(); ?></p>
                    <h3><?php the_title(); ?></h4>
                    <?php the_excerpt(); ?>
                    <a href="<?php the_permalink(); ?>">Read More</a>
                </div>

            <?php endwhile; wp_reset_postdata(); ?>
         </section>
         <?php
         if($news->post_count < 6) {

         } else {
          ?>
         <section id="load-more" class="load-more">
             <div class="load-more__holder">
                <button id="loadButton"  type="button" name="button">Load More Posts</button>
             </div>

         </section>

     <?php } ?>
     <?php endif; ?>
我遇到的问题是,当我单击“加载更多”按钮时,控制台中出现错误GET http://localhost:8888/una/wp-json/news/all-posts?page=2 404 (Not Found)

我不确定这是否重要,但我在其他一些帖子上看到一些人对此有问题,但我的帖子会返回json罚款,如果你去http://localhost:8888/una/wp-json/news -- 下面是此url上显示的内容。有没有关于如何解决这个错误的想法?

 {"namespace":"news","routes":{"\\/news":{"namespace":"news","methods":["GET"],"endpoints":[{"methods":["GET"],"args":{"namespace":{"required":false,"default":"news"},"context":{"required":false,"default":"view"}}}],"_links":{"self":"http:\\/\\/localhost:8888\\/una\\/wp-json\\/news"}},"\\/news\\/news":{"namespace":"news","methods":["GET"],"endpoints":[{"methods":["GET"],"args":[]}],"_links":{"self":"http:\\/\\/localhost:8888\\/una\\/wp-json\\/news\\/news"}}},"_links":{"up":[{"href":"http:\\/\\/localhost:8888\\/una\\/wp-json\\/"}]}}

1 个回复
SO网友:Jacob Peattie

您使用的URL不是您注册的URL。

此代码:

register_rest_route( \'news\', \'/news\', array(
    \'methods\' => \'GET\',
    \'callback\' => \'custom_api_get_news_callback\'
));
注册此URL:

http://localhost:8888/una/wp-json/news/news
但你正在向

http://localhost:8888/una/wp-json/news/all-posts
您的代码中没有定义all-posts 端点。

你要么需要改变/news 在里面register_rest_route()/all-posts, 或者/all-posts 在您的URL中/news. 它们必须是一样的。

相关推荐

“POSTS_WHERE”筛选器未在`wp_ajax`中应用“WP_Query”

我有一个函数,它添加了posts_join 和aposts_where 在中搜索WP_Query. 它在普通的php呈现的搜索页面上运行良好。然而,我也将其用于ajax产品搜索和其他posts_join 和aposts_where 似乎不适用。以下是连接和位置:function search_by_meta_join($join) { global $wpdb; if (is_search()) { $join .= \" LEFT JOI