問題描述

我使用 wordpress 3.2,我做了一個這樣的查詢帖子:

<?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>

然後我試著回答這個帖子的日期,我這樣問。

<?php echo the_date(); ?>

它給了我的職位的標題和摘錄和永久連結,但沒有日期。你認為問題是什麼?我相信這是一個很尷尬的事情。

以下是影片頁面的模板檔案中的程式碼:

    <?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>
    <h2>Recent Video</h2>
    <h3 class="date"><?php echo the_date(); ?></h3>
    <p><strong><?php echo the_title(); ?></strong><?php echo the_excerpt(); ?></p>
    <p><a href="<?php%20echo%20the_permalink();%20?>" class="more2">Watch Now</a></p>

這裡我嘗試把查詢放在一個迴圈中:

<?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2>Recent Video</h2>
<h3 class="date"><?php echo the_date(); ?></h3>
<p><strong><?php echo the_title(); ?></strong><?php echo the_excerpt(); ?></p>
<p><a href="<?php%20echo%20the_permalink();%20?>" class="more2">Watch Now</a></p>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

the_date() 沒有工作,但是 the_title() 等功能起作用。順便說一下,我的查詢更改為 the_post(),這不是我想要的。我想查詢最新的影片,就像我在迴圈之上。

順便說一下,我在頁面中使用了 the_date 功能,它的工作。這可能是問題嗎?這是在程式碼之前,我有一個問題。

<div id="col75" class="firstcol">
    <iframe id="video" src="http://www.youtube.com/embed/videoseries?list=<?php%20print%20get_post_meta($post->ID,"playlist_id", true); ?>" width='560' height='350' frameborder="0"></iframe>
    <div id="col25">
        <h2><?php echo get_post_meta($post->ID,"speaker", true); ?></h2>
        <h3 class="date"><?php echo the_date(); ?></h3>

最佳解決方案

看這個 special note about using the `the_date’

SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string. Use to add the date set in the admin interface.

  1. 您使用 query_posts 擰緊全域性變數

  2. 您回應已經列印到瀏覽器的功能

    • 你實際上是為所有的模板標籤。

    • echo the_date(); 更改為:echo get_the_date('F j, Y');

    • 從已經列印到瀏覽器的模板標籤中刪除回聲,或者使用返回值的備用函式。

  3. 使用新的 WP_Queryget_posts 代替 query_posts

  4. 閱讀法典。它告訴你如何使用所有這些功能,是非常有益的:)

次佳解決方案

the_date()僅在以前未列印同一日期的情況下列印日期。不,這與其他類似的功能不一致。但是這是它在 WordPress 的祖先 b2 /cafelog 中的工作原理,向後相容性總是勝過邏輯… 🙂

要列印日期總是使用 get_the_date()

<?php echo get_the_date(); ?>

要麼

<?php echo mysql2date( get_option( 'date_format' ), $post->post_date); ?>

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。