Lisa23 Posted May 28, 2012 Share Posted May 28, 2012 Hi have this function that is meant to return all the post of category news, the problem is that is only returning one post of that category I want to return all the post. <?php //function to display the third last news published function wptuts_third_last_news_shortcode($atts, $content=null) { query_posts( 'category_name=news' ); if (have_posts()) : while (have_posts()) : the_post(); $blog_link = get_permalink(); // this si the link $return_string = "<a href=" .$blog_link .">" . get_the_title() . "</a>" ; endwhile; endif; wp_reset_query(); return $return_string; } add_shortcode( 'third_last_news', 'wptuts_third_last_news_shortcode'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263261-functions-only-returning-one-post-when-it-should-show-all/ Share on other sites More sharing options...
trq Posted May 28, 2012 Share Posted May 28, 2012 You are over writing the $return_string variable on each iteration. Declare it before your loop starts and then append to it on each iteration. Quote Link to comment https://forums.phpfreaks.com/topic/263261-functions-only-returning-one-post-when-it-should-show-all/#findComment-1349181 Share on other sites More sharing options...
Lisa23 Posted May 28, 2012 Author Share Posted May 28, 2012 Hi not sure if thats how u mean but I managed to get it fixed like this <?php //function to display all the news titles and link to the story function wptuts_all_news_titles_shortcode($atts, $content=null) { query_posts(array('category_name' => 'news', 'order' => 'DESC' , 'offset' => 3)); // offset to skip the first 3 news if (have_posts()) : while (have_posts()) : the_post(); $blog_link = get_permalink(); // this will get the post url $return_string .= "<p><a href=" .$blog_link .">" . get_the_title() . "</a> </p>" ; endwhile; endif; wp_reset_query(); return $return_string; } add_shortcode( 'all_news_titles', 'wptuts_all_news_titles_shortcode'); ?> Thank you very much for the help Quote Link to comment https://forums.phpfreaks.com/topic/263261-functions-only-returning-one-post-when-it-should-show-all/#findComment-1349322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.