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

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 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题