0

On my single.php page I am trying to create a function to give me a custom length excerpt of a specific post by ID.

Below are my two functions I am running.

/* Custom get_the_excerpt to allow getting Post excerpt by ID */
function custom_get_the_excerpt($post_id) {
  global $post;
  $save_post = $post;
  $post = get_post($post_id);
  $output = get_the_excerpt($post);
  $post = $save_post;
  return $output;
}

/* Change Excerpt length */
function excerpt($num, $post_id = '') {
    $limit = $num+1;
    $excerpt = explode(' ', custom_get_the_excerpt($post_id), $limit);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."&#8230";
    echo $excerpt;
}

What I'm using to call the function.

<?php $previous = get_previous_post();
echo excerpt('30', $previous -> ID); ?>

The issue I am running into is $post is giving me the previous post information, however, when I pass that into get_the_excerpt it returns the current post excerpt rather than the previous post excerpt.

EDIT

Changed function to this after several people told me I can just pass the $post_id to get_the_excerpt()

  /* Change Excerpt length */
function excerpt($num, $post_id = '') {
    $limit = $num+1;
    $excerpt = explode(' ', get_the_excerpt($post_id), $limit);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."&#8230";
    echo $excerpt;
}

Still no change.

3

Try this one

/* Change Excerpt length */
function excerpt($num, $post_id) { // removed empty default value
    $limit = $num+1;
    $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
    $excerpt = explode(' ', $excerpt, $limit);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."&#8230";
    echo $excerpt;
}

Another solution - using setup_postdata($post); and wp_reset_postdata();

function custom_get_the_excerpt($post_id) {
  global $post;
  $save_post = $post;
  $post = get_post($post_id);
  setup_postdata($post);
  $output = get_the_excerpt($post);
  wp_reset_postdata();
  $post = $save_post;
  return $output;
}
  • And leave it in custom_get_the_excerpt()? Because that didn't change anything. – Landon Call Feb 22 '17 at 21:31
  • yes, leave it on both, or try 2nd option. I updated my answer. – MaximOrlovsky Feb 22 '17 at 21:38
  • After your edit I now don't see a reason to even pass it to my custom_get_the_excerpt() can't I skip that step and go right to get_the_excerpt($post_id)? – Landon Call Feb 22 '17 at 21:39
  • Yeap, you don't need it. You can skip that step. – MaximOrlovsky Feb 22 '17 at 21:41
  • Ok well either way I am still getting the current post excerpt instead of the previous or next even when I go right to get_the_excerpt($post_id) – Landon Call Feb 22 '17 at 21:42
1

You don't need to use custom CODE just to change excerpt length. There is a default WordPress filter for that: excerpt_length. You just need to use it.

To change to the same excerpt length every where on the theme

If you use the same excerpt length everywhere on your site, then just add the following CODE in your theme's functions.php file:

function my_custom_excerpt_length( $length ) {
    // set the number you want
    return 20;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );

and then in your template file (for example single.php), simply call:

echo get_the_excerpt( $post_id );

To use different excerpt lengths on the theme

If you want to use different excerpt length on different places, then it makes sense to create a custom function so that you may call it easily. Even then you can just make use of WordPress supplied functions and filters. In this case, use the following functions in functions.php file:

function set_custom_excerpt_length( $length ) {
    global $custom_excerpt_length;
    if( ! isset( $custom_excerpt_length ) ) {
        return $length;
    }
    return $custom_excerpt_length;
}

function my_excerpt( $post_id = null, $length = 55 ) {
    global $custom_excerpt_length, $post;
    $custom_excerpt_length = $length;
    if( is_null( $post_id ) ) {
        $post_id = $post->ID;
    }
    add_filter( 'excerpt_length', 'set_custom_excerpt_length', 999 );
    echo get_the_excerpt( $post_id );
    remove_filter( 'excerpt_length', 'set_custom_excerpt_length', 999 );
}

Then use it like so in your template file:

my_excerpt( $previous->ID, 30 );

You can of course use custom functions used by other answers, however, you'll miss out on WordPress default behaviour in that case.

Note: If you are using the loop properly, you shouldn't have to call wp_reset_postdata() on every excerpt function call.

  • I use several different excerpts lengths depending on where on my site you are. My question was about get_the_excerpt() not giving me my post by ID and just showing the current post instead, and not about excerpt length. – Landon Call Feb 22 '17 at 22:27
  • Yes, I saw that in the conversation. You may use wp_reset_postdata() for that, however, you shouldn't have to. If you use wp_reset_postdata() that way, that means you are not using the loop properly. It works if you use wp_reset_postdata() in every call of the custom excerpt function, but that's very inefficient. So the main problem is on your template file on how you are getting the previous post. – Fayaz Feb 22 '17 at 22:34
  • Well that's good to know. I may need to check my template then. I am just glad it's working haha. – Landon Call Feb 22 '17 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.