编程语言
首页 > 编程语言> > php-如何通过wordpress中的缩略图显示帖子顺序?

php-如何通过wordpress中的缩略图显示帖子顺序?

作者:互联网

我想按缩略图显示帖子顺序,即先显示具有缩略图的帖子,然后再显示不包含缩略图的帖子.

我在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' 
      ),
  )
)}

解决方法:

我将通过foreach分配数字变量进行排序,如果帖子有缩略图,则使用array_multisort

尝试这个,

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分页,则上述我的答案将不起作用,因为分页依赖于查询对象,

我已经进行了一些测试,并提出了在orderby上使用meta_value_num的想法,然后将_thumbnail_id设置为meta_key并添加关系,或者_thumbnail_id是否为空,因为没有缩略图的帖子将具有空的_thumbnail_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
                )
            )
        )

    );

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

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] => 
        )

)

编辑2

您还可以尝试在orderby上使用数组值,并使用具有回退日期的meta_value_num来显示所有具有缩略图ID的帖子,然后按日期排序,然后显示所有没有缩略图ID的帖子

将参数参数更改为此,

    'orderby'   => 'date, meta_value_num', // order by meta key value

标签:post,thumbnails,wordpress,php
来源: https://codeday.me/bug/20191027/1948227.html