2013-10-01 31 views
0

我正在設計一個網站,其中有一個頁面顯示來自給定某個父類別ID的子類別的帖子。 每個職位的標題將是一個問題和內容的答案。 當點擊問題或者換句話說,點擊標題時隱藏/顯示內容時,我想要實現隱藏/顯示答案。 這裏最主要的是這個腳本必須和post查詢一起運行,因爲div不是手動創建的,所以我不能指定一個特定的id或class來切換。 切換必須適用於所有帖子,不管它們有多少。隱藏/顯示jquery切換爲Query_post(wordpress)的div結果

這是我使用的查詢從父類的職位代碼(ID 327)

<div id="subcat" class="m-t-1 f13 georgia bold dark-grey"> 
         <?php 
          //get all child categories for a certain category ID, then for each child category display the posts 
          $parent_cat = 327; 
          $taxonomy = 'category'; 
          $cat_children = get_term_children($parent_cat, $taxonomy); 

          if ($cat_children) { 
          foreach($cat_children as $category) { 
           $args=array(
            'cat' => $category, 
            'post_type' => 'post', 
            'post_status' => 'publish', 
            'posts_per_page' => -1, 
            'caller_get_posts'=> 1 
           ); 
           $my_query = null; 
           $my_query = new WP_Query($args); 
           if($my_query->have_posts()) {        
           // echo 'Category name??' . $category;         
           $cat_name = get_cat_name($category); 
           echo <<<EOD 
           <div id="x">{$cat_name}</div> 
EOD; 
           while ($my_query->have_posts()) : $my_query->the_post(); ?>        
            <h5 class="m-v-10"><a href="<?php the_permalink() ?>" class="f13 blauet bold arial" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h5> 
            <div id="mini-content" class="f12 dark-grey w-normal arial dotted-1-bottom m-b-3"><?php the_content();?></div> 
            <?php 
            endwhile; 
           } 
           } 
          } 
          wp_reset_query(); // Restore global post data stomped by the_post(). 
          ?> 
         </div><!-- #subcat --> 

我想的是,由上clcking(現在的鏈接後),它在關閉div id =「mini-content」時切換。

非常感謝您的協助。

我的問候和愉快的一天。

+0

先把事情先做好。你有什麼嘗試? http://whathaveyoutried.com – bmasterson

回答

0

首先,<div id="x">{$cat_name}</div>哪裏來自x

所以,你想要做的是,當點擊標題div(id =「x」)切換ID =「迷你內容」的div。

確保頁面中的所有元素都有唯一的ID!如果沒有,管理你自己去做這件事。你不能進一步直到你沒有做到這一點!

然後將點擊事件綁定到id="x"元素。由於可以在呈現文檔之後加載元素,請使用.on()來綁定點擊。因此,現在和將來的「x」元素將被綁定到點擊事件中。

如果你不習慣使用.on(),檢查文檔http://api.jquery.com/on/

在處理函數調用$(document).on("click","#x",function(){...});.toggle()mini-content元素。當然,迷你內容的初始可見性必須是「隱藏的」。