0

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

1 回答 1

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');

让我知道事情的后续。

于 2012-08-13T22:39:00.030 回答