【问题标题】:jQuery/Ajax Wordpress Pagination 404 ErrorjQuery/Ajax Wordpress 分页 404 错误
【发布时间】:2013-05-30 20:50:54
【问题描述】:

我正在制作一个主题,它允许我的 wordpress 翻阅页面,而无需使用 jQuery 重新加载页面。

我不确定我做错了什么,但它必须与我在 wordpress 中调用新页面的方式有关,这给我的 JavaScript 控制台一个 404 错误,即使我点击链接控制台提供给未找到的页面,它加载正常

我正在与您分享我使用的 JavaScript 代码,以及它在 div 中加载的页面,因为我非常肯定该页面就是问题所在。如果我尝试使用一些传递的变量创建一个测试 PHP 文件,它加载得很好。

我的 JavaScript:

function nextpage(page) {

page++;

$('#ajaxloading').text('Loading...');   

$.ajax({
        type: "GET",
        url: homeUrl+"/wp-ajax-post.php?p="+page+"",
        cache: false,
        success: function(html){
            $('#ajaxcontent').html(html);
        }
    });
}

我的 wp-ajax-post.php:

<?php
require('wp-blog-header.php');
$wp_query = new WP_Query('paged=' . $_GET['p']); 
?>
<div id="content" style="width:400px;margin:auto;background:#c30000;padding:20px;">

        <?php /* Top post navigation */ ?>

        <?php /* The Loop — with comments! */ ?>
        <?php while ( have_posts() ) : the_post() ?>

        <?php /* Create a div with a unique ID thanks to the_ID() and semantic classes with post_class() */ ?>
                        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php /* an h2 title */ ?>
                            <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'hbd-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

        <?php /* Microformatted, translatable post meta */ ?>
                            <div class="entry-meta">
                                <span class="meta-prep meta-prep-author"><?php _e('By ', 'hbd-theme'); ?></span>
                                <span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'hbd-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
                                <span class="meta-sep"> | </span>
                                <span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'hbd-theme'); ?></span>
                                <span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>
                                <?php edit_post_link( __( 'Edit', 'hbd-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>
                            </div><!-- .entry-meta -->

        <?php /* The entry content */ ?>
                            <div style="height:50%;overflow-y:auto;" class="entry-content"  id="#ajaxloading">
        <?php the_content( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'hbd-theme' )  ); ?>
        <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'hbd-theme' ) . '&after=</div>') ?>
                            </div><!-- .entry-content -->

        <?php /* Microformatted category and tag links along with a comments link */ ?>
                            <div class="entry-utility">
                                <span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'hbd-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
                                <span class="meta-sep"> | </span>
                                <?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'hbd-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>
                                <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'hbd-theme' ), __( '1 Comment', 'hbd-theme' ), __( '% Comments', 'hbd-theme' ) ) ?></span>
                                <?php edit_post_link( __( 'Edit', 'hbd-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>
                            </div><!-- #entry-utility -->
                        </div><!-- #post-<?php the_ID(); ?> -->

        <?php /* Close up the post div and then end the loop with endwhile */ ?>      
        <?php endwhile; ?>
    <?php pagenavi(); ?>
    </div><!-- #content -->

【问题讨论】:

  • 你在这里得到页码 $wp_query = new WP_Query('paged=' . $_GET['p']);来自请求,但没有在任何地方使用
  • @AmitChotaliya ,它正在加载页面查询。当我转到 wp-ajax-post.php 并自己传递这些变量时,它会转到正确的页面...
  • 我研究了使用 WP_Query 的正确方法并修改了这一行 &lt;?php while ( $wp_query-&gt;have_posts() ) : $wp_query-&gt;the_post() ?&gt; 但我仍然得到相同的结果

标签: jquery ajax wordpress pagination http-status-code-404


【解决方案1】:

所以,虽然我显然不是在使用 WordPress 时这样做是最“正确”的方式,但由于 StackOverflow 上的另一篇文章,我已经解决了这个问题。

jQuery Ajax returning 404 Error, but correct Response

问题在于:

require('wp-blog-header.php');

应该是:

include('wp-load.php');

虽然这确实在我的插件中产生了一些全局变量的问题,但我能够解决它......我知道这不是在 Wordpress 中完成 ajax 的“正确”方式,因为它是内置的,但它为我工作。

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 2012-07-10
    • 1970-01-01
    • 2017-09-22
    • 2011-05-21
    • 2014-01-23
    • 2013-01-11
    • 2011-10-08
    • 2013-05-29
    相关资源
    最近更新 更多