2017-04-18 44 views
0

我試圖通過澤西島號碼訂購這個名單,但一直未能使其工作。我不是程序員,並且已經搜索並嘗試多次完成這項工作。如何通過此PHP代碼中的變量進行排序?

非常感謝你的任何幫助。

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

回答

0

什麼你試圖做看似簡單,並且使用正確的工具很簡單,但在嘗試做這樣的事情提取球衣號碼,然後通過命令發佈帖子,這會讓WP_Query陷入混亂,這只是一個可怕的想法。

有一個在後順序方面「欺騙」的一些方法。

你可以得到一個插件,它允許你創建一個自定義的職務序列,但此通過WP_Query和「鎖定」它打破了排序,所以我總是避免時,我可以。

你可以得到一個名爲「高級自定義字段」插件,並創建一個新的領域,你會寫帖子的球衣號碼,然後命令由該領域,這可能是做的最好的方式查詢。

我也喜歡創建一個無限的中繼器字段,允許我選擇每個重複字段的單個帖子,並以我想要的任何順序逐個添加帖子 - 這改變了您構建查詢和模板的方式這有點複雜。

(也之初IFS的該鏈是慘不忍睹。這將導致疼痛在管理方面以後。我希望我可以寫模板,並測試它:()

+0

謝謝!里斯 的$ args =陣列( 'post_type'=> $ post_type, 'posts_per_page'=> - 1, 'meta_key'=> '編號', '的OrderBy'=> 'LEN(編號),meta_value',「順序'=> 'ASC'); $ the_query =新WP_Query($參數); $國旗= 0; 其實,我現在用先進的自定義字段,只是發現一些工作,以及幾乎 我用一個博福和花名冊根據編號重新排序。 但是,不完全正確。如果你看,他們都上升,但#5是最後。有沒有辦法按字面順序排列號碼? @里斯-G –

+0

我想我正在尋找的是使用「標準」之類的,而不是「自然而然」的一種方式。 –

+0

在編程語言中有一個字符串,其可包括數之間的差 - 一段文字,如「hello2」,和一個整數,它是一個純數字,例如5(注缺乏'「)。你的'數字'字段實際上被解釋爲以#開頭的字符串。因此,排序算法檢查發現它是#開頭的全部,然後取第二個「字母」,這是數字,然後按它排序。這是1 1 1 2 2 2 3 3 4 5,你可以通過17U海軍頁面上的球衣號碼看到。 –

相關問題