0

So, I have a walker nav menu who add the page excerpt to all page links in the nav menu.

Now, I want to build a custom post excerpt for the blog archive.

All works fine when the nav menu doesn't meet the Blog Archive. On the blog archive the nav menu's excerpts get the filter of the blog archive. This is because the excerpt filter checks on the current post.

Is there a way for the excerpt filter to check on the queried item in stead of the current post? As with the walker menu...

Here are the filters I use:

For the walker menu

function q_menu_item_custom_output( $item_output, $item, $depth, $args ) {
    $excerpt_el = '';
    $p_id = $item->object_id;
    if ( has_excerpt( $p_id ) ) {
        $excerpt_el = '<span class="excerpt">' . get_the_excerpt($p_id) . '</span>';
    }
    return $item_output . $excerpt_el;
}
add_filter( 'walker_nav_menu_start_el', 'q_menu_item_custom_output', 10, 4 );

and for the post excerpt:

function q_excerpt_custom( $excerpt ) {
    if ( get_post_type( ) == 'post' ) :
        $excerpt_custom = '<a class="card-permalink" href="' . get_permalink() . '" rel="nofollow">&nbsp; HA HA HA </a>';
        return $excerpt_custom;
    endif;
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'q_excerpt_custom' );

Thank you!

1 Answer 1

0

Looking at the documentation for the get_the_excerpt filter, we can see that it receives the WP_Post object representing either the current post or the post which was passed in to the get_the_excerpt() function call.

Thus,

function q_excerpt_custom( $excerpt, $post ) {
    if ( get_post_type( $post ) == 'post' ) :
        $excerpt_custom = '<a class="card-permalink" href="' . get_permalink( $post ) . '" rel="nofollow">&nbsp; HA HA HA </a>';
        return $excerpt_custom;
    endif;
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'q_excerpt_custom', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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