我有一个选择框,允许您在父页面和他们的孩子之间导航,他们被给予一个特定的顺序,以便在网站上以某种方式显示它们。
我相信您感兴趣的代码行可能就是这一行
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
完整代码如下。您可以看到网站上页面的顺序与选择菜单中页面的顺序不同,这是因为wordpress按照order
排序,我的页面按字母顺序排序。如何按顺序对它们进行排序,以便您在网站上看到的内容与选择框相匹配?
<?php
// determine parent of current page
if ($post->post_parent)
{
$ancestors = get_post_ancestors($post->ID);
$parent = $ancestors[count($ancestors) - 1];
}
else
{
$parent = $post->ID;
}
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
?>
<div id="info-select-wrap" class="single-france-select">
<select id="info-select"class="fr-select" >
<?php
if (get_the_ID() == $parent->ID): ?>
<option value="<?php
echo get_the_permalink($parent->ID); ?>" selected>  <?php
echo get_the_title($parent->ID); ?></option>
<?php
else: ?>
<option value="<?php
echo get_the_permalink($parent); ?>">  <?php
echo get_the_title($parent); ?></option>
<?php
endif;
foreach($children as $child):
if (has_children($child))
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
else
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
endforeach; ?>
</select>
</div>
我试过了
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&orderby='menu_order'&order='ASC'");
没有解决它。
通过添加sort_column=menu_order
解决了问题
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&sort_column=menu_order");
只是一个简单的说明,&echo=0
是没用的,你可以删除它。