0

I'm currently using the featured posts sidebar widget in the Academica wordpress theme. I'd like to know if there's a way to limit the number of characters displayed in the excerpt without resorting to installing yet another plugin with this functionality.

Looks like the code for this function of the widget is as follows.

if ( empty( $sidebars['sidebar-2'] ) ) {
    $widget = get_option( 'widget_academica-featured-posts-gallery', array( '_multiwidget' => 1 ) );
    $widget[2] = array(
        'title' => __( 'Latest Posts', 'academica' ),
        'category' => 0,
        'display' => 'list',
        'amount' => 4,
  • Is there a way to modify this code to achieve character limiting on featured posts excerpt? – Jimi Clapton Sep 15 '13 at 11:54
  • That is not all of the code. What you've posted does not echo or return anything. Is this the theme? – s_ha_dum Sep 15 '13 at 14:36
0

I am guessing that this is the theme you are talking about. If so, I don't see any specialized filters that would let you alter the character count of the excerpt. The widget does use get_the_excerpt() so you should be able to alter the character count with Core filters though.

function excerpt_trim_cb_wpse_114219($excerpt) {
  remove_filter('wp_trim_excerpt','excerpt_trim_cb_wpse_114219',1000);
  return substr($excerpt,0,5);
}
function insert_into_sidebar_wpse_114219($s) {
  if (
    isset($s['callback']) 
    && is_object($s['callback'][0])
    && 'Academica_Featured_Posts_Gallery' === get_class($s['callback'][0])) 
  {
    add_filter(
      'wp_trim_excerpt',
      'excerpt_trim_cb_wpse_114219',
      1000
    );
  }
}
add_action( 'dynamic_sidebar', 'insert_into_sidebar_wpse_114219' );
  • Thanks s_ha_dum. I would have that there would at least be some reference to the default char length somewhere but I guess not. Could you please elaborate on Core filters? Not sure I follow. – Jimi Clapton Sep 15 '13 at 15:36
  • Wordpress has a huge number of "hooks" built into the Core code. With these, you can alter functionality without hacking Core code. wp_trim_excerpt, from the code above, is one of those. dynamic_sidebar is another. – s_ha_dum Sep 15 '13 at 15:39
  • Okay, so for my understanding, wp_trim_excerpt is a hook or default WP function or action which I could modify to suit my requirements? Sorry for all the questions, love this stuff, keen to learn. – Jimi Clapton Sep 15 '13 at 16:00
  • wp_trim_excerpt is a hook (a filter in this case). Don't confuse hooks with functions, even if there is a function of the same name. See: wordpress.stackexchange.com/a/103644/21376 – s_ha_dum Sep 15 '13 at 16:04
  • If this solved the question please mark it as "Accepted". Look for the check mark on the left. – s_ha_dum Sep 15 '13 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.