如何通过wordpress中的缩略图存在来显示帖子顺序?

时间:2015-09-08 11:31:47

标签: php wordpress post thumbnails

我想通过缩略图显示帖子顺序,即首先显示有缩略图的帖子,然后显示没有缩略图的帖子。

我在Stackoverflow here上发现了相同的情况,但这对我不起作用。目前我正在使用目录+主题,我想在其中实现它。

我尝试制作两个循环,一个循环将显示带缩略图的帖子,第二个将显示没有缩略图的帖子。但是这在分页的情况下不起作用,因为没有缩略图的帖子应该在最后一页上

这是代码

  {var $itemQuery = array(
    'post_type' => 'ait-item',
    'posts_per_page' => $filterCountsSelected,
            //'meta_key'=> '_thumbnail_id',
    'tax_query' => array(
        array(
            'taxonomy' => 'ait-items',
            'field' => 'term_id',
            'terms' => $currentCategory->term_id
        )
    ),
    'paged' => $pagination,
)}

 {customQuery as $query, $itemQuery}

 {* Loop for categories with featured Image*}
  {customLoop from $query as $post}
       {if $post->image}

       //Display post with thumbnail

       {/if}  //end of if

  {/customLoop} //end of loop

 {* Loop for categories without featured Image*}

   {customLoop from $query as $post}
     {if !$post->image}

       //Display post without thumbnail

     {/if}   //end of if

  {/customLoop} //end of loop

  {includePart parts/pagination, location => pagination-below, max => $query->max_num_pages} //includes pagination

编辑查询1:

{var $itemQuery = array( 
  'post_type' => 'ait-item', 
  'posts_per_page' => $filterCountsSelected, 
  'tax_query' => array(
      array(
         'taxonomy' => 'ait-items', 
         'field' => 'term_id',
         'terms' => $currentCategory->term_id 
      )
   ), 
  'paged' => $pagination, 
 )} 

 {var $itemQuery['orderby'] = 'meta_value_num'} 

 {var $itemQuery['meta_query'] = array(
   array(
     'relation' => 'OR', 
     array( 
        'key' => '_thumbnail_id',
        'compare' => 'NOT EXISTS'
      ),
      array( 
         'key' => '_thumbnail_id', 
         'compare' => '!NOT EXISTS' 
      ),
  )
)}

1 个答案:

答案 0 :(得分:4)

如果帖子有缩略图然后使用array_multisort

,我会通过foreach分配数字变量对其进行排序

试试这个,

function cmk_load_post_by_custom_order() {
    // Query Argument
    $args = array( 
        'post_type' => 'post', 
        'posts_per_page' => 6,
    );
    //Initization Wordpress Query
    $query = new WP_Query( $args ); 
    $posts = $query->get_posts();  

    $output = array();
    $order = array();

    //Ourput result as Custom Array
    foreach( $posts as $post ) { 
        $has_thumb = has_post_thumbnail( $post->ID ) ? 1 : 0 ; 
        $output[] = array( 
            'title' => $post->post_title,
            'slug' => $post->post_name,
            'thumb' => $has_thumb,//get_the_post_thumbnail( $post->ID,'full'), 
        );
        $order[] = $has_thumb;
    }
    array_multisort($order, SORT_DESC, $output);
    echo '<pre>',print_r($output,1),'</pre>';
}
add_action('wp_head','cmk_load_post_by_custom_order');

编辑1

如果你打算使用wordpress分页,上面的答案是行不通的,因为分页依赖于查询对象,

我已经完成了一些测试,我想出了在meta_value_num上使用orderby然后将_thumbnail_id设置为meta_key并添加关系OR的情况{{1是否为空,因为没有缩略图的帖子会有空的_thumbnail_id键。

尝试下面的查询参数

_thumbnail_id

我在本地测试了它,我得到了以下结果,它查询带有和没有缩略图的帖子,我在结果中包含缩略图ID,你可以看到帖子是根据缩略图值排序的

$args = array( 
        'post_type' => 'post', 
        'posts_per_page' => 99,
        'order' => 'DESC', // Display all post with thumbnail ID first 
        'orderby'   => 'meta_value_num', // order by meta key value
        'meta_query' => array(  //query post based on meta key
            array(
                'relation' => 'OR', // add condition if meta key is exists or not
                array(
                    'key' => '_thumbnail_id', 
                    'compare' => 'NOT EXISTS' // include post without _thumbnail_id key
                ),
                array(
                     'key' => '_thumbnail_id', 
                     'compare' => '!NOT EXISTS' // include post with _thumbnail_id key
                )
            )
        )

    );

编辑2

您还可以尝试在Array ( [0] => Array ( [title] => The Magnificent [slug] => the-magnificent [thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/happy-hour.png [tuhmb_id] => 17 ) [1] => Array ( [title] => The Awesome Company [slug] => the-awesome-company [thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/comedy.png [tuhmb_id] => 16 ) [2] => Array ( [title] => Selfieties [slug] => selfieties [thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/beverage-store.png [tuhmb_id] => 13 ) [3] => Array ( [title] => Drunkard [slug] => drunkard [thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/restaurant31.png [tuhmb_id] => 9 ) [4] => Array ( [title] => My First Company [slug] => my-first-company [thumb] => http://dynamized.dev/dyna/wp-content/uploads/2015/09/party.png [tuhmb_id] => 7 ) [5] => Array ( [title] => No Thumb Compnay [slug] => no-thumb-compnay [thumb] => [tuhmb_id] => ) ) 上使用数组值并使用orderby并使用回退到meta_value_num,其中显示所有带有缩略图ID的帖子,然后按日期排序,然后所有帖子都没有缩略图ID

将参数参数更改为

date