• Resolved jimmyt1988

    (@jimmyt1988)


    I am trying to only show the first 100 characters of a post, which will be truncated with ‘…’. The code I am using so far is:

    <?php
        query_posts('cat=3&posts_per_page=3');
        if (have_posts()) :
           while (have_posts()) :
              the_post(); ?>
        <ul>
            <li>
                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); echo ": "; the_time(get_option('date_format')); ?></a><br />
                  <?php the_content() ?>
            </li>
        </ul>
        <?php  endwhile;
        endif;
        wp_reset_query();
    ?>

    I think it’s a php kind of thing, anyone help me?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try replacing:

    <?php the_content() ?>

    with:

    <?php the_excerpt();
    <p><a href="<?php the_permalink();?>"><?php _e('read more &hellip;');</a></p>

    Then edit functions.php and add:

    // Custom excerpt length
    function my_excerpt_length($length) {
    	return 100; // Or whatever you want the length to be.
    }
    add_filter('excerpt_length', 'my_excerpt_length');
    Thread Starter jimmyt1988

    (@jimmyt1988)

    You have been a great help. I am actually a little confused why it is applying the truncate to the first post iteration but not the remaining ones.

    <?php
        query_posts('cat=3&posts_per_page=3');
        if (have_posts()) :
            while (have_posts()) :
            the_post(); ?>
            <ul>
                <li>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); echo ": "; the_time(get_option('date_format')); ?></a><br />
    
                    <?php
                        function my_excerpt_length($length) {
                        	return 15;
                        }
                        add_filter('excerpt_length', 'my_excerpt_length');
                    ?>
    
                    <?php the_excerpt(); ?>
            </li>
        </ul>
        <?php  endwhile;
        endif;
        wp_reset_query();
    ?>
    Thread Starter jimmyt1988

    (@jimmyt1988)

    AH HAH!!!

    The function needed to be outside of the loop. Ofcourse.

    My bad.

    THANKSSSSSS

    Move:

    function my_excerpt_length($length) {
    	return 15;
    }
    add_filter('excerpt_length', 'my_excerpt_length');

    to functions.php and just use <?php the_excerpt();?> in your template file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘truncating the content output’ is closed to new replies.