- 게시물 목록 화면에서 내용 길이 줄이고 '더보기' 출력하기 코드2025년 01월 30일
- TheStorybook
- 작성자
- 2025.01.30.:09
Q.
SNS 보면 본문 내용이 길어지는 경우 더보기를 표시해서 글을 가리고 본문으로 들어가서 보게끔 유도함.
해당 기능 구현 문의
아래 코드는 본문 내용을 요약 getSummary 하는게 아니고 getContent로 (메모장) 마냥 전체 본문을 표현하면서
장문에 대한 내용을 자르고 '더보기' 내용을 출력해 줍니다.
코드1.
{@ $maxLength = 350; if (mb_strlen(strip_tags($document->getContent(false))) > $maxLength): $fullUrl = getUrl('document_srl', $document->document_srl); // 문서의 URL 생성 $content = $document->getSummary($maxLength); $content .= " <a href='" . $fullUrl . "' class='read-more'>더보기</a>"; echo $content; else: $content = preg_replace('/<img[^>]*>/', '', $document->variables['content']); echo $content; endif; }
코드2. (내용 아래 <p> 태그 발생을 막아주고 본문 내용의 첨부파일(썸네일 아님) 이미지까지 불러오는 코드)
{@ $maxLength = 350; if (mb_strlen(strip_tags($document->getContent(false))) > $maxLength): $fullUrl = getUrl('document_srl', $document->document_srl); // 문서의 URL 생성 $content = $document->getSummary($maxLength); $content .= " <a href='" . $fullUrl . "' class='read-more'>더보기</a>"; echo $content; else: $content = preg_replace('/(<\s*p\s*>[\xA0|\xC2|\s| ]*<\s*\/p\s*>\s*)+<\s*\/div\s*>/i', '</div>', $document->getContent(false)); echo $content; endif; }
코드3. 최종 완성 , 호출된 본문 아래 공백들을 제거하고 이미지도 안불러옴 끝
{@ $maxLength = 350; if (mb_strlen(strip_tags($document->getContent(false))) > $maxLength): $fullUrl = getUrl('document_srl', $document->document_srl); // 문서의 URL 생성 $content = $document->getSummary($maxLength); $content .= " <a href='" . $fullUrl . "' class='read-more'>더보기</a>"; echo $content; else: $content = $document->variables['content']; $content = preg_replace('/<p[^>]*>\s*(<img[^>]*>)\s*<\/p>/', '', $content); echo $content; endif; }
코드4. 이온디님 코드
더보기 php 코드 list.blade.php v.1 @php $maxLength = 350; if(mb_strlen(strip_tags($document->getContent(false))) > $maxLength){ // $content = preg_replace('/<img[^>]*>/', '', $document->variables['content']); // $shortContent = mb_substr($content, 0, $maxLength) . '...'; $fullUrl = getUrl('document_srl', $document->document_srl); // 문서의 URL 생성 $content = $document->getSummary($maxLength); $content .= " <a href='" . $fullUrl . "' class='read-more'>더보기</a>"; echo $content; }else{ // echo '이하'; $content = preg_replace('/<img[^>]*>/', '', $document->variables['content']); // echo $document->getContent(false); echo $content; } @endphp
공백 제거부분 추가
@php $maxLength = 350; if(mb_strlen(strip_tags($document->getContent(false))) > $maxLength){ // $content = preg_replace('/<img[^>]*>/', '', $document->variables['content']); // $shortContent = mb_substr($content, 0, $maxLength) . '...'; $fullUrl = getUrl('document_srl', $document->document_srl); // 문서의 URL 생성 $content = $document->getSummary($maxLength); $content .= " <a href='" . $fullUrl . "' class='read-more'>더보기</a>"; echo $content; }else{ // echo '이하'; // $content = preg_replace('/<p><br><\/p>/', '', $document->variables['content']); $content = preg_replace('/<img[^>]*>/', '', $document->variables['content']); $content = preg_replace('/<br\s*\/?>/i', '', $content); // echo $document->getContent(false); echo $content; } @endphp
'XE(Rhymix) > 변수 응용' 카테고리의 다른 글
게시물 목록 화면에서 추천기능 넣기 (0) 2025.02.02 특정 게시판별 글쓰기 조건을 변경하고싶다면? (0) 2025.01.30 추천수/댓글수로 평점 계산하기 (0) 2025.01.29 게시글 본문에서 스크랩된 횟수 및 스크랩한 회원 목록 표시 (0) 2025.01.29 게시판 본문 요약을 불러올때 링크 주소는 제거하기 (0) 2025.01.29 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)