gpt4 book ai didi

php - 从wordpress中的摘录末尾删除3个点

转载 作者:行者123 更新时间:2023-12-02 04:31:19 25 4
gpt4 key购买 nike

我的首页显示了三个文本,但都以“...”结尾,除了这个特定的地方之外,其他地方都可以。这三个文本的帖子类型都是服务,所以我将代码修改如下。

// The excerpt based on words
/* Original disabled by Kenn Nielsen
function my_string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'...';
}
*/

// Created if statement to remove dots from services on frontpage

if (post_type_exists('services') ) {
function my_string_limit_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'';
}
} else {
function my_string_limit_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'...';
}
}

不幸的是,这没有按预期工作。我有什么遗漏的吗?我确信这可以用更短的代码来完成,但我选择快速的方法。

最好,肯恩

解决方法:

用最简单的解决方案修复了这个问题,而无需更改 theme-function.php 以及更改摘录以在给出字数的情况下显示三个点的方式。

在小部件中,用于显示内容的代码是:

        <div class="post_content">
<?php if ( $instance['excerpt'] ) : ?>
<?php if($limittext=="" || $limittext==0){ ?>
<?php if ( $instance['excerpt_as_link'] ) : ?>
<a href="<?php the_permalink() ?>">
<?php endif; ?>
<?php the_excerpt(); ?>
<?php if ( $instance['excerpt_as_link'] ) : ?>
</a>
<?php endif; ?>
<?php }else{ ?>
<?php if ( $instance['excerpt_as_link'] ) : ?>
<a href="<?php the_permalink() ?>">
<?php endif; ?>
<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext);?>
<?php if ( $instance['excerpt_as_link'] ) : ?>
</a>
<?php endif; ?>
<?php } ?>
<?php endif; ?>
</div>

方法是在没有函数的情况下进行回显,所以

<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,$limittext);?>

将会

<?php $excerpt = get_the_excerpt(); echo $excerpt;?>

这解决了问题/请求。

最佳答案

假设您使用 the_excerpt() 来显示内容。

将以下函数添加到您的 function.php 文件中。

function change_excerpt( $more ) {
if(post_type_exists('services')){
return '';
}
return '...';
}
add_filter('excerpt_more', 'change_excerpt'); //Wordpress 2.8+

关于php - 从wordpress中的摘录末尾删除3个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23043563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com