• Hello,
    I have rebuilt an old wordpress site that was using a discontinued theme that kept getting hacked. Everything is fine except the old theme had a function that auto-truncated the blog posts and the new one does not. I’m using the Gantry5/Hydrogen theme btw.

    There are about 300 posts in this site none of them having a “read more” inserted nor are any of them using excerpts. I’m trying to avoid going and creating an excerpt for each post.

    I’ve tried the Advanced Excerpt plugin but it did not work nor has it been tested with 4.7.1. I’ve searched for a solution and I keep getting referred to ‘wp-includes –> formatting.php ” but it looks like this file the ‘ wp_trim_words ‘ function only applies if I use the excerpt feature in each post and that is what I’m trying to avoid doing.

    Any ideas on how I can do this, is there a function I can use to automatically cut off the post after 100 characters, or after 25 words or something like that?

    • This topic was modified 9 years, 4 months ago by traveler. Reason: I added the tag excerpts
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In this theme, are you using the_excerpt or the_content to display post content?

    Where did you obtain this theme?

    Thread Starter traveler

    (@visualeight)

    Hello Steve,
    Thanks for your reply. The theme is the Rockettheme base boilerplate if you will, Gantry5/hydrogen. I use it when building Joomla sites as well. They have a content array particle that will not allow pagination, I’ve gotten that from the theme devs, so now I need to rebuild it using the WordPress blog page.

    I’m much newer to WP than Joomla so I may be mistaken about your question, but my theme’s archive.php file uses get_post_type() to pull in the blog posts.

    I’ve posted the archive.php file:

    <?php
    /**
    * @package Gantry 5 Theme
    * @author RocketTheme http://www.rockettheme.com
    * @copyright Copyright (C) 2007 – 2016 RocketTheme, LLC
    * @license GNU/GPLv2 and later
    *
    * http://www.gnu.org/licenses/gpl-2.0.html
    */

    defined(‘ABSPATH’) or die;

    /*
    * The template for displaying Archive pages.
    *
    * Used to display archive-type pages if nothing more specific matches a query.
    * For example, puts together date-based pages if no date.php file exists.
    *
    * Learn more: http://codex.wordpress.org/Template_Hierarchy
    */

    $gantry = Gantry\Framework\Gantry::instance();
    $theme = $gantry[‘theme’];

    // We need to render contents of <head> before plugin content gets added.
    $context = Timber::get_context();
    $context[‘page_head’] = $theme->render(‘partials/page_head.html.twig’, $context);

    $templates = [‘archive.html.twig’, ‘index.html.twig’];

    $context[‘title’] = __(‘Archive’, ‘g5_hydrogen’);
    if (is_day()) {
    $context[‘title’] = __(‘Archive:’, ‘g5_hydrogen’) . ‘ ‘ . get_the_date(‘j F Y’);
    } else if (is_month()) {
    $context[‘title’] = __(‘Archive:’, ‘g5_hydrogen’) . ‘ ‘ . get_the_date(‘F Y’);
    } else if (is_year()) {
    $context[‘title’] = __(‘Archive:’, ‘g5_hydrogen’) . ‘ ‘ . get_the_date(‘Y’);
    } else if (is_tag()) {
    $context[‘title’] = single_tag_title(”, false);
    } else if (is_category()) {
    $context[‘title’] = single_cat_title(”, false);
    array_unshift($templates, ‘archive-‘ . get_query_var(‘cat’) . ‘.html.twig’);
    } else if (is_post_type_archive()) {
    $context[‘title’] = post_type_archive_title(”, false);
    array_unshift($templates, ‘archive-‘ . get_post_type() . ‘.html.twig’);
    }

    $context[‘posts’] = Timber::get_posts();

    Timber::render($templates, $context);

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Yeah, this theme does things in a very non-standard way. I think the best thing to do is to go back to their own support forums and ask.

    Hi,

    Yes of course this is possible. You can edit the .html.twig files under theme/views/ directory and use the get_preview Timber function.

    This is example on how to do it in the views/partials/content.html.twig file.
    Edit this file and replace the :

    {{ post.content }}

    with:

    {{ post.get_preview(50) }}

    where 50 is “The number of words that WP should use to make the tease. If you’ve set a post_excerpt on a post, we’ll use that for the preview text; otherwise the first X words of the post_content”

    Thanks,
    Jakub

    • This reply was modified 9 years, 4 months ago by Gantry.
    Thread Starter traveler

    (@visualeight)

    Hello Jakub, I will try that. Thank you very much, it would save me so much time!

    Thread Starter traveler

    (@visualeight)

    @jakub, thank you this worked perfectly!

    Great πŸ™‚

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

The topic ‘Auto truncate blog post excerpts’ is closed to new replies.