如何通过动态 WORDPRESS 设置 post 顺序

how to set post order by dynamically WORDPRESS

Image 1

大家好

我创建了自定义 post 类型和各种自定义字段。我想控制每个 post 的顺序,所以我创建了一个名为“Order”的自定义字段。目前,我只是在此字段中手动输入一个数字,作为我希望 post 在列表中的位置。这样做的问题是,如果我说要将 post 8 移动到位置 3,我需要进入每个 post 并相应地更改顺序。

这里的例子: https://prnt.sc/vua6dm

我需要单独更改 post 的顺序,这也会更改前端。

首先您需要创建一个名为 order 的元数据框,然后是以下代码:

<?php
$price_query_args = array(
  'meta_key' => 'order',
  'meta_value' => "",
  'meta_compare' => '!=',
  'post_type' => array('post'),
);
<?php if (have_posts()):?>
  <ul style="display:flex;">
 <?php  while (have_posts()):the_post();?>
        <li style="order:<?php echo get_post_meta(get_the_ID(), 'order', true); ?>;" >
       <img src="<?php $image=wp_get_attachment_image_src(get_post_thumbnail_id( $post -> ID ),'single-post-thumbnail' ); echo $image[0]; ?>" style="width:100%">
        <label class="order"><?php echo get_the_title(); ?></label>
          <div class="btn-pro" >
          <a href="<?php the_permalink(); ?>/#tab1">bulk</a>
          <a href="<?php the_permalink(); ?>/#tab2">pack</a>
          </div>
        </li>
  <?php endwhile; ?>
  </ul>
  <?php endif; ?>