尝试通过AJAX将单个帖子加载到首页

时间:2015-01-08 作者:Desi

我之前也发过一个类似的问题,但我修改了一些代码,所以我想我应该开始一个新的主题。下面是我目前必须在首页上加载单个帖子的设置。

This is what I\'m trying to do. 请注意,单击帖子时,它会加载到隐藏的容器中。

有人能帮我弄清楚吗?

Front-page.php

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <div id="project-container">
            <img id="loading-animation" src="http://i.imgur.com/5RMfW8P.gif" style="display:none">
        </div>

        <!-- Start the loop -->
        <?php $home_query = new WP_Query(\'post_type=projects\');

        while($home_query->have_posts()) : $home_query->the_post(); ?>

            <a class="post-link" href="#" rel="<?php the_ID(); ?>">
                <article id="post-<?php the_ID(); ?>">
                    <div class="post-info">
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    </div>
                    <?php the_post_thumbnail( "home-thumb", array( \'class\' => \'grayscale grayscale-fade\') ); ?>
                </article><!-- #post-## -->
            </a>

        <?php endwhile; ?>
        <?php wp_reset_postdata(); // reset the query ?>

    </main><!-- #main -->
</div><!-- #primary -->

Functions.php

/**
 * Enqueue scripts and styles.
 */
function starter_scripts() {
    wp_deregister_script( \'jquery\' );
    wp_register_script( \'jquery\', includes_url( \'/js/jquery/jquery.js\' ), false, NULL, true );
    wp_enqueue_script( \'jquery\' );
    wp_enqueue_script( \'jquery-effects-core\');

    wp_enqueue_style( \'starter-style\', get_stylesheet_uri() );
    wp_enqueue_script( \'gray\', get_template_directory_uri() . \'/js/min/jquery.gray.min.js\', array(\'jquery\'), \'\', true );
    wp_enqueue_script( \'includes\', get_template_directory_uri() . \'/js/min/includes.min.js\', array(\'jquery\'), \'\', true );
    wp_localize_script( \'includes\', \'site\', array(
                \'theme_path\' => get_template_directory_uri(),
                \'ajaxurl\'    => admin_url(\'admin-ajax.php\')
            )
    );
}
add_action( \'wp_enqueue_scripts\', \'starter_scripts\' );

/**
 * Return the post content to the AJAX call
 */
function my_load_ajax_content () {
    $args = array( \'p\' => $_POST[\'post_id\'] );
    $post_query = new WP_Query( $args );
    while( $post_query->have_posts() ) : $post_query->the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr( \'Permalink to %s\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <div class="entry-content">
            <?php the_content(); ?>
        </div> <!-- end .entry-content -->
    </div>

    <?php       
    endwhile;           
    exit;
}
add_action ( \'wp_ajax_nopriv_load-content\', \'my_load_ajax_content\' );
add_action ( \'wp_ajax_load-content\', \'my_load_ajax_content\' );

Includes.js

// Load posts via AJAX
$(".post-link").click(function(e) {

    e.preventDefault();

    $("#loading-animation").show();
    var post_id = $(this).attr(\'rel\');
    var ajaxURL = site.ajaxurl;

    $.ajax({
        type: \'POST\',
        url: ajaxURL,
        data: {"action": "load-content", post_id: post_id },
        success: function(response) {
            $("#project-container").html(response);
            $("#loading-animation").hide();
        return false;
        }
    });
});

Single.php

<?php while ( have_posts() ) : the_post(); ?>

<div class="post-container">

    <?php the_title( \'<h1 class="entry-title">\', \'</h1>\' ); ?>

    <?php the_content(); ?>

</div><!-- #post-## -->

    <?php starter_post_nav(); ?>

<?php endwhile; // end of the loop. ?>

2 个回复
最合适的回答,由SO网友:Milo 整理而成

您链接的代码与您的版本之间的区别在于,您的帖子链接将永久链接作为href值,而原始链接只有一个哈希。当您向锚定标记添加单击处理程序时,它不会阻止您单击该链接时通常发生的情况,它只会执行javascript,正常的链接行为会继续,否则会继续。如果要防止在单击事件中跟踪链接,必须explicitly prevent that in your javascript:

$(".post-link").click(function(event) {
    event.preventDefault();
    // the rest of your code... 
});

SO网友:Vadim Goncharov

加载single.php 模板,首先需要移动functions.php 密码

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
   <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
   <div class="entry-content">
     <?php the_content(); ?>
   </div>
</div>
将上述代码放入single.php. 确保你更换了所有东西,因为单身。php有一个while循环,但我们已经在函数中做了一个循环。php。

并代替上述代码添加get_template_part(\'single\'); 到您的functions.php 在while循环之间。

结束