我不知道menu_order出了什么问题,但帖子没有按我想要的方式显示。以下是我的意思
我的博客里有3篇文章。以下是数据库看起来像的样子
id ..... menu_order
56 ..... 2
59 ..... 5
65 ..... 3
index.php(我的自定义主题)
我想只显示图像,所以这里是我使用的代码
<?php while ( have_posts() ) : the_post();
$images = get_children(
array( 'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'DESC',
'numberposts' => 999 )
);
if( $images )
{
$total_images = count( $images );
$image = array_shift( $images );
echo wp_get_attachment_image($image->ID, 'full', 0, array('id' => 'photo'));
}
endwhile; // end of the loop.
?>
问题是帖子是按id 65,59,56的顺序显示的,而不是我所期望的59,65,56
这是怎么回事?
使用以下代码。它将解决您的问题
'sort_column'=>'menu_order'
我会使用具有相同参数的get_posts()
。请参见此处。
这个menu_order
似乎不是你想的那样,例如你在网站自定义菜单上选择的顺序。相反,这是你在写每一页时设置的顺序。。。这也不是我想要的,所以我做了这个解决方案:
sort_column=menu_order只根据页面的书写顺序对页面进行排序,而不是根据您在视图>菜单(翻译)中设置的顺序,如果您想这样做的话:
$children = get_pages('child_of='. $topID);
// 'sort_column=menu_order' <-- only sorts by post order in writing mode (page > edit) not the menu order set in view > menus
// wp_nav_menu has what we need, let's sort it the same way.
$options = array(
'container' => '',
'echo' => false,
);
$nav = wp_nav_menu($options);
$nav = strip_tags($nav);
$nav = str_replace("'r", '', $nav);
$nav = explode("'n", $nav);
//print_r($nav);
$newChildren = array();
foreach ($nav as $item) {
$item = trim($item);
$run = true;
for ($c = 0; $c < count($children) && run; $c++) {
$child = $children[$c];
if (strcmp($child->post_title, $item) == 0 && !in_array($child, $newChildren)) {
$newChildren[] = $child;
$run = false;
}
}
// Adding the children the nav_menu is lacking, not sure why not all sub-children
// are added to the first child here..(works but don't know why :/)
if ($run == true) {
for ($c = 0; $c < count($children) && run; $c++) {
$child = $children[$c];
if (!in_array($child, $newChildren)) {
$newChildren[] = $child;
}
}
}
}
$children = $newChildren;
填充菜单时,添加sort_column=menu_order
:
<ul class="main-nav">
<?php wp_list_pages('&title_li=&depth=2&sort_column=menu_order'); ?>
</ul>