Показать описание поста за пределами цикла - PullRequest
0 голосов
/ 06 октября 2019

У меня есть список членов команды (пользовательский тип записи), которые я вызываю с помощью класса WP_Query. Эта часть работает, однако я пытаюсь показать описание (the_content ()) члена команды вне цикла while при нажатии на его имя. Этот контейнер (# 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>

1 Ответ

2 голосов
/ 07 октября 2019

Вы можете попробовать это с jQuery. См. Приведенный ниже код.

<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 );
    });
});
...