0

我正在制作一个主题,该主题将允许我的 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 -->
4

1 回答 1

0

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

jQuery Ajax 返回 404 错误,但响应正确

问题在于:

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

它应该是:

include('wp-load.php');

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

于 2013-06-04T15:31:26.643 回答