Viewing 7 replies - 1 through 7 (of 7 total)
  • I think we’ll need a bit more explanation than that, if you can give it…

    Cheers

    PAE

    Thread Starter webipsum

    (@ipsum)

    peredur.

    Today I use the functions.php:

    function short_title($num) {
    $limit = $num+1;
    $title = str_split(get_the_title());
    $length = count($title);
    if ($length>=$num) {
    $title = array_slice( $title, 0, $num);
    $title = implode("",$title)."...";
    echo $title;
    } else {
    the_title();
    }}

    And the index.php:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Read: <?php the_title_attribute(); ?>"><?php short_title(73); ?></a>

    But the limit is in characters and I want it in words.

    Tks…

    Use explode() using a space as the boundary character to create an array of words. Put the first x number of words back together from the first x array locations, using implode()

    See:

    http://www.php.net/manual/en/function.implode.php
    http://www.php.net/manual/en/function.explode.php

    Cheers

    PAE

    Thread Starter webipsum

    (@ipsum)

    I am using to limit the display of the content.
    Functions.php:

    function excerpt($limit) {
    $excerpt = explode(' ', get_the_content(), $limit);
    if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'...';
    } else {
    $excerpt = implode(" ",$excerpt);
    }
    $excerpt = preg_replace('<code>\[[^\]]*\]</code>','',$excerpt);
    return $excerpt;
    }

    Index.php
    <?php echo excerpt(70); ?>

    But I’m not having success in the title.
    I’m not able to write the correct code.

    Tks for your attention.

    What’s wrong with:

    function title_excerpt($limit_in_words) {
      $excerpt_array = explode(' ', get_the_title(), $limit_in_words - 1);
      $excerpt = implode(' ', $excerpt_array);
      // manipulate the string if you need to
      return $excerpt;
    }

    I haven’t tried it, but it should work.

    HTH

    PAE

    Thread Starter webipsum

    (@ipsum)

    functions.php:

    function title_excerpt($limit_in_words) {
      $excerpt_array = explode(' ', get_the_title(), $limit_in_words - 1);
      $excerpt = implode(' ', $excerpt_array);
      // manipulate the string if you need to
      return $excerpt;
    }

    index.php:
    <?php title_excerpt(10); ?>

    I tested, but no word of the title was displayed.

    Well, try it in a standalone application and see what’s going on. In other words, construct a PHP page with this code in it and pass constant values into it to see what you get back. At least that way you’ll be able to see if the function works given that there is a reasonable value in the limit and in the title string. Once you’re sure of that you can check to see what get_the_title() is returning – by just echoing it in your page. That should give you enough to sort things out.

    It’s just a matter of doing the programming.

    Cheers

    PAE

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to limit the display by number of words? (Title in the Loop)’ is closed to new replies.