Reputation: 1175
I have a custom post type in WordPress and I'm using a loop in a function so that I can call the listings using a shortcode.
I am adding to a string using .=
as I have read around that this is the best way to do it.
The problem is that it isn't formatting the code in the correct format, please see code below.
$args = array( 'post_type' => 'gms_product', 'posts_per_page' => 30 );
$loop = new WP_Query( $args );
function get_the_products() {
global $loop;
$code = '';
while ( $loop->have_posts() ) : $loop->the_post();
$code .= '<a href="#"><h3>'.the_title().'</h3></a>';
endwhile;
return $code;
}
The output looks like this.
post1post2 <a href="#"><h3></h3></a><a href="#"><h3></h3></a>
Upvotes: 0
Views: 68
Reputation: 3348
Try with get_the_title()
$code .= '<a href="#"><h3>'.get_the_title().'</h3></a>';
Upvotes: 3