php Wordpress摘录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Wordpress摘录相关的知识,希望对你有一定的参考价值。

// custom excerpt length
function themify_custom_excerpt_length( $length ) {
   return 20;
}
add_filter( 'excerpt_length', 'themify_custom_excerpt_length', 999 );

// add more link to excerpt
function themify_custom_excerpt_more($more) {
   global $post;
   return '<a class="more-link" href="'. get_permalink($post->ID) . '">'. __('Read More', 'themify') .'</a>';
}
add_filter('excerpt_more', 'themify_custom_excerpt_more');



// GUIDE
http://wordpress.stackexchange.com/questions/141125/allow-html-in-excerpt
/*

Custom excerpt function, keeps HTML tags but does the word count without counting HTML tags.

*/


function wpse_int_allowedtags() {
// Add custom tags to this string
    return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>'; 
}

if ( ! function_exists( 'wpse_int_custom_wp_trim_excerpt' ) ) : 

  function wpse_int_custom_wp_trim_excerpt($wpse_excerpt) {
  $raw_excerpt = $wpse_excerpt;
    if ( '' == $wpse_excerpt ) {

      $wpse_excerpt = get_the_content('');
      $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
      $wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
      $wpse_excerpt = str_replace(']]>', ']]&gt;', $wpse_excerpt);
      $wpse_excerpt = strip_tags($wpse_excerpt, wpse_int_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */

      //Set the excerpt word count and only break after sentence is complete.
      $excerpt_word_count = 175;
      $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
      $tokens = array();
      $excerptOutput = '';
      $count = 0;

      // Divide the string into tokens; HTML tags, or words, followed by any whitespace
      preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);

      foreach ($tokens[0] as $token) { 

        if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) { 
          // Limit reached, continue until , ; ? . or ! occur at the end
          $excerptOutput .= trim($token);
          break;
        }

        // Add words to complete sentence
        $count++;

        // Append what's left of the token
        $excerptOutput .= $token;
      }

      $wpse_excerpt = trim(force_balance_tags($excerptOutput));

      // $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'wpse' ), get_the_title()) . '</a>'; 
      $excerpt_end = '';
      $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

      //$pos = strrpos($wpse_excerpt, '</');
      //if ($pos !== false)
      // Inside last HTML tag
      //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
      //else
      // After the content
      // $wpse_excerpt .= $excerpt_more; // Add read more in new paragraph 

      return $wpse_excerpt;   

    }

    return apply_filters('wpse_int_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
  }

endif; 

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_int_custom_wp_trim_excerpt'); 

以上是关于php Wordpress摘录的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress摘录

php 从摘录中删除短代码(WordPress)

php 将类添加到WordPress摘录

php 将类添加到WordPress摘录

php 将类添加到WordPress摘录

php 在Wordpress中手动输出摘录