• Resolved kongkang

    (@kongkang)


    I want to make the_excerpt characters can be an any characters.

    Example template structure

    First loop = the_excerpt() limit chars – 100
    Second loop = the_excerpt() limit chars – 200
    Third loop = the_excerpt() limit chars – 300

    How to do that?

Viewing 15 replies - 1 through 15 (of 21 total)
  • First loop

    <?
    $content = get_the_excerpt();
    echo substr($content, 0, 100);
    ?>

    2nd loop

    <?
    $content = get_the_excerpt();
    echo substr($content, 0, 200);
    ?>

    Third loop

    <?
    $content = get_the_excerpt();
    echo substr($content, 0, 300);
    ?>

    Thread Starter kongkang

    (@kongkang)

    hmmm.. not working. Still return to default value.

    Thread Starter kongkang

    (@kongkang)

    Simple way to make it work I replace the_excerpt to be

    <?php the_content_rss('', FALSE, '', 100); ?>
    <?php the_content_rss('', FALSE, '', 200); ?>
    <?php the_content_rss('', FALSE, '', 300); ?>

    and it’s worked but the function has been deprecated. hmmm..

    use <?php rss_feed_100() ?>, <?php rss_feed_200() ?>, <?php rss_feed_300() ?>

    Thread Starter kongkang

    (@kongkang)

    Fatal error: Call to undefined function rss_feed_100()

    <?php the_content_limit(100, ‘Read more>>’); ?>
    <?php the_content_limit(200, ‘Read more>>’); ?>
    <?php the_content_limit(300, ‘Read more>>’); ?>

    Paste the following code in Function.php file

    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    and

    The following code in index.php file

    <?php the_excerpt(); ?>

    Thread Starter kongkang

    (@kongkang)

    I want different length for my excerpt. Example on my theme have 3 sections.

    Section 1 – I want the excerpt length will be 100 characters.
    Section 2 – I want the excerpt length will be 200 characters.
    Section 3 – I want the excerpt length will be 300 characters.

    you could use a self-made function; added to functions.php of your theme:

    function the_excerpt_dynamic($length) { // Outputs an excerpt of variable length (in characters)
    global $post;
    $text = $post->post_exerpt;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    }
    $text = strip_shortcodes( $text ); // optional, recommended
    $text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' to keep some formats; optional
    
    $text = substr($text,0,$length).' [...]';
    echo apply_filters('the_excerpt',$text);
    }

    and instead of the_excerpt(); you would use the_excerpt_dynamic(100);

    Thread Starter kongkang

    (@kongkang)

    Thanks alchymyth for the code, but the the_excerpt_dynamic(100); still returned to default value. I tried on twenty ten theme.

    you might be changing the code in the wrong spot;

    could you paste the file that you just changed, into a https://wordpress.pastebin.com/ and post the link to it here?

    ps:
    it might help if you could describe in more detail with an example what you like to achieve; and post a link to your site.

    Thread Starter kongkang

    (@kongkang)

    Here the code, twenty ten theme

    loop.php https://wordpress.pastebin.com/x5ANEzGM
    functions.php https://wordpress.pastebin.com/P0ULDRDf

    it might help if you could describe in more detail with an example what you like to achieve; and post a link to your site.

    I want something like this.. in the default loop.php (twenty ten) in search will shown 100 chars, index will be 200 chars, on category 300 chars.. something like that by examples.

    So I can implement it to another custom theme if it worked on twenty ten theme.

    both files look ok;

    i installed the same code into my local test blog; and it seems to work.
    (i get the excerpt on the front page and a slightly shorter excerpt on the archive pages)

    error correction:
    There is only a small error in the function ( $post_excerpt mis-spelled – correction below) that would have prevented the handwritten excerpt from being shown (shortend).

    global $post;
    $text = $post->post_excerpt;

    that is pretty much all i can contribute.

    there is the possibility that someone else might find something more, particular if they would have a chance to get a link to your live site.


    have you tried to de-activate all plugins, to see if that resolves the issue?
    and if it does, then re-activated one plugin after the other to find out which one might have caused the issue?

    Thread Starter kongkang

    (@kongkang)

    Thanks mate! now it worked after you fix the miss-spelled. I’ll email you when site goes live.. Awesome alchymyth.

    Thanks all, answered all my questions. You guys wprock.

    So is a custom function like that the only way to achieve such an effect? I tried it real quick to test it out and it seemed to work, however it wasnt stripping the tags correctly so it screwed up my css formatting.

    Im still learning this stuff and that function was a bit over my head compared to the basic one the codex offers.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Dynamic the_excerpt’ is closed to new replies.