Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach语句中的Wordpress排序帖子_Wordpress_Post_Foreach - Fatal编程技术网

foreach语句中的Wordpress排序帖子

foreach语句中的Wordpress排序帖子,wordpress,post,foreach,Wordpress,Post,Foreach,我有一个for-each语句,它根据ID查找分类法,并获取所有termchildren,并使用foreach循环列出它们。我想知道如何按标题的字母顺序排列。这是我的密码: $termID = 5; $taxonomyName = "apartmentlocation"; $termchildren = get_term_children( $termID, $taxonomyName ); foreach ($termchildren as $c

我有一个for-each语句,它根据ID查找分类法,并获取所有termchildren,并使用foreach循环列出它们。我想知道如何按标题的字母顺序排列。这是我的密码:

$termID = 5;
$taxonomyName = "apartmentlocation";                        
$termchildren = get_term_children( $termID, $taxonomyName );

foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName);
    echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    }
$termID=5;
$taxonomyName=“apartmentlocation”;
$termchildren=get_term_children($termID,$taxonomyName);
foreach($termchildrenas$child){
$term=get_term_by('id',$child,$taxonomyName);
回音“
  • ”; }
    请尝试
    获取术语()
    ,因为它接受更广泛的参数集

    //put your term and taxonomy variables here
    $termID = 5;
    $taxonomyName = "apartmentlocation"; 
    
    $args = array(
        'child_of' => $termID,
        'orderby' => 'name', //this is the default, it actually isn't needed
        'order' => 'ASC' //this is the default as well
    );
    $termchildren = get_terms($taxonomyName, $args);
    
    //do the rest of your loop
    foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName);
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    }
    
    //将术语和分类变量放在这里
    $termID=5;
    $taxonomyName=“apartmentlocation”;
    $args=数组(
    'child_of'=>$termID,
    'orderby'=>'name',//这是默认值,实际上不需要
    'order'=>'ASC'//这也是默认值
    );
    $termchildren=get_terms($taxonomyName,$args);
    //完成循环的其余部分
    foreach($termchildrenas$child){
    $term=get_term_by('id',$child,$taxonomyName);
    回音“
  • ”; }
    谢谢贤凯,但我还是有麻烦。你能给我一个更详细的例子吗?我在你的代码的其余部分添加了-这是现在完整的代码。但是,如果不知道这个新代码还有什么问题,我真的无法进一步帮助您。您的代码会产生相同的结果,列出项目,但不排序。如果我注释掉你的$args数组并获得_terms行,它似乎对结果没有影响。再次感谢您的回复!糟糕的是,我忘了将
    get_terms()
    的结果分配给
    $termchildren
    。你能再试一次吗?否则,您似乎需要通过PHP手动对其进行排序。运气不好,您的编辑似乎阻止了页面执行任何超出插入位置的代码。还有什么想法吗?我找到了一个解决方案,但由于我的技能有限,无法跟进并提出解决方案。