首页 > 解决方案 > 使用 PHP、JQuery 和 wordpress 在单页应用程序中一次显示一篇文章

问题描述

我想在 div 中一次显示一篇文章并替换内容以在单击按钮时显示下一个(相邻)文章的内容。

我目前正在使用 循环浏览帖子wp_query,但所有帖子都堆叠显示。

query_posts($args);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
$count = $wp_query->post_count;
$i = 1;
if ( $wp_query->have_posts() ) :
    while ( $wp_query->have_posts() && $i < 9) : $wp_query->the_post();
?>

我正在提取以下内容:
<?php echo $i?> / <?php echo $count?>
<?php the_title();?>
<?php the_content();?>

我在按钮周围还有一个条件来拉入相邻链接的永久链接,但是当我只需要帖子的 slug 时,它会抓住整个永久链接。我在想象这样的事情,但它不起作用:
$next_slug = get_slug(get_adjacent_post(false, '', false));

标签: phpwordpressloops

解决方案


<?php  while ( have_posts() ) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php the_title( '<h1>','</h1>' );  ?>
        <div class="post-thumbnail"><?php the_post_thumbnail(array(250, 250)); ?> </div>
        <div class="entry-content"><?php the_content(); ?></div>
        <div class="btn"><?php next_post_link('%link', '%title'); ?></div>
    </article>
<?php endwhile; ?>

</div>

推荐阅读