Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php最大摘录长度不必为长度(不包括html/css)_Php_Wordpress - Fatal编程技术网

php最大摘录长度不必为长度(不包括html/css)

php最大摘录长度不必为长度(不包括html/css),php,wordpress,Php,Wordpress,我在使用以下命令时遇到问题: <?php echo excerpt(15); ?> 您有三种选择(可能更多,这并不意味着详尽无遗) 放弃wordpress功能。通过使用自定义PHP将$post->post_内容传递给您自己的函数,您可以使用PHP的XML操作函数,或者使用正则表达式来删除某些标记 在运行摘录之前,请检查您的帖子内容。如果您可以通过检查“thisextract()函数来自何处?”的第一个位置来检查是否存在以前15个字符开头的标记?@mopo922是的!谢谢,添加了!也

我在使用以下命令时遇到问题:

<?php echo excerpt(15); ?>
您有三种选择(可能更多,这并不意味着详尽无遗)

  • 放弃wordpress功能。通过使用自定义PHP将$post->post_内容传递给您自己的函数,您可以使用PHP的XML操作函数,或者使用正则表达式来删除某些标记


  • 在运行摘录之前,请检查您的帖子内容。如果您可以通过检查“this
    extract()
    函数来自何处?”的第一个位置来检查是否存在以前15个字符开头的标记?@mopo922是的!谢谢,添加了!也许您应该在这里询问:
    function excerpt($limit) {
          $excerpt = explode(' ', get_the_excerpt(), $limit);
          if (count($excerpt)>=$limit) {
            array_pop($excerpt);
            $excerpt = implode(" ",$excerpt).'...';
          } else {
            $excerpt = implode(" ",$excerpt);
          } 
          $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
          return $excerpt;
        }
    
        function content($limit) {
          $content = explode(' ', get_the_content(), $limit);
          if (count($content)>=$limit) {
            array_pop($content);
            $content = implode(" ",$content).'...';
          } else {
            $content = implode(" ",$content);
          } 
          $content = preg_replace('/\[.+\]/','', $content);
          $content = apply_filters('the_content', $content); 
          $content = str_replace(']]>', ']]&gt;', $content);
          return $content;
        }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    
    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = 35;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '...');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }