I have a problem with a shortcode that I'm creating.
I have 5-10 different custom post types, and rather than creating a template for every individual post type, Id rather use a normal page and import the posts via a shortcode.
So to explain further, I have a page called Attractions and a post type called 'Attraction'. On the page I insert the shortcode -> [feed type="attraction" limit="5"]
This outputs all the posts under the custom post type "attraction".
The problem I'm having, is getting it to paginate correctly. Ive read a thousand ways to paginate custom post types, and the code I'm currently using tends to work according to so many people, however I cannot get this to work inside the shortcode. Is there a reason for this?
Currently the page displays fine, with five posts, but doesn't echo the next, previous posts links. If I type in the url + /page/2 the second page correctly displays with the next 5 posts in the section. So it seems pagination is working, however I cant access them because no Next/Previous Link appears.
The benefit to doing it this way, is that I can use a normal page, import the posts, and I can include content above and below the shortcode called in.
Hope this all makes sense.
Heres my shortcode code -->
function section_feed_shortcode( $atts ) {
extract( shortcode_atts( array( 'limit' => -1, 'type' => 'post'), $atts ) );
global $paged;
$q = new WP_Query( array (
'posts_per_page' => $limit,
'post_type' => $type,
order => 'ASC',
orderby =>'menu_order',
'paged' => $paged ) );
$list = ' ';
while ( $q->have_posts() ) { $q->the_post();
$list .= '<article class="listing-view clearfix">'
. '<div class="listing-content">'
. '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'
.'<p>' . get_the_excerpt() . '</p>'
. '<a href="' . get_permalink() . '">' . 'View »' . '</a>'
. '</div>'
. '<a class="listing-thumb" href="' . get_permalink() . '">' . get_the_post_thumbnail($page->ID, 'listing-thumb') . '<span></span></a>'
. '</article>';
}
return
'<div class="listings clearfix">'
. $list
. '<div class="nav-previous">' . next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>' ) ) . '</div>'
. '</div>' .
wp_reset_query();
}
add_shortcode( 'feed', 'section_feed_shortcode' );