如何将此代码用作短代码

时间:2012-08-13 22:02:11

标签: wordpress shortcode

我想在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');
?>

1 个答案:

答案 0 :(得分:1)

这是你要找的东西(减去后期统计)。将其粘贴到您functions.php并在帖子编辑器中用作[testimonials],或直接在<?php echo do_shortcode("[testimonials]");

模板中使用
function testimonials_function(){ 

  ob_start();
  ?>
  <div class="row cols3 testimonials">
  <?php 

  $custom_query = new WP_Query( 'post_type=testimonial&posts_per_page=3' );

  if($custom_query->have_posts()) :

     while ( $custom_query->have_posts() ) : $custom_query->the_post();
     ?>
            <article>
              <p><?php the_content(); ?></p>
              <div class="arrow"><div class="tri"></div></div>
              <div class="name"><strong><?php echo get_post_meta( get_the_id(),'_cmb_testominal_author', true ); ?></strong> - <?php echo get_post_meta( get_the_id(),'_cmb_testominal_company', true ); ?></div>
           </article>   
    <?php       
    endwhile;

  endif;

  // Reset Post Data
  wp_reset_postdata();

  $content =  ob_get_contents();
  ob_clean();

  return $content;

}

add_shortcode('testimonials','testimonials_function');

让我知道它是怎么回事。