問題描述

我正在嘗試創建一個只有我的”normal” post 格式的文件 (不鏈接,旁邊,報價等格式) 的歸檔列表。

如何在下面的代碼中實現 has_post_format( 'standard' )或類似的東西?

我無法找到僅請求特定格式類型的 get_posts 查詢。

<?php    
    // Get the posts
    $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');     
?>

<?php foreach($myposts as $post) : ?>   

<?php    
    // Setup the post variables
    setup_postdata($post);

    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);
    $day = mysql2date('j', $post->post_date);    
?>

<p>
    <span class="the_article">
        <a href="http://<?php%20the_permalink();%20?>"><?php the_title(); ?></a>
    </span>
    &nbsp;&nbsp;&nbsp;
    <span class="the_day">
        <?php the_time('j F Y'); ?>
    </span>
</p>

<?php endforeach; ?>

我的 php 技能最多在初級階段,所以任何幫助將不勝感激。

最佳解決方案

您不能將 taxonomy-related 參數實際傳遞給 get_posts()。 (編輯:實際上,是的,可以的,Codex 只是有點不清楚,從源頭上看,get_posts()的核心僅僅是 WP_Query()的封裝。) 你可以傳遞元鍵/值和後期類型,但不能分類作為郵政格式。所以對於這一行:

$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');

我建議使用 WP_Query()而不是 get_posts()

$myposts = new WP_Query( array(
    'tax_query' => array(
        array(                
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 
                'post-format-aside',
                'post-format-audio',
                'post-format-chat',
                'post-format-gallery',
                'post-format-image',
                'post-format-link',
                'post-format-quote',
                'post-format-status',
                'post-format-video'
            ),
            'operator' => 'NOT IN'
        )
    )
) );

注意:是的,這是很多嵌套數組。税務查詢可能很棘手。

下一步是修改你的循環打開/關閉語句。更改這些:

<?php foreach($myposts as $post) : ?>

    <?php /* loop markup goes here */ ?>

<?php endforeach; ?>

… 到這個:

<?php if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

    <?php /* loop markup goes here */ ?>

<?php endwhile; endif; ?>

<?php wp_reset_postdata(); ?>

您的實際循環標記應該能夠保持不變,除了不再需要調用 setup_postdata( $post )

<?php        
    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);
    $day = mysql2date('j', $post->post_date);    
?>

<p>
    <span class="the_article">
        <a href="http://<?php%20the_permalink();%20?>"><?php the_title(); ?></a>
    </span>
    &nbsp;&nbsp;&nbsp;
    <span class="the_day">
        <?php the_time('j F Y'); ?>
    </span>
</p>

所以,把它放在一起:

<?php
// Only query posts with the
// "standard" post format, which
// requires *excluding* all other
// post formats, since neither the
// "post_format" taxonomy nor the
// "post-format-standard" taxonomy term
// is applied to posts without
// defined post formats
$myposts = new WP_Query( array(
    'tax_query' => array(
        array(                
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 
                'post-format-aside',
                'post-format-audio',
                'post-format-chat',
                'post-format-gallery',
                'post-format-image',
                'post-format-link',
                'post-format-quote',
                'post-format-status',
                'post-format-video'
            ),
            'operator' => 'NOT IN'
        )
    )
) );

// Open the loop
if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);
    $day = mysql2date('j', $post->post_date);    
    ?>

    <p>
        <span class="the_article">
            <a href="http://<?php%20the_permalink();%20?>"><?php the_title(); ?></a>
        </span>
        &nbsp;&nbsp;&nbsp;
        <span class="the_day">
            <?php the_time('j F Y'); ?>
        </span>
    </p>  
    <?php 

// Close the loop
endwhile; endif;

// Reset $post data to default query
wp_reset_postdata();

參考文獻

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