Month archive in menu option for users

Hi!

An option our users really are asking us about is how they can get displayed an archive in the menu showing the months. See attached screenshot or see this blog: http://littleken.dk
When you hover "Arkiver", a dropdown is showing all the months the users has blogged. If you click the option, all the post are showing published in that month.

  • Predrag Dubajic
    • Support

    Hey @Mathias,

    Hope you're doing well today :slight_smile:

    I have checked your site and everything seems to work as expected there, so I'm guessing how you can recreate this menu, am I correct?

    If that's the case than you can go to Appearance > Menus and simply add new menu items using the Custom Links field.

    If you're not sure which links you need to add simply go to Appearance > Widgets and add archive widget to your sidebar or footer, it will list all your available archives so you can get URL from there.

    Let me know if I missed the question completely :slight_smile:

    Best regards,
    Predrag

    • Mathias
      • The Bug Hunter

      Ah, I see. But it it possible to create an option for this, so the users doesn't have to copy and paste every link to create this menu? So the user just can add a single menu item, and then it just displays all the months in the dropdown.

  • Adam Czajczyk
    • Support Gorilla

    Hey Mathias,

    I hope you're well today!

    I think you may want to try out this code:

    function wpmu_monthly_archives_shortcode() {
    	$args = array(
    		'type'            => 'monthly',
    		'limit'           => '',
    		'format'          => 'html',
    		'before'          => '',
    		'after'           => '',
    		'show_post_count' => false,
    		'echo'            => 0,
    		'order'           => 'DESC',
    		'post_type'     => 'post'
    	);
    	return wp_get_archives( $args );
    }
    
    add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
    function your_custom_menu_item ( $items, $args ) {
        if ($args->theme_location == 'primary') {
            $items .= '<li><a>Archives</a><ul class="sub-menu wpmu-monthly-archive">'.wpmu_monthly_archives_shortcode().'</ul></li>';
        }
        return $items;
    }

    To apply it to your site, copy and past it to your current theme's "functions.php" file. It should add an "Archives" drop-down menu to your "Primary" menu on site where drop-down menu elements are links to monthly archives.

    Let me know please if it helps!

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    It is supposed to automatically add "Archive" item to the menu that's set as "primary" on your site. I understand though that that's not exactly what you're looking for so I assume the code will have to be modified a bit so let's please make sure that we're on the same side here :slight_smile:

    this should be optional for the user.

    Is it a Multisite WP network then and admins of sub-sites should be able to add "Archive" menu item if the want? Is that right?

    Or is it a single site (standard) WP setup with just multiple authors? If so, could you please share an example "case scenario" with me?

    Please advise! I'll be happy to tweak the solution the way it would work for you then.

    Best regards,
    Adam

  • Predrag Dubajic
    • Support

    Hi Mathias,

    This is possible but it would require bit more more work, you would need to create custom on/off field, for example checkbox, via functions.php or mu-plugin, something like this should help you get started:
    https://wordpress.org/support/topic/how-to-add-a-simple-checkbox-to-settingsgeneral

    Once you have checkbox you can wrap Adam's above code so it's used only when checkbox is set to true.

    Best regards,
    Predrag

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    I think I can provide you with a sort of "in-between" solution. Assuming admins of your network's sub-sites do have access to the "Plugins" menu in their dashboards and are able to enabled/disable some plugins - an "on demand" feature could be provided this way.

    I've attached a .zip file to this post that contains basically the same code as I shared previously but wrapped as a plugin. You could download it and then:

    - extract .zip archive to your local storage
    - access your server via FTP
    - upload the "monthly-archive-in-menu-multisite.php" file to the "/wp-content/plugins/" folder on your server

    Once this is done a new plugin called "Monthly Archive Menu Item" appears on plugin's list for each site (including main) site. Assuming there are no additional limitations put on plugin activation (for example via Pro Sites plugin), admins of sites will now be able to click on "Activate" link enabling plugin (therefore adding "Archive" menu item) their site's menu.

    Please note though that you will not want to "Network activate" plugin but activate it "site-by-site". Also, in case you'd like to translate it you will want to edit plugin's code directly (it doesn't support translation files) by replacing "Archives" word inside this line:

    $items .= '<li><a>Archives</a><ul class="sub-menu wpmu-monthly-archive">'.wpmu_monthly_archives_shortcode().'</ul></li>';

    Please note as well that this is not an official WPMU DEV plugin.

    I hope that helps!

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias,

    I hope you're well today!

    I've just downloaded the plugin, uploaded it to my test multisite install and activated on one of sub-sites. It did add the monthly archive menu (see screenshot).

    Have you "network-enabled" it or enabled it on any of the sub-sites?

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    Have you checked that only on a single sub-site or on a few different sub-sites? Does these sub-site use the same theme or different themes?

    Also, would you mind me taking a closer look at your setup? You may grant me an access to it via our WPMU DEV Dashboard (it can be removed when no longer necessary). Here's a short guide on how to do this:

    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    I asked for access only to take a look at how the theme(s) that you're using are built, because I suppose that's the reason the plugin is not working. Unfortunately, the fact that sub-sites use different themes and as far as I remember there's really many of them, complicates that a bit.

    Last time (before I posted here recently) I tested this with Twenty Sixteen WP theme on my multisite setup and it worked great. Yet, there's no way of knowing if other themes are using e.g. the same "menu location" or if they alter the way the menu is generated. In other words, the plugin is made to work only with themes that strictly follow WordPress codex suggestions.

    That said, as it doesn't seem to be working for you, I think some more advanced custom development would be necessary here. You may want to ask for custom coding of such a feature on our "Jobs & Pros" job board (please note: no WPMU DEV staff members involved!) here:

    https://premium.wpmudev.org/wordpress-development/

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    Would you be able to make this work if you got support access or got logins so that you could get a closer look at the theme?

    I'll be happy to take a closer look. I'd need a full direct access to the site because editing files through WP dashboard (and this is the only way available if only "support access" is granted) brings too much risk as I wouldn't be able to revert/remove changes in case anything went wrong.

    If you wish me to check that out, please send in:

    Subject: "Attn: Adam Czajczyk"

    - your site's WordPress admin login URL
    - admin login and password (may be a temporary account)
    - FTP credentials (hostname, username, password, port number if needed)
    - any other relevant URLs and information
    - link back to this thread

    Please use our contact form here https://premium.wpmudev.org/contact/.
    Select "I have a different question" from the drop-down list.

    Please be sure to make a full backup of your entire site first!

    Please note however that this is a custom development job and since we do not provide such services I have already gone as far with this as possible. Therefore, I'll gladly check my existing code compatibility against your setup and if possible, I'll make it work. I'm not able to give you any warranty on this and if it turns out that due to some site-specific reasons the code doesn't work with your setup, I'll let you know here. In such case you may want to ask for a custom development on our "Jobs & Pros" job board (no WPMU DEV staff members involved though!) here:

    https://premium.wpmudev.org/wordpress-development/

    I hope that's fair enough :slight_smile: If you wish me to proceed please provide me with access credentials as described above.

    Have a nice day!
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hello Mathias!

    I received your e-mail. I'll take a look at the site and see what I can do. I'd appreciate if you granted me some time though. I'll report to you back here soon and in case I'd need any other detailed information on your setup, I'll get in touch with you via replay e-mail.

    Please keep an eye on this thread!
    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.