admin管理员组文章数量:1122846
I'm trying to show the last 5 posts from a specific category, which will be linked to a function so i can insert the shortcode in a Wordpress page. The code i have is as below, it does everything i need (although i want to add featured image too) except it does not show posts from a specific category.
I've tried numerous things, but cannot find a working fix.
function Last5posts()
{
$args = array( "showposts" => 5, "category" => 3 );
query_posts($args);
$content = "";
if( have_posts() ) :
while( have_posts() ) :
the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= "<div class='latest-posts'>";
$content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>\n";
$content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
$content .= "</div>";
endwhile;
wp_reset_query();
endif;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
I have tried replacing lines 3 and 4 with the code below, but it throws an error "syntax error, unexpected '}' on line 31".
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
Any help would be greatly appreciated.
I'm trying to show the last 5 posts from a specific category, which will be linked to a function so i can insert the shortcode in a Wordpress page. The code i have is as below, it does everything i need (although i want to add featured image too) except it does not show posts from a specific category.
I've tried numerous things, but cannot find a working fix.
function Last5posts()
{
$args = array( "showposts" => 5, "category" => 3 );
query_posts($args);
$content = "";
if( have_posts() ) :
while( have_posts() ) :
the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= "<div class='latest-posts'>";
$content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>\n";
$content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
$content .= "</div>";
endwhile;
wp_reset_query();
endif;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
I have tried replacing lines 3 and 4 with the code below, but it throws an error "syntax error, unexpected '}' on line 31".
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
Any help would be greatly appreciated.
Share Improve this question asked Mar 28, 2013 at 23:28 Nikki MatherNikki Mather 3161 gold badge4 silver badges16 bronze badges 4- Your second code sample - using WP_Query - is the way to go. Can you show that version? Fixing the syntax error should be straightforward. Using query_posts (in your first sample) is a bad idea. – vancoder Commented Mar 28, 2013 at 23:54
- The problem with your query_posts is probably that you should be using 'cat', not 'category'. But again - don't use query_posts. – vancoder Commented Mar 28, 2013 at 23:56
- did you also correct lines 6, 7, and 8? – Michael Commented Mar 29, 2013 at 10:00
- No, i'm a novice where PHP is concerned and wouldn't even know what needs fixing. – Nikki Mather Commented Mar 29, 2013 at 15:56
2 Answers
Reset to default 0Here is the correct way to run this. I am using 'category_name'
in theme code lately as it is much easier to read and remember. It uses the category slug. Here I am retrieving the last 5 posts in "uncategorized".
function Last5posts() {
$args = array( 'posts_per_page' => 5, 'category_name' => 'uncategorized');
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) :
$last_5_posts_query->the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= '<div class="latest-posts">';
$content .= '<h3><a href='.$link.' target="_top">'.$title.' / '.$date. '</a></h3>';
$content .= '<p class="excerpt">' .get_the_excerpt(). '</p>';
$content .= '</div>';
endwhile;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
Just simply replace the $args
with this one.
$args = array('posts_per_page' => 5, 'post_type' => 'post', 'post_status' => 'publish','orderby'=>'ASC' );
And this show you last 5 posts.
本文标签: phpShow last 5 posts from specific category
版权声明:本文标题:php - Show last 5 posts from specific category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295513a1929586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论