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 に答える