duanlan2003 2015-06-16 20:25
浏览 105
已采纳

Wordpress - wp_insert_post()和nav_menu_items

I'm using wp_insert_post() to add a new page in WordPress. This inserts the post into wp_posts which in turn makes the post show up in the Dashboard.

From there, I have to manually click and drag under the Pages tab to make that post a menu item in my nav bar,

How can I do this from the PHP side? I've scanned the Codex and can't find what I'm looking for. There doesn't seem to be a WordPress function for that sort of thing.

  • 写回答

1条回答 默认 最新

  • doujun1495 2015-06-17 22:44
    关注

    Problem solved.

    While WordPress provides no specific function for adding items and sub-items to the nav bar, there's a workaround. The project involved an author being able to add new books to her database, and have them automatically show up in the nav bar as a sub-item under "Books" without having to go through the Dashboard to do it.

    I went to header.php in the theme dir and found the wp_nav_menu() function. After reading this article, I was able to modify the output, using str_replace to add some <li> tags of my own.

    This is what the default looked like:

    wp_nav_menu( array( 'theme_location' => 'primary' ) );
    

    Here's my modified version:

    // First open up a database connection
    $db = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    

    What I did next was basically call up the links (called "guid" in the table) and post titles from "wp_posts" table in the WordPress database. I also needed to get the book names from my own "books" table. The book titles are the same as the "post_title" in "wp_posts".

    // Here's the command I used to call them up
    $command = 'SELECT b.book_title AS book_title, wp.guid AS guid FROM books AS b
    LEFT OUTER JOIN wp_posts AS wp ON b.book_title = wp.post_title
    GROUP BY b.order;';
    
    $books_submenu = ''; // leave this empty for now
    
    // Fetch objects from tables
    $result = $db->query($command);
    while ($data = $result->fetch_object()) {
        $books_submenu = $books_submenu . '
        <li class="menu-item">
        <a href="' . $data->guid . '">' . $data->book_title . '</a>
        </li>'; // Each loop will add another <li> to the variable.
    

    Now comes the part that had me stumped. You can add arguments to the wp_nav_menu function by adding them into the array inside. The important ones I needed were the "theme_location" and "echo". I put all of this in a variable:

    $my_new_menu = wp_nav_menu( array( 'theme_location' => 'primary', 'echo' => false ) );
    

    Setting "echo" to false makes the HTML of the function an editable PHP variable. Now we can manipulate it much more easily:

    $my_new_menu = str_replace(   '>Books</a></li>', // We're going to replace this...
                                  '>Books</a><ul class="sub-menu">' . $books_submenu . '</ul></li>', // ...with this.
                                  $my_new_menu
                             );
    

    And finally:

    echo $my_new_menu; // Result should be a custom dropdown
    

    That's my end of the work. Every time she adds a new book, my plugin uses wp_post_insert() to add the page to "wp_posts" and adds the book to my "books" table. From there, this script should add that new book to the top of her navigation page :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?