Wordpress 제목: 50자를 초과하는 경우 생략 부호 표시
제목이 있는 WordPress 사이트가 있는데 제목이 50자를 넘으면 줄임표를 추가해야 합니다....제목 말미에 있는 제목을 50자로 정지합니다.
아래는 제가 쓰고 있는 PHP입니다만, 정상적으로 동작하지 않는 것 같습니다.
<?php if (strlen("the_title()") > 50) { ?>
<?php the_title(); ?>
<?php } if (strlen("the_title()") < 50) { ?>
<?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>
mb_strimwidth 함수는 바로 이 기능을 수행합니다.
echo mb_strimwidth(get_the_title(), 0, 50, '...');
WordPress는 기능이 내장되어 있습니다."wp_trim_words()"입력한 단어 수에 따라 문장을 자르기 위해 단어별로 자르기 기능을 사용할 수 있습니다.
https://codex.wordpress.org/Function_Reference/wp_trim_words
제목을 5단어 이상 잘라내려면 이 작업을 수행할 수 있습니다.
<?php
$title = get_the_title();
$short_title = wp_trim_words( $title, 5, '...' );
echo '<h3>'.$short_title.'</h3>';
?>
단일 코드, 100% 작동
PHP 함수 mb_strimwidth() | Wordpress 함수 get_title()
<?php
echo mb_strimwidth( get_the_title(), 0, 100, '...' );
?>
이것을 테마 폴더의 「functions.php」파일에 추가합니다.
function the_title_excerpt($before = '', $after = '', $echo = true, $length = false)
{
$title = get_the_title();
if ( $length && is_numeric($length) ) {
$title = substr( $title, 0, $length );
}
if ( strlen($title)> 0 ) {
$title = apply_filters('the_title_excerpt', $before . $title . $after, $before, $after);
if ( $echo )
echo $title;
else
return $title;
}
}
그럼 이렇게 제목을 불러주세요.
<?php the_title_excerpt('', '...', true, '50'); ?>
문자열 길이를 확인하는 중입니다."the_title()"따옴표를 삭제하면 효과가 있을 것입니다(Wordpress를 오랫동안 사용하지 않았기 때문에 _title()과 get_title()의 차이를 100% 확신할 수 없습니다.그것도 전환해야 할 수도 있습니다).
<?php if (strlen(the_title()) > 50) { ?>
<?php the_title(); ?>
<?php } if (strlen(the_title()) < 50) { ?>
<?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>
또는 아마도
<?php if (strlen(get_the_title()) > 50) { ?>
<?php the_title(); ?>
<?php } if (strlen(get_the_title()) < 50) { ?>
<?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>
<?php
$title = the_title('','',false);
if(strlen($title) > 60):
echo trim(substr($title, 0, 65)).'...';
else:
echo $title;
endif;
?>
가지고 가다the_title()를 사용할 때 인용구가 없습니다.strlen()기능.
echo (strlen(the_title())>50) ? (substr(the_title(), 0, 50) . "...") : the_title());
이것은 3진 연산자입니다.기본적으로 말하는 것은 그 결과가the_title()50자를 초과하여 처음 50자를 에코하고 다음으로 문자열을 에코합니다....그렇지 않은 경우 결과를 그대로 반영합니다.the_title().
에 대한 자세한 내용을 참조해 주세요.substr여기: http://php.net/manual/en/function.substr.php
3진 연산자에 대한 정보는 http://php.net/manual/en/language.operators.comparison.php 에서 찾을 수 있습니다.
strlen을 사용하다
eg:
<?php echo ((strlen(get_the_title())>50) ? (substr(get_the_title(), 0, 50) . "...") : get_the_title())?>
언급URL : https://stackoverflow.com/questions/4617769/wordpress-titles-if-longer-than-50-characters-show-ellipsis
'programing' 카테고리의 다른 글
| Jenkins 오류 - 문서 프레임이 샌드박스에 저장되고 '스크립트 허용' 권한이 설정되지 않아 에서 스크립트 실행이 차단되었습니다. (0) | 2023.03.18 |
|---|---|
| JSON 파일의 값 변경(파일 쓰기) (0) | 2023.03.18 |
| React에서 상태 비저장 구성 요소의 참조에 연결하는 방법은 무엇입니까? (0) | 2023.03.18 |
| Spring REST 서비스: json 응답의 null 개체를 제거하도록 구성하는 방법 (0) | 2023.03.18 |
| 스프링 임베디드 Kafka를 사용한 @Kafka Listener 테스트 (0) | 2023.03.18 |