WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Limit text lenght on post_meta (2 posts)

  1. Gregoire Noyelle
    Member
    Posted 2 years ago #

    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.

    Thanks
    Grégoire

  2. Gregoire Noyelle
    Member
    Posted 2 years ago #

    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)
      array_pop($words);
      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);?>
    </p>
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic