• I have this problem:

    I want that all posts/articles, published and future posts, will have automatically trimmed contents.

    The reason is when people reach the website, they will click in the articles they want; WP will redirect to the post showing a trimmed article (limited to X words or characters). All articles already have the source link, so all people are redirect to the source.

    How can I force WP to do this kind of thing?

    I just want to understand if:
    – I need a plugin that automatically does it (I was not able to find a right one);
    – I need to put some functions in the theme;
    – I need to change some default functions in WP.

    What I’m looking for, it’s like an RSS Aggregator works; in my case, we are not talking about feeds, just articles already published.

Viewing 7 replies - 1 through 7 (of 7 total)
  • In the theme where the content is outputted you likely have the_content()

    You could change this to:

    echo(substr(strip_tags(get_the_content()), 0, 100));

    where 100 is the number of characters you want to output.

    Thread Starter axop9

    (@axop9)

    It works, but: is there a way to not remove HTML tags? Because this code remove the formatting style, I have only a clean-text.

    Thread Starter axop9

    (@axop9)

    Is this correct for not have any changes in the formatting?

    <?php echo(substr(strip_tags(nl2br(get_the_content('')),"<img><b><strong><i><em><a>"), 0, 300)); ?>

    Well the problem is substr is a pretty basic function stripping 300 chars could land you in the middle of an HTML tag or not closing one which is bad times which is why i added it in there.

    Its not an easy problem :D. Bit of google and someone has this function you could maybe use instead:

    https://stackoverflow.com/questions/2398725/using-php-substr-and-strip-tags-while-retaining-formatting-and-without-break

    Other than that some other members might chime in.

    Thread Starter axop9

    (@axop9)

    In fact this is a big problem, because, how I said, all articles have also the source on the bottom. So with this code I have only the first 300 characters.
    If I use also this other code (below), I can let the source to be displayed:
    <?php echo(substr(strip_tags(nl2br(get_the_content('')),"<img><b><strong><i><em><a>"), -100, 100)); ?>

    But the problem is the same: it can cut html, receiving broken links or broken text, because an URL is different from post to post.

    I tested the two echos receiving a text with a broken img and the source cutted with broken link.

    I will check your link for more infos.

    To be honest you might want to consider re-structuring your WP setup to include some more structure:

    You could easily have some custom fields:
    Summary
    Link

    Then you have full control of what shows in parts of your template e.g. you can output a summary then output the link right under it.

    Thank you!
    <?php echo strip_tags(nl2br(get_the_content('')),"<img><b><strong><i><em><a>"); ?>

    But after apply this code I have a problem with
    [caption] [/caption] of images.

    At the moment the caption is visible as text:
    [caption id="attachment_75" align="alignnone" width="300"] xxxIMAGE-Caption-Textxxx[/caption]

    How I can fix it?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Cutting a post’ is closed to new replies.