0

I lifted the "continue reading" link functions from the Twenty Eleven theme and for the most part, it is working (works in RSS feeds).

I created a shortcode to display the latest post on the front static page using get_posts, however, the "continue reading" link just links to the front page instead of the post page.

function gavsiu_continue_reading_link() {
    return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Read more <span class="meta-nav">&raquo;</span>' ) . '</a>';
}

function gavsiu_auto_excerpt_more( $more ) {
    return ' &hellip;' . gavsiu_continue_reading_link();
}
add_filter( 'excerpt_more', 'gavsiu_auto_excerpt_more' );

function gavsiu_custom_excerpt_more( $output ) {
    if ( has_excerpt() && ! is_attachment() ) {
        $output .= gavsiu_continue_reading_link();
    }
    return $output;
}
add_filter( 'get_the_excerpt', 'gavsiu_custom_excerpt_more' );

Shortcode:

function gavsiu_recent_posts( $atts ) {
    extract( shortcode_atts( array(
        'cat'            => 3,
        'max_posts'      => 1
    ), $atts ) );

    $recent_posts = get_posts( array(
        'category'       => "{$cat}",
        'numberposts'    => "{$max_posts}"
    ) );

    foreach($recent_posts as $recent_post) : setup_postdata($recent_post);
        $post = '<h4 class="recent-post-title c0">' . $recent_post->post_title . '</h4>';
        $post .= '<p class="recent-content c0">' . get_the_excerpt() . '</p>';

        return $post;
    endforeach;
}

add_shortcode( 'recent_posts', 'gavsiu_recent_posts' );
0

You need to reset the post data after your get_posts loop since your calling setup_postdata. It would be better to remove the setup_postdata all together.

global $post;
$recent_posts = get_posts( $yourargshere );
    foreach($recent_posts as $post) : 
        $return = '<h4 class="recent-post-title c0">' . $post->post_title . '</h4>'; 
        $return .= '<p class="recent-content c0">' . apply_filters( 'the_excerpt', $post->post_excerpt ) . '</p>';
    return $return;
    endforeach;

You also need to change your add_filter to the_excerpt instead of get_the_excerpt

  • It doesn't show the excerpt this way, it's blank. Also, it adds the comments_template from the post to my front static page. – gavsiu May 29 '12 at 17:20
0
function gavsiu_recent_posts( $atts ) {
    extract( shortcode_atts( array(
        'cat'            => 3,
        'max_posts'      => 1
    ), $atts ) );

    $recent_posts = get_posts( array(
        'category'       => "{$cat}",
        'numberposts'    => "{$max_posts}",
        'offset' => 0
    ) );

    foreach($recent_posts as $post) : setup_postdata($post);
        $permalink = ' <a href="'. esc_url( get_permalink( $post->ID ) ) . '" title="Permalink to ' . esc_attr( $post->post_title ) . '" rel="bookmark">' . __( 'Read more <span class="meta-nav">&raquo;</span>' ) . '</a>';

        $return = '<h4 class="recent-post-title">' . $post->post_title . "</h4>\n";
        if ( $post->post_excerpt == '' ) :
            $return .= preg_replace( array( '/ <a href="http:\/\/gavsiu.com\/" title="Permalink to ".*?<\/a>/', '/<p .*?>/' ), array( $permalink, '<p>' ), get_the_excerpt() ) . "\n";
        else :
            $return .= '<p>' . $post->post_excerpt . $permalink . "</p>\n";
        endif;

        return $return;
    endforeach;
}

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.