【问题标题】:WordPress shortcode show some mistakeWordPress简码显示一些错误
【发布时间】:2016-12-25 07:23:00
【问题描述】:

这是显示类别帖子。但它有一些错误。请任何人修复它。

这是我的 WordPress 简码

   function my_form_shortcode($atts) {
   ob_start();
  $atts = shortcode_atts(
      array(
          'cat' => '1',
         'showposts' => '5', 
     ), $atts, 'my_form_shortcode' );

  //YOUR CODE START

 $recent = new WP_Query(); 
 $query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
 $recent->query( $query ); 
 $is_first_post = true; 
 while( $recent->have_posts() ) : $recent->the_post(); ?>

 <div class="panel-heading"><?php the_category(', '); ?></div>

<ul class="panel-grid">
 <li class="ex">
<?php 
 if ( $is_first_post  && has_post_thumbnail() ) {
  the_post_thumbnail( 'image', array( 'class' => 'img-responsive' )); 
  $is_first_post = false; 
  }
 ?>

 <a href="<?php the_permalink(); ?>">
 <?php the_title(); ?>
  </a>
 </li>
 </ul>
<?php endwhile; 
//YOUR CODE END

 return ob_get_clean(); 
}

add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );

在此代码类别标题名称显示在所有帖子之前。但我只想在第 1 个帖子之前显示类别标题。这段代码有什么问题。请帮帮我..

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    试试这个... https://developer.wordpress.org/reference/functions/get_the_category_by_id/

    function get_the_category_by_ID( $cat_ID ) {
        $cat_ID = (int) $cat_ID;
        $category = get_term( $cat_ID, 'category' );
     
        if ( is_wp_error( $category ) )
            return $category;
     
        return ( $category ) ? $category->name : '';
    }

    【讨论】:

      【解决方案2】:

      试试这个代码:

      <?php
      function my_form_shortcode($atts) {
          ob_start();
          $atts = shortcode_atts(
          array(
              'cat' => '1',
              'showposts' => '5', 
          ), $atts, 'my_form_shortcode' );
      
          //YOUR CODE START
      
          $recent = new WP_Query(); 
          $query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
          $recent->query( $query ); 
          $is_first_post = true; ?>
          <div class="panel-heading"><?php the_category(', '); ?></div>
          <?php while( $recent->have_posts() ) : $recent->the_post(); ?>
      
              <ul class="panel-grid">
                  <li class="ex">
                  <?php 
                      if ( $is_first_post  && has_post_thumbnail() ) {
                          the_post_thumbnail( 'image', array( 'class' => 'img-responsive' )); 
                          $is_first_post = false; 
                      }
                  ?>
      
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  </li>
              </ul>
          <?php endwhile; 
          //YOUR CODE END
      
          return ob_get_clean(); 
      }
      
      add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多