how can I get my function to returl all post instead of just one

Discussion in 'PHP' started by macaela, May 28, 2012.

  1. #1
    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');
    ?> 
    PHP:

     
    macaela, May 28, 2012 IP
  2. stasiek

    stasiek Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hi,
    try something like that:
    
    [LEFT][COLOR=#000088][FONT=monospace]$return_string[/FONT][/COLOR][COLOR=#111111][FONT=monospace] .[/FONT][/COLOR][COLOR=#339933][FONT=monospace]=[/FONT][/COLOR][COLOR=#111111][FONT=monospace]  [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"<a href="[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#000088][FONT=monospace]$blog_link[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]">"[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#111111][FONT=monospace] get_the_title[/FONT][/COLOR][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"</a><br />"[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    instead of:
    
    [LEFT][COLOR=#000088][FONT=monospace]$return_string[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace]=[/FONT][/COLOR][COLOR=#111111][FONT=monospace]  [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"<a href="[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#000088][FONT=monospace]$blog_link[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]">"[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#111111][FONT=monospace] get_the_title[/FONT][/COLOR][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace].[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"</a>"[/FONT][/COLOR][COLOR=#111111][FONT=monospace] [/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    Why?
    Because when it is without dot it will be overwriting $return_string variable and returning only one link.
     
    stasiek, May 28, 2012 IP
  3. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Hi thanks that did the trick thanks alot here the whole code in case someone pumbs into it.

    <?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');
    ?>
    PHP:
     
    macaela, May 28, 2012 IP