• Hi, can somebody please help me, I use this code to show random 5 post on home page from my category No. 1. And now it show me a title of this post, but actually I need to show me the 12 first words of this post, I read to i can use excerpt function but I am not so good in PHP and I just do not know how to import this code in my, I try it something but I always get same content 5 time… This is my code below:

    <?php
    $rand_posts = get_posts( array ( 'cat' => '1', 'orderby' => 'rand' ) );
    foreach( $rand_posts as $post ) :
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>

    Also on this page I have more code, but I do not know its need to show it here?

Viewing 4 replies - 1 through 4 (of 4 total)
  • function new_excerpt_length($length) {
    	return 12;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    added to functions.php will make the excerpt length 12 words

    <?php
    $rand_posts = get_posts( array ( 'cat' => '1', 'orderby' => 'rand' ) );
    foreach( $rand_posts as $post ) :
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    Will display it in your code, after the title. The change to functions.php will affect all calls to the excerpt tho. If you need to limit the length of the excerpt on just this page, we will have to alter the code a bit

    http://wpengineer.com/1909/manage-multiple-excerpt-lengths/
    Shows how to set variable excerpt lengths for use in different templates

    Thread Starter dados

    (@dados)

    uff yes it need to show only on this page.. 🙁

    I edited my post, if you need to alter the excerpt length for different templates, you’ll need to use a different approach.

    Thread Starter dados

    (@dados)

    Thank you very much… Only one more question, if you can help me.. I have some custom fields, and he show me in post, can I you remove it from post… If you can see this action in live http://brandsane.com/starbarz/
    it is:
    User Artist Name /User Song Name /User Album Name

    also the value of this custom field is Artist Name, Song Name, Album Name if you can help me great, if not I am very grateful for the help…

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘excerpt function, please help me!’ is closed to new replies.