我不知道menu_order有什么问题,但帖子没有按我的意愿显示。这就是我的意思
我的博客中有3个帖子。这是db的样子
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
这有什么问题?
答案 0 :(得分:2)
使用以下代码。它将解决您的问题
'sort_column'=&gt; 'menu_order'
答案 1 :(得分:0)
我会使用相同参数的get_posts()
。请参阅here。
答案 2 :(得分:0)
似乎这样menu_order
不是你想的,例如。您在网站自定义菜单上选择的顺序。相反,它是你在每一页写下的顺序......这不是我想要的,所以我做了这个解决方案:
sort_column = menu_order仅根据书写顺序对页面进行排序,而不是您在视图中设置的顺序&gt;菜单(翻译),如果你想要你可以这样做:
$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;
答案 3 :(得分:0)
填充菜单时,添加sort_column=menu_order
:
<ul class="main-nav">
<?php wp_list_pages('&title_li=&depth=2&sort_column=menu_order'); ?>
</ul>