如何按此 PHP 代码中的变量排序? [英] How can I order by a variable in this PHP code?

查看:31
本文介绍了如何按此 PHP 代码中的变量排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按 Jersey Number 订购此花名册,但一直无法使用.我不是程序员,已经多次搜索并尝试使这项工作成功.

非常感谢您的帮助.

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

这是带有输出的 PHP 代码:

<?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 );$标志= 0;如果 ($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 口音"><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 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><?php the_field('gradyear');的类?></strong><br/><strong>高度:</strong><?php the_field('height');?><br/><strong>职位:</strong><?php the_field('位置');?><br/><?php if(get_field('bballhonors')): ?><strong>篮球荣誉:</strong><?php the_field('bballhonors');?><br/><?php endif;?><?php if(get_field('ncaaclearing')): ?><strong>NCAA 清算所:</strong><?php the_field('ncaaclearing');?><br/><?php endif;?><?php if(get_field('highlightfilm')): ?><strong>亮点影片:</strong><a href="<?php the_field('highlightfilm'); ?>"target="_blank">link</a><?php endif;?><?php if(get_field('hobbies')): ?><strong>爱好:</strong><?php the_field('爱好');?><?php endif;?><li class="x-block-grid-item"><strong>高中:</strong><?php the_field('highschool');?><br/><strong>家乡:</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>学校荣誉:</strong><?php the_field('schoolhonors');?><br/><?php endif;?><?php if( get_field('favoritequote') ): ?><strong>最喜欢的引用:</strong><em><br/>"<?php the_field('favoritequote');?>"</em><br/><?php if(get_field('author')): ?>~&nbsp;<?php the_field('author');?><?php endif;?><?php endif;?></li>

<?php endwhile;?><?php endif;?>

您尝试做的事情看起来很简单,使用正确的工具非常简单,但是尝试做一些事情,例如提取 Jersey Number 然后订购那个帖子会留下一团糟的WP_Query,这只是一个可怕的想法.

就后序而言,有几种方法可以作弊".

您可以获得一个插件,让您可以创建自定义发布订单,但这会破坏通过 WP_Query 的排序并锁定"它,因此我总是尽可能避免这种情况.

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

我还喜欢创建一个无限制的转发器字段,它允许我为每个重复字段选择一个帖子,并按照我想要的任何顺序一个接一个地添加帖子 - 这会改变您构建查询和模板的方式,因此它是稍微复杂一点.

(而且一开始的 ifs 链看起来很可怕.它会导致以后管理方面的痛苦.我希望我可以编写模板并对其进行测试:( )

I'm trying to order this roster by Jersey Number but haven't been able to make it work. 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/

Here is the PHP code with the output:

<?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.

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.

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 :( )

这篇关于如何按此 PHP 代码中的变量排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆