在wordpress中通过php生成其ID生成的div

时间:2018-04-06 18:46:01

标签: javascript php jquery wordpress toggle

希望有人可以提供帮助。我在wordpress网站上创建了一些自定义标记功能,我对如何显示和隐藏数据有点困惑。以下是我到目前为止的情况:

 <?php global $post;
//Get terms for this taxonomy - orders by name ASC by default
$terms = get_terms('post_tag');

//Loop through each term
foreach($terms as $term):

   //Query posts by term. 
   $args = array(
    'orderby' => 'title', //As requested in comments
       'post_type'   => 'reiseberichte',
    'post_parent' => $post->ID,
    'orderby'     => 'menu_order',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => array($term->slug)
        )
     ));
    $tag_query = new WP_Query( $args );

    //Does tag have posts?
    if($tag_query->have_posts()):?>

                    <div class='<?php echo esc_html($term->slug);?>'><a href="#"><?php echo esc_html($term->name);?></a></div>
<?php
        //Loop through posts and display
        while($tag_query->have_posts()):$tag_query->the_post(); ?>
                    <div class="content">
        <div class="post_content hidden-content" id='<?php echo esc_html($term->slug);?>'><?php echo the_post_thumbnail(); 
            echo the_title(); ?></div></div>
        <?php endwhile;

    endif; //End if $tag_query->have_posts
    wp_reset_postdata();
 endforeach;//Endforeach $term

?>

我需要做这样的事情,用他们的术语slug生成的ID来关闭div:

<script>
jQuery(document).ready(function($){
        $(".<?php echo esc_html($term->slug);?>").click(function() {
            $("#<?php echo esc_html($term->slug);?>").toggle();
        });
    });
</script>

我知道这是错的,但我会感激任何指针。按钮的类和包含div的ID都由<?php echo esc_html($term->slug);?>

生成

提前致谢

1 个答案:

答案 0 :(得分:0)

如何通过类属性查找div?

<script>
jQuery(document).ready(function($){
        $(".content").click(function() { //Look for 'content' class
            $(this).toggle();
        });
    });
</script>