問題描述

WP 3.1 讓每個人都興奮,如果有些困惑,關於郵政格式。

啟用新的帖子格式

關於啟用郵政格式的問題已經詳細論述了。就像將這行新增到 functions.php 中一樣簡單:add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

顯示某種格式的帖子

但顯示這些職位的問題根本就沒有涵蓋。讓我們使用一個例子記錄顯示/查詢特定格式的帖子的過程:

假設我們想在側欄中新增一個 Twitter-like 狀態更新。很容易啟用 status 後期格式,但是如何實際查詢這些帖子,使其顯示在側欄中?

我做了相當多的搜尋,沒有找到這個問題的答案,歡迎所有人的貢獻。如果我們提出了一個很好的答案,我認為這將是第一個記錄這個問題。

謝謝!

最佳解決方案

您有很多使用”Post Formats” 功能進行顯示的選項:

例如在 index.php 迴圈中,您可以根據使用 has_post_format()函式的釋出格式來決定要顯示的內容:

        if ( has_post_format( 'aside' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'chat' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'gallery' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'image' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_post_thumbnail('medium');
            echo the_content();
        }

        elseif ( has_post_format( 'link' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'quote' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'status' )) {
            echo the_content();
        }

        elseif ( has_post_format( 'video' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        elseif ( has_post_format( 'audio' )) {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

        else {
            echo '<h3>';
            echo the_title();
            echo '</h3>';
            echo the_content();
        }

使用 get_template_part()get_post_format()獲取基於格式的錯誤迴圈,這是假設您為主題中使用的每個格式製作了一個格式 loop.php(說 format-status.php) 檔案,因此您只需呼叫它:

get_template_part( 'format', get_post_format() );

您還可以根據其格式查詢帖子:

$args = array(
                'tax_query' => array(
                    array(
                        'taxonomy' => 'post-format',
                        'field' => 'slug',
                        'terms' => array( 'post-format-quote' )
                    )
                )
            )
            $query = new WP_Query( $args );

和最後 (現在),您可以使用”post_class();” 功能基於 CSS 的風格

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

這將輸出如下:

<div id="post-id" class=」post format-status」>

希望這有助於開始

參考文獻

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