仅某些帖子格式的GET_POST

时间:2012-04-13 作者:daba

我正在尝试创建一个只有我的“普通”文章格式的归档列表(不是链接、旁白、引用等格式)。

我将如何实施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技能充其量只是初级水平,因此任何帮助都将不胜感激。

4 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

实际上,您不能将与分类法相关的参数传递给get_posts().(Edit: actually, yes you can. The Codex is just somewhat unclear. Looking at source, get_posts() is, at its heart, just a wrapper for WP_Query().) 您可以传递元键/值和post类型,但不能传递诸如post格式之类的分类法。因此,对于这一行:

$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();

SO网友:shabushabu

Post格式只是一种称为post_format, 因此,您应该能够使用WP模板层次结构来创建后期格式归档。只需创建一个名为taxonomy-post_format-post-format-standard.php 该文件将用于输出所有标准帖子。您可以用任何其他格式名称替换“standard”,如aside, linkvideo, 因此,例如。taxonomy-post_format-post-format-video.php. 顺便说一句,这也适用于任何其他分类法,只要您坚持此格式:taxonomy-{TAXONOMY_NAME}-{TERM_NAME}.php

如果您想通过自定义循环显示帖子格式,例如在侧栏或页面模板中,则可以使用@kaiser的税务查询。只需将分类替换为post_format 还有鼻涕虫post-format-{FORMAT_NAME}.

SO网友:kaiser

对于两种不同的分类法。对于单个,您可以离开relation arg out。

$args = array(
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'movie_janner\',
            \'field\' => \'slug\',
            \'terms\' => array( \'action\', \'commedy\' ) // Single terms as string - multiple as array
        ),
        array(
            \'taxonomy\' => \'actor\',
            \'field\' => \'id\',
            \'terms\' => array( 103, 115, 206 ),
            \'operator\' => \'NOT IN\'
        )
    )
);

SO网友:theogrost

你可以这样做:

<?php 
while( have_posts() ) : the_post();
get_post_format()==false? get_template_part( \'loop\', \'posts\' ) : false;
endwhile;
?>
这是因为标准post格式的get\\u post\\u format()返回false。http://codex.wordpress.org/Function_Reference/get_post_format

结束

相关推荐

如何在Archive.php中显示用户的状态(在线-离线)

我正在使用代码用户状态How to check if a user (not current user) is logged in?. 在作者的个人资料页面上,效果很好(谢谢大家)。但如果我把它放在文章底部的存档页上,它就不起作用了functions.php add_action(\'wp\', \'update_online_users_status\'); function update_online_users_status(){ if(is_user_logge