簡體   English   中英

如何在此PHP代碼中按變量排序?

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

我正在嘗試按Jersey號碼訂購此花名冊,但無法使其正常工作。 我不是程序員,已經搜索並嘗試多次進行這項工作。

非常感謝您的幫助。

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

您嘗試做的事情看起來很簡單,並且使用正確的工具也很簡單,但是嘗試執行諸如提取球衣號碼然后按順序排列帖子之類的事情,將使WP_Query變得一團糟,這簡直太可怕了理念。

有幾種通過郵購方式“欺騙”的方法。

您可以獲得一個允許您創建自定義訂單的插件,但這會通過WP_Query中斷排序並“鎖定”它,因此我總是盡量避免這樣做。

您可以獲取一個名為“高級自定義字段”的插件,並創建一個新字段,在其中寫入帖子的澤西編號,然后按該字段排序查詢,這可能是最好的方法。

我還喜歡創建一個無限制的轉發器字段,該字段使我可以為每個重復的字段選擇一個帖子,並以我想要的順序逐個添加帖子-這改變了查詢和模板的結構方式,因此有點更復雜。

(同樣,一開始的if鏈看起來很可怕。稍后將在管理方面帶來痛苦。我希望我可以編寫模板並對其進行測試:()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM