-1

I'm posting this question because I was trying to look for an answer but couldn't find it. I found the answer myself with the help of this answer and I wanted to share it with everybody that would have the same problem.

My goal was to create a menu entry called "archives" and pending from it links for the monthly archives of the posts, and of course, that it was updated automatically and automatically translated into the correct language (we are having a three language webpage powered by qTranslate).

2
  • 1
    This is not a question. May 10, 2014 at 11:00
  • I know, I just wanted to share my research. But apparently I can't post answer to my own question until 8 hours have passed, so I have to wait.
    – lopezvit
    May 10, 2014 at 11:02

1 Answer 1

1

And this is the custom code I put in my "functions.php" of my child theme. It is important to notice that there are many things to get improved (like creating an id for every submenu and grab the classes dinamically in case you change the theme) but this was a "quick and dirty" solution. If I have ever time to improve it, I will post it. So here is the code:

function create_archives_menu_entry( $item_output = '', $item = '', $depth = '', $args = '' ) {
    global $post;

    if ( $item->type == 'custom' && $item->object == 'custom' && $item->attr_title == 'archives') {
    //We eliminate the title since we use it just for selecting the correct entry
    $item_output = str_replace('title="archives" ' , '', $item_output);
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
        $indent_sub = ( $depth ) ? str_repeat( "\t", $depth + 1 ) : '';
        $item_output .= "\r\n$indent<ul class=\"sub-menu\">\r\n";
        $item_output .= wp_get_archives( array( 'type' => 'monthly', 'format' => 'custom', 'before' => $indent_sub . '<li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2407 fusion-dropdown-submenu">', 'after' => '</li>', 'echo' => 0 ) );
        $item_output .= $indent . "</ul>\r\n";
    }

    return $item_output;
}

add_filter( 'walker_nav_menu_start_el', 'create_archives_menu_entry', 10, 4 );

To make it work, you just need to add a custom link to your menu with the title attribute: archives.

And here is the working version

PS:Here is the documentation that I used to find my solution: wp_get_archives

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.