首页 文章

使用HTML格式在帖子档案中的内容中的Wordpress修剪词

提问于
浏览
-1

我试图限制重复的帖子/帖子存档中的单词数量 . 然而,当我修剪单词Wordpress似乎删除所有格式 - 单词限制工作正常 . 这是我到目前为止: -

<?php
// trim the content
global $post;
 $my_content= $post->post_content;
 //$my_content = get_the_content();
 trimmed_my_content = wp_trim_words( $my_content, 400, '<a href="'. get_permalink() .'">&nbsp;<span class="moretext">More</span></a>' );
//echo $trimmed_my_content;
echo apply_filters('trimmed_my_content', $trimmed_my_content);
?>

您将从// out php中看到我尝试了几种方法但没有成功 . 但是,如果我只是在循环中输出内容而没有过滤它可以正常工作: -

<?php the_content('Read more...'); ?>

适用于HTML过滤/格式化...

我应该注意,我想在我的Wordpress模板中包含这些代码,因为我限制其他地方的内容/摘录,我不希望它是通用的 . 显然,模板中php调用的functions.php中的函数很好 .

谢谢Glennyboy

2 回答

  • 0

    您可以使用

    $my_content = apply_filters('the_content', $post->post_content);
    

    代替:

    $my_content = $post->post_content;
    
  • 0
    <?php
         global $post;
         //$my_content= $post->post_content;
         $my_content=apply_filters('the_content',$post->post_content);//this will get the content for you
    
         trimmed_my_content = wp_trim_words( $my_content, 400, '<a href="'. get_permalink() .'">&nbsp;<span class="moretext">More</span></a>' );
        echo $trimmed_my_content;
        ?>
    

    或者您现在可以根据需要格式化数据 .

相关问题