2017-05-03 46 views
1

HMTL原:後setup_postdata($ POST)the_excerpt沒有出現 - woocomerce/WordPress的渲染

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

<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頁面,但是當我打電話的摘錄,它並沒有出現,標題和縮略圖都OK。我整天都在努力,但我無法弄清楚會發生什麼。當我用一個簡單的字改變「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> 

謝謝大家!

+0

你不提供文章ID來獲得摘錄喜歡你與你的其他功能做的事情。既然你沒有使用wordpress的正式循環機制,你需要手動給出id。看到我的答案。 – shazyriver

+0

在foreach開始後寫這個setup_postdata($ post);它會工作,只是檢查它。 – shazyriver

+0

截圖相當無用。 CSS或其他古怪可能會隱藏你的輸出。相反,請執行「查看源代碼」並向我們顯示呈現的HTML。 –

回答

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

所以最後你使用了正式的循環機制。 +1 – shazyriver

+0

TKS男人!沒有你,我無法做到! –

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

Tks @shazyriver,但沒有工作:( –

+0

我改變了代碼的位置並刪除了標題和縮略圖,這次,摘錄了紅外。 –

+0

在我的代碼中正常工作。我已經檢查過它。 – shazyriver