有没有办法限制帖子标题的字数?
我在网上搜索了一下,但什么也没找到。
我所知道的是,只有帖子的内容可以被限制或摘录。
有没有办法限制帖子标题的字数?
我在网上搜索了一下,但什么也没找到。
我所知道的是,只有帖子的内容可以被限制或摘录。
只要在你想用有限的文字显示标题的地方使用这个
<?php echo wp_trim_words( get_the_title(), 5 ); ?>
将上述代码中的数字5替换为需要显示的任何字数。当做
如果继续,请签署“…”标题末尾需要
<?php
echo wp_trim_words( get_the_title(), 10, \'...\' );
?>
如果继续,请签署“…”标题末尾不需要<?php
echo wp_trim_words( get_the_title(), 10 );
?>
有一个内置功能:wp_trim_words()
.
add_filter( \'the_title\', \'wpse_75691_trim_words\' );
function wpse_75691_trim_words( $title )
{
// limit to ten words
return wp_trim_words( $title, 10, \'\' );
}
如果你想根据帖子的某些属性删减单词,请WPpass the post ID 回拨您的电话。下面是按帖子类型筛选的示例。但你也可以检查相关的术语,后年龄或作者,甚至后元。add_filter( \'the_title\', \'wpse_75691_trim_words_by_post_type\', 10, 2 );
function wpse_75691_trim_words_by_post_type( $title, $post_id )
{
$post_type = get_post_type( $post_id );
if ( \'product\' !== $post_type )
return $title;
// limit to ten words
return wp_trim_words( $title, 10, \'\' );
}
function limit_word_count($title) {
$len = 5; //change this to the number of words
if (str_word_count($title) > $len) {
$keys = array_keys(str_word_count($title, 2));
$title = substr($title, 0, $keys[$len]);
}
return $title;
}
add_filter(\'the_title\', \'limit_word_count\');
你可以对任何你喜欢的东西施加任何限制,你只需要正确的过滤器我的单曲中有以下代码。php页面:UPDATED CODE <?php if (have_posts()) : the_post(); $post_id = get_the_ID(); if ( is_single() ) { $cats = get_the_category(); $cat = $cats[0]; } else { // category archives