我想在 Wordpress 短代码中使用以下代码,以便可以像 [推荐书] 一样使用它,我已经尝试了很多次,但都失败了,非常感谢您的想法:
<div class="row cols3 testimonials">
<?php $postnum=1; query_posts('post_type=testimonial&showposts=3'); if (have_posts()) $post = $posts[0]; $c=0; while ( have_posts() ) : the_post();?>
<article class="col<?php if($postnum ==1){echo" first";}elseif($postnum==3){echo" last";}?>">
<p><?php echo get_the_content(); ?></p>
<div class="arrow"><div class="tri"></div></div>
<div class="name"><strong><?php global $post;$text = get_post_meta( $post->ID, '_cmb_testominal_author', true );echo $text;?></strong> - <?php global $post;$text = get_post_meta( $post->ID, '_cmb_testominal_company', true );echo $text;?></div>
</article>
<?php $postnum++; endwhile; wp_reset_postdata(); ?>
</div><!-- /Testimonials -->
*更新*
下面的代码有效,但我真的不知道如何,我不太有信心使用它,因为我不明白它是如何工作的。任何帮助都会很棒:
<?php
function testimonials_shortcode($atts, $content = null) {
$the_query = new WP_Query();
$the_query->query($atts);
if ($the_query->have_posts()) : while ($the_query->have_posts()) :
$the_query->the_post(); ob_start(); ?>
<div class="row cols3 testimonials">
<?php $postnum=1; query_posts('post_type=testimonial&showposts=3'); if (have_posts()) $post = $posts[0]; $c=0; while ( have_posts() ) : the_post();?>
<article class="col<?php if($postnum ==1){echo" first";}elseif($postnum==3){echo" last";}?>">
<p><?php echo get_the_content(); ?></p>
<div class="arrow"><div class="tri"></div></div>
<div class="name"><strong><?php global $post;$text = get_post_meta( $post->ID, '_cmb_testominal_author', true );echo $text;?></strong> - <?php global $post;$text = get_post_meta( $post->ID, '_cmb_testominal_company', true );echo $text;?></div>
</article>
<?php $postnum++; endwhile; wp_reset_postdata(); ?>
</div><!-- /Testimonials -->
<?php endwhile; endif; wp_reset_query();
$content = ob_get_contents(); ob_end_clean();
return $content;
}
add_shortcode('testimonials', 'testimonials_shortcode');
?>