在while循环外显示帖子描述

问题描述 投票:0回答:1

我有使用WP_Query类打电话的团队成员列表(自定义帖子类型)。这部分工作正常,但是我试图在单击成员名称时在while循环外显示其描述(the_content())。如代码中所示,此容器(#team-info)在while循环之外。理想情况下,单击名称后,页面将滚动到描述容器。

enter image description here

<div class="container">
<div class="row">
    <div class="col-md-12">
        <div id="team-info"></div>
    </div>
</div>

<div class="container mt-15">
<div class="row">
    <div class="col-md-12">
    <?php
        // The Query
        $the_query = new WP_Query( array (
            'post_type' => 'my_team_sp',
        ) );

        if( $the_query->have_posts() ): $i = 0;
            while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; ?>
                 <div class="col-md-4 <?php echo $i ;?>">
                        <a href="#" id="team-name" onclick="myFunction()"><h4><?php the_title() ;?></h4></a>
                    </div>
            <?php endwhile;
        else :
        endif;


        /* Restore original Post Data */
        wp_reset_postdata();
        ?>
    </div>
</div>

javascript wordpress custom-post-type
1个回答
1
投票
<div class="container mt-15"> <div class="row"> <div class="col-md-12"> <?php // The Query $the_query = new WP_Query( array ( 'post_type' => 'my_team_sp', ) ); if( $the_query->have_posts() ): $i = 0; while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; ?> <div class="col-md-4 <?php echo $i ;?>"> <a href="javascript:void(0)" class="team-name"><h4><?php the_title() ;?></h4></a> <div style="display: none;"><?php the_content(); ?></div> </div> <?php endwhile; else : endif; /* Restore original Post Data */ wp_reset_postdata(); ?> </div> </div>

jQuery

将此代码添加到Js文件中。

jQuery(document).ready(function($){ $( '.team-name' ).on('click', function(){ var teamInfo = $(this).next().html(); $( '#team-info' ).html( teamInfo ); }); });
© www.soinside.com 2019 - 2024. All rights reserved.