워프 본문 하단에 관련글(카테고리의 관련글) 더보기 표시하기

페이지 정보

작성자 관리자 (101.♡.52.5) 조회 891회 작성일 23-08-07 17:52()

본문

워드프레스 포스트 글 하단에 관련글 더보기를 나타내는 코드 입니다.

 

[code]

<!-- 관련 글 -->

<div class="related-posts">

<h4><span><?php $categories = get_the_category();

echo esc_html( $categories[0]->name ); ?></span> 카테고리의 다른 글</h4>

<?php 

$post_id = get_the_ID();

    $cat_ids = array();

    $categories = get_the_category( $post_id );


    if(!empty($categories) && is_wp_error($categories)):

        foreach ($categories as $category):

            array_push($cat_ids, $category->term_id);

        endforeach;

    endif;


    $current_post_type = get_post_type($post_id);

    $query_args = array( 


        'category__in' => wp_get_post_categories($post->ID),

        'post_type'      => $current_post_type,

        'post__not_in' => array(get_the_ID()),

        'posts_per_page'  => '5',

'orderby' => 'rand'


     );


    $related_cats_post = new WP_Query( $query_args );


    if($related_cats_post->have_posts()):

         while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>

            <ul>

                <li>

                    <a href="<?php the_permalink(); ?>">

                        <?php the_title(); ?>

                    </a>

                 

                </li>

            </ul>

        <?php endwhile;


        // Restore original Post Data

        wp_reset_postdata();

     endif; ?>

</div>

<!-- 관련 글 -->

[/code]




댓글목록

등록된 댓글이 없습니다.

사이트안내 | 서비스 이용약관 | 개인정보 처리방침 |
마이링크 | 후원안내 | FAQ | Admin
TOP