簡體   English   中英

使用get_adjacent_post wordpress獲得get_the_date

[英]get_the_date with using get_adjacent_post wordpress

我想在get_adjacent_post中顯示發布日期。 但是get_the_date($ prevpost-> ID)顯示post_id。

它可以顯示縮略圖和標題。

<?php
   $prevpost = get_adjacent_post(true, '', true); 
   $nextpost = get_adjacent_post(true, '', false);
   if( $prevpost or $nextpost ){ 
   ?>
<div class="cat_paging">
   <div class="row">
      <?php
         if ( $prevpost ) {
             echo '<div class="col-sm-6">
                 <div>Before</div>
                 <a href="' . get_permalink($prevpost->ID) . '">' . 
                 get_the_post_thumbnail($prevpost->ID, 'thumbnail') . '</a><p>' . 
                 get_the_title($prevpost->ID) . '</p><p>' . get_the_date($prevpost->ID) . '</p>
                 </div>';
         } else {
            echo '<div class="col-sm-6"><a href="' . network_site_url('/') . '">TOP</a>
         </div>';
         }
         if ( $nextpost ) {
             echo '<div class="col-sm-6">
                 <div>Next</div>
                 <a href="' . get_permalink($nextpost->ID) . '">' . 
                 get_the_post_thumbnail($nextpost->ID, 'thumbnail') . '</a><p>' . 
                 get_the_title($nextpost->ID) . '</p><p>' . get_the_date($nextpost->ID) . '</p>
                 </div>';
         } else {
            echo '<div class="col-sm-6"><a href="' . network_site_url('/') . '">TOP</a>
         </div>';
         }
         ?>
   </div>
</div>
<?php } ?>

有人知道任何主意,請教我。

使用get_post_meta()代替get_the_date($ nextpost-> ID)。從此鏈接閱讀此函數的官方文檔

get_the_date()接受兩個參數,一個格式字符串和一個get_the_date()

您將傳遞帖子ID作為第一個參數,而不是第二個。

正確用法:

get_the_date( '', $prevpost )

需要注意的另一點是,我傳遞的是post對象,而不是ID。 您可以傳入一個ID,但是該函數無論如何都必須檢索post對象。

文檔: https : //codex.wordpress.org/Function_Reference/get_the_date

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM