Outputting WP_query data from within a function
-
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)
Viewing 1 replies (of 1 total)
- The topic ‘Outputting WP_query data from within a function’ is closed to new replies.