首页 文章

the_excerpt没有出现 - woocommerce / wordpress

提问于
浏览
1

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>

当我更改代码的位置并删除 Headers 和缩略图时,代码运行良好:

<?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页面中调用了最近的帖子,但是当我打电话给摘录时,它并没有弄明白发生了什么 . 当我用一个简单的单词更改"echo $my_excerpt"时,它可以工作 . 问题应该是我把内容放在一个数组中 . 您已经按照文档查看了: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 回答

  • 1

    Edit: 我已经改变了你的代码尝试使用这个 .

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

    好吧,经过大量的选择,测试,反馈和研究,我找到了一种方法来实现它 .

    谢谢大家!

    我的最终代码:

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

相关问题