Wordpress显示内容,无需重新加载页面

时间:2019-03-05 09:25:30

标签: javascript jquery wordpress

我有下一个代码:

$('.left article').on('click', function(event) {
   var article_content = $(this).children('.article-content').html()
	$('.right').html(article_content);
});
* {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

section {
  display: flex;
}

section > div {
  flex: 0 0 50%;
  padding: 15px;
  width: 50%;
}

.left .article-content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
  <div class="left">
     <article>
        <h2>My post title</h2>
        <div class="article-content"><p>This is the content for the first article</p></div>
        </article>

     <article>
        <h2>Another post title</h2>
        <div class="article-content"><p>Content for the the second post</p></div>
        </article>

     <article>
        <h2>Third post title</h2>
        <div class="article-content"><p>This is the content for the third post</p></div>
        </article>
  </div>


  <div class="right">

  </div>
</section>

我想使用来自自定义帖子类型的帖子来实现此目的,因此在第一列中,我需要显示标题列表,在第二列中,我要显示帖子的内容。我使用下一个代码完成了此操作:

 <section>
        <div class="left">
            <article>
                <h2>
                    <ul>
                        <?php global $post;
                        $wpposts = get_posts('numberposts=3&post_type=>book');
                        foreach($wpposts as $post):
                            setup_postdata($post);?>
                            <li>
                                    <?php the_title(); ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </h2>
                <div class="article-content">
                    <p>
                    <?php the_content(); ?>
                    </p>
                </div>
            </article>
        </div>


        <div class="right">

        </div>
    </section>

但是当我单击一个链接时,我得到了全部内容,而第二次单击另一个标题时,我没有得到结果。

谁知道我应该修改什么?

1 个答案:

答案 0 :(得分:0)

您的循环似乎不正确。试试这个:

 <section>
        <div class="left">
           <?php global $post;
                        $wpposts = get_posts('numberposts=3&post_type=>book');
                        foreach($wpposts as $post):
                            setup_postdata($post);?>
            <article>
                <h2>
                    <ul>
                            <li>
                                    <?php the_title(); ?>
                            </li>

                    </ul>
                </h2>
                <div class="article-content">
                    <p>
                    <?php the_content(); ?>
                    </p>
                </div>
            </article>
           <?php endforeach; ?>
        </div>


        <div class="right">

        </div>
    </section>

现在每个帖子都将包裹在<article>中。