2016-02-12 51 views
0

我使用的是WordPress,我有一篇文章想要抓取,並在我的網站的首頁上發佈信息。字符串文本的限制量

我已經設法讓它工作,但它發佈了整篇文章,我希望在最後發佈一個可能有30個字符的「更多閱讀」按鈕。

以下是我正在使用的代碼。由於我對Php比較陌生,所以我有點卡住了。

<div class="entry-content"> 
     <?php if (siteorigin_setting('blog_archive_content') == 'excerpt') the_excerpt(); else the_content(); ?> 

     <?php 
      wp_link_pages(array(
       'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'ultra') . '</span>', 
       'after' => '</div>', 
       'link_before' => '<span>', 
       'link_after' => '</span>', 
      )); 
     ?> 
    </div><!-- .entry-content --> 

這裏是php文件的全部內容:

<?php 
/** 
* @package ultra 
* @since ultra 0.9 
* @license GPL 2.0 
*/ 
?> 

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    <?php if (! is_single() && has_post_thumbnail() && siteorigin_setting('blog_archive_featured_image')) : ?> 

     <div class="entry-thumbnail"> 
      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
       <?php the_post_thumbnail(); ?> 
      </a> 
     </div> 

    <?php elseif (is_single() && has_post_thumbnail() && siteorigin_setting('blog_archive_featured_image')) : ?> 

     <div class="entry-thumbnail"> 
      <?php the_post_thumbnail(); ?> 

     </div> 

    <?php endif; ?> 
    <header class="entry-header"> 
     <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 

     <?php if ('post' == get_post_type()) : ?> 


     <?php endif; ?> 
    </header><!-- .entry-header --> 

    <div class="entry-content"> 
     <?php if (siteorigin_setting('blog_archive_content') == 'excerpt') the_excerpt(); else the_content(); ?> 

     <?php 
      wp_link_pages(array(
       'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'ultra') . '</span>', 
       'after' => '</div>', 
       'link_before' => '<span>', 
       'link_after' => '</span>', 
      )); 
     ?> 
    </div><!-- .entry-content --> 



</article><!-- #post-## --> 

回答

0

可以使用wp_trim_words,這將給你一個定義的話(不是字符)的量。

<?php echo wp_trim_words(get_the_content(), 5, '...<a href="'. get_permalink().'">Read More</a>');?> 

或使用mb_strimwidth這會給你一個字符限制。

<?php echo mb_strimwidth(get_the_content(), 0, 30, '...');?> 
+0

完美的作品,非常感謝。 –