Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question
add comment

2 Answers

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

share|improve this answer
    
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
add comment
up vote 0 down vote accepted
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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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