普通网友 2017-05-03 14:47
浏览 47
已采纳

the_excerpt没有出现 - woocommerce / wordpress

HMTL original rendered:

<div class="col-md-4">
     <div class="row">
     <a href="http://localhost/PHP/wordpress/3-blog-post/">3 - Blog Post</a> 
     </div>
     <div class="row">                   
     <img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />  


    </div>
    </div>

HTML after setup_postdata( $post );

    <div class="row">

<div class="col-md-4">
        <div class="row">
            <a href="http://localhost/PHP/wordpress/3-blog-post/">
                3 - Blog Post            </a> 
        </div>
        <div class="row">                   
            <img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />

Bem-vindo ao WordPress. Esse é o seu primeiro post. Edite-o ou exclua-o, e então comece a escrever!
            </div>
        </div>

Weird things:

enter image description here

The code:

 <div class="container">


    <div class="row">

<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) 
{
    ?><div class="col-md-4">
        <div class="row">
            <a href="<?php echo get_the_permalink($post['ID']); ?>">
                <?php echo $post['post_title']; ?>
            </a> 
        </div>
        <div class="row">                   
            <?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
            <?php
            $my_excerpt = get_the_excerpt($post['ID']);
            if ( '' != $my_excerpt ) {
              // Some string manipulation performed
            }
            echo $my_excerpt // Outputs the processed value to the page
            ?>

        </div>
    </div><?php 
}  
wp_reset_query();
?>

      </div>
</div>

</br>
</br>


<div class="container">

    <div class="row">

      <?php
        $args = array( 'numberposts' => '3' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach($recent_posts as $post) { ?>


            <div class="col-md-4">
              <div class="row">
                <a href="<?php echo get_the_permalink($post['ID']) ?>">
                  <?php echo $post['post_title'] ?>
                </a> 


              </div>
              <div class="row">                   

                <?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>

              </div>

              <div class="row">                   

                <?php
                $my_excerpt = get_the_excerpt($post['ID']);
                if ( '' != $my_excerpt ) {
                  // Some string manipulation performed
                }
                echo $my_excerpt // Outputs the processed value to the page
                ?>

              </div>
            </div>      


      <?php 
      }  
        wp_reset_query();
      ?>
      </div>

</div>

When I change the position of the code and remove title and thumbnail, the code works well:

  <?php
    $args = array( 'numberposts' => '3' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach($recent_posts as $post) { ?>


            <?php
            $my_excerpt = get_the_excerpt($post['ID']);
            if ( '' != $my_excerpt ) {
              // Some string manipulation performed
            }
            echo $my_excerpt // Outputs the processed value to the page
            ?>

          </div>
        </div>      


  <?php 
  }  
    wp_reset_query();
  ?>    

I call the recent posts in my wordpress / woocommerce page, but when I call the excerpt, it didn't appear, title and thumbnail are ok. I have worked on it all day long, but I couldn't figure out what happen. When I change "echo $my_excerpt" with a simple word, it works. The problem should be when I put the content in a array. I had followed the documentation as you can see: https://codex.wordpress.org/Function_Reference/get_the_excerpt

Examples get_the_excerpt() can be used to retrieve and store the value in a variable, without outputting it to the page.

<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>

My code:

      <?php
        $args = array( 'numberposts' => '3' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach($recent_posts as $post) { ?>


            <div class="col-md-4">
              <div class="row">
                <a href="<?php echo get_permalink($post['ID']) ?>">
                  <?php echo $post['post_title'] ?>
                </a> 


              </div>
              <div class="row">                   

                <?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>

                <?php
                $my_excerpt = get_the_excerpt();
                if ( '' != $my_excerpt ) {
                  // Some string manipulation performed
                }
                echo $my_excerpt // Outputs the processed value to the page
                ?>

              </div>
            </div>      


      <?php 
      }  
        wp_reset_query();
      ?>
      </div>

</div>
  • 写回答

2条回答 默认 最新

  • drcrc28428 2017-05-03 20:31
    关注

    Well, after a plenty of options, tests, feedbacks and research, I've found a way to make it.

    Thank you everyone!

    My final code:

    <!-- Testando novo formato -->
    
    <p class="display-4" style="text-align:center">Testando - Posts Recentes</p>
    
    
    <div class="container"> 
      <div class="row">
    
    
    <?php
        query_posts( array('posts_per_page'=>3) );
        while ( have_posts() ) : the_post();
    ?>
    
        <div class="col-md-4">
    
                  <div class="card" style="width: 20rem; margin-bottom:3rem; margin-top:3rem;">
    
                    <img class="card-img-top img-fluid" src="<?php the_post_thumbnail(); ?>">
    
                    <div class="card-block">
                        <h4 class="card-title"><?php the_title(); ?></h4>
                        <p class="card-text">
                          <?php
                            the_excerpt();
                          ?>
                        </p>
                        <a class="btn btn-primary" href="<?php the_permalink(' ') ?>">Ler o post</a>
                    </div>  
                  </div>
        </div>
    
          <?php 
            endwhile;
            wp_reset_query(); // resets main query
          ?>
    
    
      </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook