2

我不知道 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

这有什么问题?

4

4 回答 4

3

使用以下代码。它会解决你的问题

'sort_column' => 'menu_order'

于 2013-06-18T03:33:37.843 回答
0

似乎这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;
于 2013-05-22T14:12:54.583 回答
0

我会改用get_posts()相同的参数。见这里

于 2012-08-28T12:57:34.447 回答
0

当您填充菜单时,添加sort_column=menu_order

 <ul class="main-nav">

    <?php wp_list_pages('&title_li=&depth=2&sort_column=menu_order'); ?>

 </ul>
于 2020-08-25T15:24:40.347 回答