0

我正在设计一个网站,其中有一个页面将显示来自给定父类别 ID 的子类别的帖子。每个帖子的标题都是一个问题,内容是答案。单击问题时我想要实现隐藏/显示答案,或者换句话说,单击标题时隐藏/显示内容。这里的主要内容是该脚本必须与 post 查询一起运行,因为 div 不是手动创建的,所以我无法分配特定的 id 或类来切换。切换必须适用于所有帖子,无论它们有多少。

这是我用来从父类别(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 -->

我想要的是,通过单击 a(现在是帖子的链接),它会打开关闭 div id="mini-content" 。

非常感谢你的协助。

我的问候,祝你有美好的一天。

4

1 回答 1

0

首先,<div id="x">{$cat_name}</div>从哪里来x

因此,您要做的是,当单击标题 div (id="x") 时,用 id="mini-content" 切换 div。

确保页面中的所有元素都有唯一的 ID!如果没有,请管理自己做到这一点。除非你没有这样做,否则你不能走得更远!

然后将click事件与id="x"元素绑定。由于您的元素可以在文档渲染后加载,使用.on()来绑定点击。所以现在和未来的“x”元素将与点击事件绑定。

如果您不习惯使用.on(),请查看文档http://api.jquery.com/on/

在元素的处理函数$(document).on("click","#x",function(){...});调用中。当然,迷你内容的初始可见性必须是“隐藏的”。.toggle()mini-content

于 2013-10-02T10:34:34.767 回答