0

特定の部分に投稿項目のリスト (メニュー/サイドバーとして参照) と、コンテンツをロードする空のスペースがあるコンテナーがある wordpress でサイトを構築しています。投稿のリストからタイトルの 1 つをクリックすると、そのクリックされたタイトルのコンテンツが空の div に読み込まれるはずです。タイトルのリストから別のタイトルをクリックします。

したがって、基本的には次のようになります。

<div class="container">
  <ul class="cats">
     <?php
       $my_query = new WP_Query('showposts=10&cat=4');
       while ($my_query -> have_posts()) : $my_query->the_post();  $category = get_the_category(); 
     ?>

       <li class="trigger">
         <h5>
           <? the_title(); ?>
         </h5>
       </li>

     <?php 
       endwhile; wp_reset_query(); 
     ?>
  </ul>
  <div class="the-content">
    <? the_content(); ?>
  </div>
</div>

だから、私はすでに ajax の何かを調べましたが、それについての経験はありません。これを行う最善の方法は何ですか?

4

1 に答える 1

0

投稿を取得している間、投稿コンテンツを投稿固有の ID を持つ非表示の div に保存することもできます。例えば;

<?php
       $my_query = new WP_Query('showposts=10&cat=4');
       while ($my_query -> have_posts()) : $my_query->the_post();  $category = get_the_category();
echo '<div id="post_' . $my_query->the_post()->id . '" style="display:none;">' . $my_query->the_post()->content . '</div>'; 
 ?>

特定のタイトルをクリックすると、非表示の div から関連する投稿コンテンツを取得できます。

<li class="trigger" onclick="showContent(this)" id="post_id">//This id must be the id of post
         <h5>
           <? the_title(); ?>
         </h5>
</li>

そしてJavaScriptコード;

function showContent(obj) {
    $(".the-content").empty().html($("#" + $(obj).attr('id')).html());
}

それが役に立てば幸い

于 2012-11-12T10:39:32.837 に答える