如何通过此PHP代码中的变量进行排序?

时间:2017-04-18 20:07:41

标签: php wordpress

我正试图通过泽西数来订购这个名单但是却无法使其发挥作用。我不是程序员,已经搜索并试图多次完成这项工作。

非常感谢您的帮助。

http://onramp.website/rosters-2017/17u-navy/

这是带有输出的PHP代码:

<?php get_header(); ?>

<?php wp_reset_query(); ?>


<?php
$post_name = $post->post_name;

if ($post_name == "17u-navy") {
$post_type = "17u_navy";
}

elseif ($post_name == "15u-navy") {
$post_type = "15u_navy";
}

elseif ($post_name == "17u-red") {
$post_type = "17u_red";
}

elseif ($post_name == "17u-white") {
$post_type = "17u_white";
}

elseif ($post_name == "17u-gold") {
$post_type = "17u_gold";
}

$args = array('post_type'=>$post_type,'posts_per_page'=>-1, 'orderby' => 'number', 'order' => 'ASC',);
$the_query = new WP_Query( $args );
$flag = 0;

if ($the_query->have_posts()):
?>




<?php while ($the_query->have_posts()): $the_query->the_post(); ?>

<?php
$flag++;
?>

<?php if ($flag == 999): ?>

<?php $flag = 0; ?>





<?php endif; ?> 



<h3 class="h-custom-headline cs-ta-left h4 accent"><span><?php the_title(); ?> &nbsp; #<?php the_field('number'); ?></span></h3>

<div id="x-section-2" class="x-section" style="margin: 0px 0px 25px 0px;padding: 0px; background-color: transparent; margin-bottom: 50px;">
<div class="x-column x-sm x-1-5" style="padding: 0px;">
<img class="x-img x-img-none" src="<?php the_field('profilephoto'); ?>">
</div>
<div class="x-column x-sm x-4-5" style="padding: 0px;">
<ul class="x-block-grid two-up">
<li class="x-block-grid-item"><strong>Class of <?php the_field('gradyear'); ?></strong><br />
<strong>Height:</strong> <?php the_field('height'); ?><br />
<strong>Position:</strong> <?php the_field('position'); ?><br />
<?php if( get_field('bballhonors') ): ?>
<strong>Basketball Honors:</strong> <?php the_field('bballhonors'); ?><br />
<?php endif; ?>
<?php if( get_field('ncaaclearing') ): ?>
<strong>NCAA Clearing House:</strong> <?php the_field('ncaaclearing'); ?><br />
<?php endif; ?>
<?php if( get_field('highlightfilm') ): ?>
<strong>Highlight Film:</strong> <a href="<?php the_field('highlightfilm'); ?>" target="_blank">link</a>
<?php endif; ?>
<?php if( get_field('hobbies') ): ?>
<strong>Hobbies:</strong> <?php the_field('hobbies'); ?>
<?php endif; ?>
</li>
<li class="x-block-grid-item">
<strong>High School:</strong> <?php the_field('highschool'); ?><br />
<strong>Hometown:</strong> <?php the_field('citystate'); ?><br />
<?php if( get_field('gpa') ): ?>
<strong>GPA:</strong> <?php the_field('gpa'); ?><br />
<?php endif; ?>
<?php if( get_field('sat') ): ?>
<strong>SAT:</strong> <?php the_field('sat'); ?><br />
<?php endif; ?>
<?php if( get_field('schoolhonors') ): ?>
<strong>School Honors:</strong> <?php the_field('schoolhonors'); ?><br />
<?php endif; ?>
<?php if( get_field('favoritequote') ): ?>
<strong>Favorite Quote:</strong><em><br />"<?php the_field('favoritequote'); ?>"</em><br />
<?php if( get_field('author') ): ?>
 ~&nbsp;<?php the_field('author'); ?>
<?php endif; ?><?php endif; ?></li>
</ul>
</div>
</div>





<?php endwhile; ?>

<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

你想要做的事情看起来很简单,并且使用正确的工具非常简单,但是尝试做一些事情,比如提取泽西数字,然后按顺序排序帖子就会留下一堆WP_Query这只是一个可怕的想法。

有几种方法可以欺骗&#39;在邮购方面。

您可以获得一个允许您创建自定义发布订单的插件,但这会打破通过WP_Query和&#39;锁定&#39;它,所以当我可以时,我总是避免这样做。

您可以获得一个名为“高级自定义字段”的插件。并创建一个新字段,您将在其中编写帖子的泽西号,然后按该字段对查询进行排序,这可能是最好的方法。

我还想创建一个无限的转发器字段,允许我为每个重复的字段选择一个帖子,并按照我想要的顺序逐个添加帖子 - 这会改变您构建查询和模板的方式,以便它和#39;有点复杂。

(也就是那个开头的ifs链看起来很可怕。以后它会导致管理方面的痛苦。我希望我能写出模板并测试它:()< / p>