个性化阅读
专注于IT技术分析

主题wpexplorer Today主题的WordPress摘录字符限制

我需要帮助修复http://wpexplorer-demos.com/today/page/2/今日主题。

frontbox框采用随机大小。我如何解决从单词到字符的问题。还有图像。任何帮助PLZ真的很感激。我不是编码员。

/**
 * Custom excerpts based on wp_trim_words
 * Created for child-theming purposes
 * 
 * @link  http://codex.wordpress.org/Function_Reference/wp_trim_words
 * @since 1.0.0
 */
function wpex_excerpt( $length = 45, $readmore = false ) {

    // Get global post data
    global $post;

    // Check for custom excerpt
    if ( has_excerpt( $post->ID ) ) {
        $output = $post->post_excerpt;
    }

    // No custom excerpt...so lets generate one
    else {

        // Redmore text
        $readmore_text = get_theme_mod( 'entry_readmore_text', esc_html__( 'read more', 'today' ) );

        // Readmore link
        $readmore_link = '<a href="'. get_permalink( $post->ID ) .'" title="'. $readmore_text .'">'. $readmore_text .'<span class="wpex-readmore-rarr">&rarr;</span></a>';

        // Check for more tag and return content if it exists
        if ( strpos( $post->post_content, '<!--more-->' ) ) {
            $output = get_the_content( '' );
        }

        // No more tag defined so generate excerpt using wp_trim_words
        else {

            // Generate excerpt
            $output = wp_trim_words( strip_shortcodes( get_the_content( $post->ID ) ), $length );

            // Add readmore to excerpt if enabled
            if ( $readmore == true ) {
                $output .= apply_filters( 'wpex_readmore_link', $readmore_link );
            }

        }

    }

    // Apply filters and echo
    echo apply_filters( 'wpex_excerpt', $output );

}

#1


我已经为你编辑过:

**
 * Custom excerpts based on wp_trim_words
 * Created for child-theming purposes
 * 
 * @link  http://codex.wordpress.org/Function_Reference/wp_trim_words
 * @since 1.0.0
 */
function wpex_excerpt( $length = 45, $readmore = false ) {

    // Get global post data
    global $post;

   $max_characters = 40;

    // Check for custom excerpt
    if ( has_excerpt( $post->ID ) ) {
        $output = mb_strimwidth($post->post_excerpt, 0, $max_characters, '...');
    }

    // No custom excerpt...so lets generate one
    else {

        // Redmore text
        $readmore_text = get_theme_mod( 'entry_readmore_text', esc_html__( 'read more', 'today' ) );

        // Readmore link
        $readmore_link = '<a href="'. get_permalink( $post->ID ) .'" title="'. $readmore_text .'">'. $readmore_text .'<span class="wpex-readmore-rarr">&rarr;</span></a>';

        // Check for more tag and return content if it exists
        if ( strpos( $post->post_content, '<!--more-->' ) ) {
  $output = mb_strimwidth(get_the_content( '' ), 0, $max_characters, '...');
        }

        // No more tag defined so generate excerpt using wp_trim_words
        else {

            // Generate excerpt
            $output_to_trim = wp_trim_words( strip_shortcodes( get_the_content( $post->ID ) ), $length );
$output = mb_strimwidth($output_to_trim, 0, $max_characters, '...');    
            // Add readmore to excerpt if enabled
            if ( $readmore == true ) {
                $output .= apply_filters( 'wpex_readmore_link', $readmore_link );
            }

        }

    }

    // Apply filters and echo
    echo apply_filters( 'wpex_excerpt', $output );

}

你可以通过更改$ max_characters变量中的数字来更改此代码中的最大字符数。

我使用了mb_strimwidth()函数并将其返回到$ output变量:https://secure.php.net/manual/pl/function.mb-strimwidth.php

通过示例, 你应该了解发生了什么:

 <?php
echo mb_strimwidth("Hello World", 0, 10, "...");
// outputs Hello W...
?>

该函数需要像” Hello World”这样的字符串, 字符串的开头(如0), 最大字符数(如10), 最后你可以选择摘录的漂亮结尾(如…)。

赞(0) 打赏
未经允许不得转载:srcmini » 主题wpexplorer Today主题的WordPress摘录字符限制
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏