add_shortcode('random_quotes', 'my_random_quotes');
function my_random_quotes()
{
//The Query
query_posts('post_type=client-quotes&posts_per_page=1&orderby=rand');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<h3>'; echo the_title(); echo '</h3>';
echo '<p class="name"><a title="Testimonials from our clients" href="/testimonials-from-our-clients/">...read more</a></p>';
endwhile; else:
endif;
//Reset Query
wp_reset_query();
}
My shortcode added to the wordpress editor always fires to early. In this case it shows up above the "the_content" when I insert it in the content editor
Maor Barazany answers:
Try this:
add_shortcode('random_quotes', 'my_random_quotes');
function my_random_quotes()
{
//The Query
query_posts('post_type=client-quotes&posts_per_page=1&orderby=rand');
//The Loop
ob_start();
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<h3>'; echo the_title(); echo '</h3>';
echo '<p class="name"><a title="Testimonials from our clients" href="/testimonials-from-our-clients/">...read more</a></p>';
endwhile; else:
endif;
//Reset Query
$output = ob_get_clean();
wp_reset_query();
return $output;
}
Rick Bible comments:
this is the only one that worked right off the bat, Thanks,
John Cotton answers:
You need to return the value, not echo it!
In your loop, assign the content to a variable (eg $html) and then add return $html at the end of the function.
Keith Donegan answers:
<?php
add_shortcode('random_quotes', 'my_random_quotes');
function my_random_quotes()
{
$output = '';
//The Query
query_posts('post_type=client-quotes&posts_per_page=1&orderby=rand');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
$output .= '<h3>' . get_the_title() . '</h3>';
$output .= '<p class="name"><a title="Testimonials from our clients" href="/testimonials-from-our-clients/">...read more</a></p>';
endwhile; else:
endif;
//Reset Query
wp_reset_query();
return $ouput;
}
Rick Bible comments:
nothing shows, this is on my function file and I left out <?php
Dbranes answers:
As the experts above have said, the problem is because of the "echo".
Here is a simplified version of your shortcode:
add_shortcode('random_quotes', 'my_random_quotes');
function my_random_quotes(){
$s="";
$posts=get_posts(array( 'post_type'=>'client-quotes','numberposts' =>1,'orderby'=>'rand'));
foreach( $posts as $post ){
setup_postdata($post);
$s.="<h3>".get_the_title()."</h3>";
$s.='<p class="name"><a title="Testimonials from our clients" href="/testimonials-from-our-clients/">...read more</a></p>';
}
return $s;
}