【问题标题】:Custom Wordpress Excerpt by two paragraphs自定义 Wordpress 摘录两段
【发布时间】:2021-05-30 00:50:15
【问题描述】:

我找到了将摘录限制为 2 段或更多段的答案。但是,我发现的代码仅适用于实际摘录,不适用于我制作的自定义摘录。我做了两段摘录,一段用于首页上的某些帖子,另一段用于帖子页面。我想将该代码添加到我为帖子页面制作的自定义摘录中。我怎么做。

这是我创建的代码:

// Create the Custom Excerpts callback
function wpden_excerpt($length_callback = '', $more_callback = '')
{
    global $post;
    if (function_exists($length_callback)) {
        add_filter('excerpt_length', $length_callback);
    }
    if (function_exists($more_callback)) {
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p>' . $output . '</p>';
    echo $output;
}
// Custom Length 
function wpden_mag_len($length) {
    return 200;
}
function wpden_more_view($more){
global $post;
return '... <a class="view-article" href="' . get_permalink($post->ID) . '">' . __('', 'wpden') . '</a>';
}

我在我的 template_post.php 中调用了它:

<?php wpden_excerpt('wpden_mag_len','wpden_more_view'); ?>

我想用于我的自定义 wpden_excerpt 的代码是这样的:

add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );

function my_custom_excerpt($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    return $text;
}

来自this site 或来自另一个stack overflow question

$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
// Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
$wpse0001_excerpt = "<p>$wpse0001_excerpt</p>";
    $wanted_number_of_paragraph = 2;
    $tmp = explode ('</p>', $wpse0001_excerpt);
    for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
       if (isset($tmp[$i]) && $tmp[$i] != '') {
           $tmp_to_add[$i] = $tmp[$i];
       }
    }
$wpse0001_excerpt = implode('</p>', $tmp_to_add) . '</p>';

$wpse0001_excerpt = str_replace(']]>', ']]&gt;', $wpse0001_excerpt);

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

//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;

return $wpse0001_excerpt;

}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}

endif; 

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

问题是这些代码适用于 the_excerpt 并覆盖我制作的自定义摘录。我尝试像这样格式化代码:

function wpden_excerpt($text = '')
{
   global $post;
        if ( '' == $text ) {
                $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace('\]\]\>', ']]&gt;', $text);
                $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                $text = strip_tags($text, '<p>');
                $excerpt_length = 80;
                $words = explode(' ', $text, $excerpt_length + 1);
                if (count($words)> $excerpt_length) {
                        array_pop($words);
                        array_push($words, '[...]');
                        $text = implode(' ', $words);
                }
        }
        return $text;
}

并在模板中将其称为

<?php wpden_excerpt('text'); ?>

但它不起作用。出了什么问题,我该如何解决?

更新

我还需要帮助!!!我尝试了许多不同的自定义摘录组合并将摘录限制为一个段落,但我无法这样做...请帮助!!

【问题讨论】:

    标签: php html wordpress function


    【解决方案1】:

    感谢我兄弟的帮助,我能够编辑代码以显示自定义摘录的两个段落:

    // Create the Custom Excerpts callback
    function wpden_excerpt()
    {
        global $post;
    
       $output = get_the_content();
    
    $wanted_number_of_paragraph = 2;
    
    $tmp = explode ('</p>', $output);
    for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
       if (isset($tmp[$i]) && $tmp[$i] != '') {
           $tmp_to_add[$i] = $tmp[$i];
       }
    }
    $output = implode('</p>', $tmp_to_add) . '</p>';
    
        echo $output;
    
    }
    

    并在我的模板文件中将其称为

    <?php wpden_excerpt(); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2016-10-29
      • 2014-09-08
      • 1970-01-01
      相关资源
      最近更新 更多