我需要使用这个循环对帖子进行排序,这是一个名为“pc”的CTP的归档页面(pc archive.php)。
尝试使用<?php while ( have_posts(\'order=desc\') ) : the_post(); ?>
但运气不好。。。我一定做了什么坏事。
然后我尝试添加这个过滤器,但我没有让它工作。。。
function my_custom_archive_order( $vars ) {
  if ( !is_admin() && isset($vars[\'post_type\']) && is_post_type_hierarchical($vars[\'post_type\']) ) {
    $vars[\'orderby\'] = \'menu_order\';
    $vars[\'order\'] = \'DESC\';
  }
  return $vars;
}
add_filter( \'request\', \'my_custom_archive_order\');
 这是循环:
             <?php while ( have_posts() ) : the_post(); ?>
      <div class="galleryphotos">
    <a target="_blank" href="<?php echo types_render_field("pcurl", array("raw" => "true")); ?>">
              <img  class="pcimagestyle" src="<?php echo types_render_field("pcimage", array("raw" => "true")); ?>">
</a>       
    <div class="icons">            <a target="_blank" href="<?php echo types_render_field("pcurl", array("raw" => "true")); ?>"
                      <ul class="pcitems"> 
        <li><?php the_title(); ?> </li>
              <li>   <?php echo do_shortcode(\'[types field="pcpubname"][/types]\'); ?>  </li>     
              <li>   <?php echo do_shortcode(\'[types field="pcdate" format="d/m/Y"][/types]\'); ?></li>
                </ul>
              </a>
</div>
               </div>       <?php endwhile; // end of the loop. ?>
 
                SO网友:mathieuhays
                必须在中设置此筛选器functions.php.您的筛选器无法按预期工作,因为您的条件错误。这里的条件意味着您尝试对页面(分层帖子)进行排序,而不是像条目这样的常规帖子。
你必须小心使用这个过滤器,因为它对你的整个网站都有影响。
function my_custom_archive_order( $vars ) {
  if ( !is_admin() && isset($vars[\'post_type\']) && $vars[\'post_type\'] == \'pc\' ) {
    $vars[\'orderby\'] = \'menu_order\';
    $vars[\'order\'] = \'DESC\'; // you probably don\'t need this because that\'s the default behaviour
  }
  return $vars;
}
add_filter( \'request\', \'my_custom_archive_order\');
https://codex.wordpress.org/Plugin_API/Filter_Reference/request