GeneratePress테마 관련글 더 보기 추가하기

GeneratePress테마 관련글 더 보기 추가하는 방법에 대해 알아보겠습니다.

포스팅 글을 쓴 후 하단에 관련 글 더 보기가 노출되어 있다면 방문자의 이탈율이 낮아져 워드프레스 SEO에 도움이 됩니다. 방문자들이 한번 블로그에 방문 후 계속 블로그의 다른 글을 읽어 사이트 체류시간을 늘려야 한다는 것입니다.

GeneratePress의 엘리먼트 >> 후크를 이용하면 간단하게 해결 가능 합니다. 후크를 이용한 다른 글은 여기를 참고 해주세요.

GeneratePress테마 관련글 더 보기: Hook 이용하기

아래와 같이 후크 타이틀을 Related Posts라고 적당히 적은 후 php 코드를 넣어 주면 됩니다. 관련 코딩자료는 하단에 있습니다. 참고 해주세요. php 소스가 실행되어야 하기 때문에 반드시 하단의 execute php를 반드시 체크 해줘야 합니다. 다음으로 주의 할 점은 후크 위치를 적당히 잡아 주면 됩니다.

GeneratePress테마 관련글 더 보기: PHP 소스

아래 내용으로 후크를 만들어 주면 됩니다.

<!-- 관련 글 -->
<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>
<!-- 관련 글 -->