Support » How-To and Troubleshooting » [Resolved] Limit text lenght on post_meta

[Resolved] Limit text lenght on post_meta

  • Hi
    I have some custom post type with custom meta boxes. On one of this boxes, I need to limit text length on the archive page.
    I made a google search but I find nothing about that. Any idea?
    Here is my query to get the meta:

    <?php if ( get_post_meta($post->ID, '_gncom_mbe_bio' ,true) ) : ?>
    <?php echo get_post_meta($post->ID, '_gncom_mbe_bio' ,true); ?>
    <?php endif; ?>

    “_gncom_mbe_bio” is the value for the biography custom meta boxe.
    Here is the website with the custom post type archive page.


Viewing 1 replies (of 1 total)
  • I’ve found the solution

    First, you create the function:

    function string_limit_words($string, $word_limit) {
      $words = explode(' ', $string, ($word_limit + 1));
      if(count($words) > $word_limit)
      return implode(' ', $words);

    Secondo you use the function with a variable in your template:

    <?php if ( get_post_meta($post->ID, '_gncom_mbe_bio' ,true) ) : ?>
    <p class="fiche-atelier">
    <?php $extrait_bio = get_post_meta($post->ID, '_gncom_mbe_bio' ,true);
    echo string_limit_words($extrait_bio,60);?>
    <?php endif; ?>

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Limit text lenght on post_meta’ is closed to new replies.