【问题标题】:Only show WordPress 'standard' post format on template?只在模板上显示 WordPress 的“标准”帖子格式?
【发布时间】:2011-09-20 14:23:11
【问题描述】:

我最近在我的 WordPress 主题中添加了帖子格式 - 在博客页面上它很好,因为它们都具有相应的样式。但是在我的主页模板上,我只想显示“标准”帖子格式(没有链接、画廊、音频、视频等)。

在我的主题选项中,我可以决定在首页上显示多少帖子,这就是“dft_recent_number”的用途。

有人知道我可以如何更改下面的代码以排除除“标准”帖子格式之外的所有帖子格式吗?

<?php 
$query = new WP_Query();
    $query->query('posts_per_page='.get_option('dft_recent_number'));

    //Get the total amount of posts
$post_count = $query->post_count;

    while ($query->have_posts()) : $query->the_post(); 

 ?>

非常感谢任何帮助!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    WP_Query 似乎没有post_format 的直接参数。

    根据我的快速研究,帖子格式似乎通过分类法相关联。因此,理论上,使用分类参数应该起作用。

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

    注意:您需要更新博客的分类名称和 slug。这应该是您在 functions.php 文件中设置的名称。

    【讨论】:

    • 这让我找到了正确的方向,杰森,谢谢!我使用“Not In”删除了我不想要的格式,例如 $args = array( 'showposts' =&gt; $number, 'tax_query' =&gt; array( array( 'taxonomy' =&gt; 'post_format', 'field' =&gt; 'slug', 'terms' =&gt; array('post-format-video','post-format-quote','post-format-link','post-format-audio'), 'operator' =&gt; 'NOT IN' ) ) ); query_posts( $args );
    【解决方案2】:
    // there's no post-format-standard so you should write it like this to exclude all other postpformats
    array(
    'taxonomy' => 'post_format',
    'field' => 'slug',
    'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
    'operator' => 'NOT IN'
    )
    

    【讨论】:

      【解决方案3】:

      我知道这很旧,但我也面临同样的问题,即使我找到了解决方案,我也想知道其他人做了什么来“解决”这个问题。

      我认为更具可扩展性的解决方案可能是这样的:

      $post_formats = get_theme_support( 'post-formats' );
      
      $tax_query = false;
      if ( $post_formats ) {
          $tax_query = array(
              array(
                  'taxonomy' => 'post_format',
                  'field'    => 'slug',
                  'terms'    => $post_formats[0],
                  'operator' => 'NOT IN'
              )
          );
      }
      
      // WP_Query arguments
      $args = array(
          'post_type' => 'post',
          'order'     => 'DESC',
          'orderby'   => 'date',
          'tax_query' => $tax_query
      );
      

      这将排除已启用的帖子格式,并且在 WP 将添加更多帖子格式(或添加添加更多的功能)的情况下也将起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        • 2012-06-16
        • 2012-02-08
        • 1970-01-01
        相关资源
        最近更新 更多