简体   繁体   English

如何在此PHP代码中按变量排序?

[英]How can I order by a variable in this PHP code?

I'm trying to order this roster by Jersey Number but haven't been able to make it work. 我正在尝试按Jersey号码订购此花名册,但无法使其正常工作。 I'm not a programmer and have searched and tried to make this work multiple times. 我不是程序员,已经搜索并尝试多次进行这项工作。

Thank you so much for any help. 非常感谢您的帮助。

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

Here is the PHP code with the output: 这是带有输出的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; ?>

What you're trying to do seems simple, and with the right tools is very simple, but trying to do something like extract the Jersey Number and then order the posts by that is going to leave a mess of a WP_Query and is just a horrible idea. 您尝试做的事情看起来很简单,并且使用正确的工具也很简单,但是尝试执行诸如提取球衣号码然后按顺序排列帖子之类的事情,将使WP_Query变得一团糟,这简直太可怕了理念。

There's a few ways to 'cheat' in terms of post order. 有几种通过邮购方式“欺骗”的方法。

You could get a plugin that allows you to create a custom post order, but this breaks the ordering via WP_Query and 'locks' it, so I always avoid that when I can. 您可以获得一个允许您创建自定义订单的插件,但这会通过WP_Query中断排序并“锁定”它,因此我总是尽量避免这样做。

You could get a plugin called 'Advanced Custom Fields' and create a new field where you will write Jersey number of the post, then order the query by that field, which is probably the best way to do it. 您可以获取一个名为“高级自定义字段”的插件,并创建一个新字段,在其中写入帖子的泽西编号,然后按该字段排序查询,这可能是最好的方法。

I also like to create an unlimited repeater field that will allow me to choose a single post per repeated field and keep adding posts one by one in whatever order I want - this changes the way you structure the query and the template so it's a little bit more complex. 我还喜欢创建一个无限制的转发器字段,该字段使我可以为每个重复的字段选择一个帖子,并以我想要的顺序逐个添加帖子-这改变了查询和模板的结构方式,因此有点更复杂。

(also that chain of ifs at the beginning is horrible to look at. It's going to lead to pain in terms of management later on. I wish I could just write the template and test it :( ) (同样,一开始的if链看起来很可怕。稍后将在管理方面带来痛苦。我希望我可以编写模板并对其进行测试:()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM