简体   繁体   中英

Problem returning excerpt with specified number of characters

I'm using the following code to grab a string and return a spesific number of characters

<div class="top_posts">
<ul>';
}
elseif($i>1 && $i<=$number_of_posts) 
{
$retour.= "\n" . '<li>';
if($image_url) { $retour .= '<a href="' . get_permalink() . '"><img src="' . get_bloginfo('template_directory') . '/js/timthumb.php?src=' . $image_url . '&amp;h=120&amp;w=180&amp;zc=1" alt="" /></a>'; }
$retour .= '<h6><a href="' . get_permalink() . '">' . the_title("","",false) . '</a></h6>';
$retour.= get_wpe_excerpt('wpe_popular_posts');
$retour.='</li>';
if($i%2==1) 
$retour.= "\n" . '<li class="clear">&amp;</li>'; 
}
$i++;
endforeach;
$retour.='</ul></div>';
return $retour;
}
add_shortcode('popular-posts', 'popular_posts_code');

The chunk at issue is this part

$retour.= get_wpe_excerpt('wpe_popular_posts');

which calls to

function wpe_popular_posts($length) {
    return 55;
}

However I'm still getting the full text string untrimmed - any help appreciated.

//update

The get_wpe_excerpt function looks like this

function get_wpe_excerpt($length_callback='', $more_callback='') {
    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>';
    return $output;
}

You want to return the first 55 characters of the string?

Try substr($input, 0, 55);

However I really don't understand what's going on with that call to $retour.= get_wpe_excerpt('wpe_popular_posts');... so maybe I am misunderstanding what you are trying to achieve?

Now sure how the get_wpe_excerpt() function is done. You may want to try the simple code I used in several WordPress themes.

implode(' ', array_slice(explode(' ', get_the_content()), 0, 55));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM