普通网友 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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀