the_excerpt没有出现 - woocommerce / wordpress

时间:2017-05-03 14:47:20

标签: php wordpress woocommerce

HMTL原创呈现:

<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>

在setup_postdata之后的HTML($ 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>

奇怪的事情:

enter image description here

代码:

 <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>

当我更改代码的位置并删除标题和缩略图时,代码效果很好:

  <?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();
  ?>    

我在wordpress / woocommerce页面中调用了最近的帖子,但是当我打电话给摘录时,它没有出现,标题和缩略图都没问题。我整天都在努力,但我无法弄清楚发生了什么。当我改变&#34; echo $ my_excerpt&#34;用一个简单的词,它的工作原理。问题应该是我把内容放在一个数组中。我已经按照文档进行了操作,您可以看到: https://codex.wordpress.org/Function_Reference/get_the_excerpt

实例 get_the_excerpt()可用于在变量中检索和存储值,而无需将其输出到页面。

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

我的代码:

      <?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 个答案:

答案 0 :(得分:1)

修改:我已尝试使用此代码修改您的代码。

<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) 
{
    setup_postdata( $post ); // Edit: This will force wordpress to setup the data
    ?><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();
?>

试试这个。我已经为你的摘录提供了帖子ID。

<?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
?>

答案 1 :(得分:1)

嗯,经过大量的选择,测试,反馈和研究,我找到了一种方法。

谢谢大家!

我的最终代码:

<!-- 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>