the_excerpt 사용시 class 내용 자동 입력하기


워드프레스 포스트 내용을 출력하는데 있어 목록으로 사용하거나, 간략한 설명으로 사용하기 위해 포스트 내용에서 태그를 제거한 후 일정 글자수로 잘라 출력할때 사용하는 워드프레스 기본 함수 입니다.

the_excerpt()

get_the_excerpt()

<?php the_excerpt();?> 형태로 사용하며, 자동으로 <p>본문내용</p> 으로 출력됩니다.

<p><?php echo get_the_excerpt();?></p> 본문 내용만 출력하며 p 또는 div 태그를 이용하여 블럭처리하여 사용합니다.

테마를 직접 개발한다면 functions.php 파일에 아래 내용을 포함하여 자동으로 말줄임을 사용하면 편리합니다.

add_action('the_excerpt','add_class_to_excerpt');
function add_class_to_excerpt( $excerpt ){
    return str_replace('<p', '<p class="dotdotdot"', $excerpt);
}