问题描述

我正在尝试创建一个只有我的”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="<?php the_permalink(); ?>"><?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="<?php the_permalink(); ?>"><?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="<?php the_permalink(); ?>"><?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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。