Forums

Arthemia: How to add text to featured posts? (8 posts)

  1. stijlvollemannen
    Member
    Posted 1 year ago #

    I'm using the Arthemia theme and have posts displayed in the Featured section.
    However, I only get to see the Post title and date. Adding <?php the_excerpt(); ?> to index.php also didn't do the trick, this text was too long.
    How can I just display the first sentence of a post on my homepage?

    Blog: http://stijlvollemannen.nl

  2. stvwlf
    Member
    Posted 1 year ago #

    putting this code in your theme's functions.php file will limit the length of the_excerpt() to 20 characters. You can adjust 20 to whatever you want.

    <?php
    // Add custom excerpt length
    function custom_excerpt_length($length) {
      return 20;
    }
    add_filter('excerpt_length', 'custom_excerpt_length');
    ?>
  3. stijlvollemannen
    Member
    Posted 1 year ago #

    Thanks for the comment, but inserting this gives an error.

    This is the current code in function.php;

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    
    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    
    		$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$excerpt_length = apply_filters('excerpt_length', 90);
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '...');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }

    I just want to limit the excerpt length for the featured posts, the limitation should not apply to all other posts using excerpts as well...

    Any idea?

  4. stvwlf
    Member
    Posted 1 year ago #

    This should only be there once. From what you posted it is there twice

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');

    How do you know when a post is a featured post? Is it in a featured category? If it is, what is the category id # of that category on your site? From that the function can be modified to only create an excerpt in some conditions.

  5. stijlvollemannen
    Member
    Posted 1 year ago #

    Yes, the posts are in a category; ID 93.
    What should the code be and what do I need to delete or replace?

    Thanks so much for your quick repsonse!

  6. stvwlf
    Member
    Posted 1 year ago #

    The way I am setting this up you will not be able to have excerpts anyplace on your site other than on featured posts. If that is not what you want let me know how it should work.

    I didn't test the small change I made. Try it.

    You already have these two lines at the top - keep them

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');

    replace the existing function with this

    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    	if ( '' == $text && in_category(93) ) {
    		$text = get_the_content('');
    
    		$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text);
    		$excerpt_length = apply_filters('excerpt_length', 90);
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '...');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }

    If it doesn't work try changing the IF line to
    if ( '' == $text && in_category(93, $post->ID) ) {

  7. stijlvollemannen
    Member
    Posted 1 year ago #

    Thanks!

    What I really would like to have is:
    Featured posts: 50 characters (20 words, taken from original post
    Posts on homepage or on categorye page: use excerpt settings

    Does your code provide this?

  8. stvwlf
    Member
    Posted 1 year ago #

    What I sent is the code you posted with a few changes to limit where it is applied.

    The number 90 in the code is the number of characters. Change it to whatever you want.

Topic Closed

This topic has been closed to new replies.

About this Topic