• Resolved Ronnieinspain

    (@ronnieinspain)


    Hi,
    Lost countles hours on this already and just cant figure it out… im creating a shortcode that will display a chosen number of custom posts with a chosen excerpt length; if i output with echo, it works, except, as expected it doesnt output where expected. If i return, it just shows one post only… Someone please point me in the right direction.

    Here’s the code:

    /*
     * function to show the last hotspots that were added
     * with posibility of changing the number displayed and the
     * lenght of the extract shown, in words.
     */
    function show_latest_hotspots( $atts ) {
    
    	$data = extract( shortcode_atts( array( 'number' => 3, 'lenght' => 30, ), $atts ) );
    
    	global $post;
    
    	// var_dump( $data );
    	// print_r( $lenght );
    
    	$args = array(
    		'post_type' 	=> 'ait-dir-item',
    		'posts_per_page' => $number,
    		'orderby' 		=> 'date',
    		'order' 		=> 'ASC',
    		 );
    
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		$url = get_permalink();
    		$title = get_the_title();
    		$excerpt = strip_tags( $post->post_content );
    
    		$excerptArr = str_word_count( $excerpt, 1);
    			if(count ( $excerptArr > $lenght )) {
    				$text = join( " ", array_slice( $excerptArr, 0, $lenght ) );
    				$text .= " <a href='$url'>Read More</a>";
    			} else {
    				$text = $excerpt;
    			}
    
    		$content = 	"<div class=\"text\">";
    		$content .= "<div class=\"title\"><h3><a href='$url'>".$title."</a></h3></div>";
    		$content .= $excerpt."</div><br /><!-- eof div.text-->";
    
    		// echo $content;
    		echo $content;
    
    	endwhile;
    
    	// Reset Post Data
    	wp_reset_postdata();
    }
    
    add_shortcode('show_latest_hotspots', 'show_latest_hotspots');
Viewing 1 replies (of 1 total)
  • Thread Starter Ronnieinspain

    (@ronnieinspain)

    Couldnt leave it alone and finally figured it out; was giving new values to $output on every iteration of the loop. So easy.

    This code works:

    /*
     * function to show the last hotspots that were added
     * with posibility of changing the number displayed and the
     * lenght of the extract shown, in words.
     */
    function show_latest_hotspots( $atts ) {
    
    	// global $lenght;
    	global $post;
    	$content = "";
    
    	$data = extract( shortcode_atts( array( 'number' => 3, 'lenght' => 30, ), $atts ) );
    
    	$args = array(
    		'post_type' 	=> 'ait-dir-item',
    		'posts_per_page' => $number,
    		'orderby' 		=> 'date',
    		'order' 		=> 'DESC',
    		 );
    
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		$url = get_permalink();
    		$title = get_the_title();
    		$myexcerpt = strip_tags( $post->post_content );
    
    		$excerptArr = str_word_count( $myexcerpt, 1);
    			if(count ( $excerptArr > $lenght )) {
    				$text = join( " ", array_slice( $excerptArr, 0, $lenght ) );
    				$text .= " <a href='$url'>Read More</a>";
    			} else {
    				$text = $myexcerpt;
    			}
    
    		$content .= "<div class=\"text\">";
    		$content .= "<div class=\"title\"><h3><a href='$url'>".$title."</a></h3></div>";
    		$content .= $myexcerpt."</div><br /><!-- eof div.text-->";
    
    	endwhile;
    
    	// Reset Post Data
    	wp_reset_postdata();
    
    	// echo $content;
    	return $content;
    
    }
    
    add_shortcode('show_latest_hotspots', 'show_latest_hotspots');
Viewing 1 replies (of 1 total)
  • The topic ‘Outputting WP_query data from within a function’ is closed to new replies.