• Resolved swinggraphics

    (@swinggraphics)


    I thought others might like this modification, which adds the option to replace the category menu link with the URL of the first post in that category. If the category is empty, it is removed from the menu.

    Insert this at line 205 after ‘case “never”: ….. break;’

    case "link to first":
        if (empty($posts))
        {
            $inc += -1;
        }
        else
        {
            $menu_item->url = wp_setup_nav_menu_item( $posts[0] )->url;
            array_push($result,$menu_item);
            $menu_item_parent = $menu_item->db_id;
        }
        break;

    Insert this at (new) line 448 after ‘<option value=”never”>…</option>’:

    <option value="link to first" <?php selected( get_post_meta($item_id, "cpcm-remove-original-item", true), "link to first" )  ?>><?php _e('Link to first post'); ?></option>

    https://www.ads-software.com/plugins/category-posts-in-custom-menu/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Hi swinggraphics,

    As far as I can tell, the functionality you describe is already possible.

    1. Create a new, empty category
    2. Under Appearance > Menus, add the category to the custom menu
    3. Choose “Create submenu containing links to posts in this category.”
    4. Set “Number of Posts” to 1.

    Check: The category is empty, so it is removed from the menu

    5. Create a new post, set its category to the one you just created.

    Check: The category menu link is replaced with a link to the one post.

    6. Create a new post, set its category to the one you just created.

    Check: Depending on your ordering choice, only one of the two posts is shown. To show only the _chronologically first_ (to name just one definition of ‘first’), choose to order by Date Descending.

    If I’m mistaken, please clarify, perhaps I can extend the ordering options to support your request.

    Thread Starter swinggraphics

    (@swinggraphics)

    Hi Diana, you have a great plugin. I really appreciate it! The modification is necessary to keep the title of the category in the menu and replace its href with the url of the first post in the list.

    I do this because on a number of sites, it would be unnecessary or even un-user-friendly to link to a category archive page.

    Cheers,
    Greg

    Hi Greg,

    still, I don’t think you need to make a modification for this: Just do what I said above, and also set the title of all replaced posts to the name of your category. I.e. just don’t use any of the wildcards in the Post Navigation Label, but set it to the name of your category.

    Kind regards,
    Diana

    Thread Starter swinggraphics

    (@swinggraphics)

    The instructions you gave are incomplete, but the effect is doable without new code by creating two category menu items: the first set to 1 post, uncheck “Create submenu”, remove navigation label, replace only if empty; the second as a child of the first, set to all posts, default navigation label, replace always. It’s a little convoluted, which is why I like the modification that does all that with one option choice.

    Hi Greg,

    thanks for your reply. From your description, I’m afraid I don’t quite understand yet what you wanted to achieve. I’ll follow your instructions this weekend to see what you meant. Either way, I’m glad to hear you found my plugin useful nonetheless, thank you for posting to the support forums.

    Kind regards,
    Diana

    I need this modification, too. I have categories with multiple posts, but I want the main menu item to just go to the first post in that category. I also want the other posts to show up as sub menus.

    The code that swinggraphics posted works beautifully. However, if you ever release an update, the change is lost, the menu no longer works right, and I have confused or angry customers. If you do have some time to add the code and update on your end, it would be greatly appreciated.

    Hi rgbcraft and Greg,

    thank you for your feedback, I’ll take your requests seriously. I intend to incorporate this functionality into the plugin sometime this weekend, but only if I can find a neater way than putting it under ‘when to hide the item’. Perhaps the dropdown needs a rename and use the more general keyword ‘behavior’.

    Anyway, I appreciate your feedback, I’ll do my best to make some modifications this weekend.

    Kind regards,
    Diana

    The instructions you gave are incomplete, but the effect is doable without new code by creating two category menu items: the first set to 1 post, uncheck “Create submenu”, remove navigation label, replace only if empty; the second as a child of the first, set to all posts, default navigation label, replace always. It’s a little convoluted, which is why I like the modification that does all that with one option choice.

    I can’t reproduce this. If I add a post and uncheck ‘create submenu’ then there’s no reason to choose ‘replace only if empty’ because if you uncheck ‘create submenu’, then my plugin shouldn’t be doing anything.

    Thread Starter swinggraphics

    (@swinggraphics)

    Hmm, I wasn’t able to work out how to do it again. I know I did the one time when you said it was doable without modifying the code, but now I can’t figure it out! Resolves in my mind that the modification was actually necessary. ??

    Hi swinggraphics and rgbcraft,

    I’ve just checked in a change into the development branch. Are you willing and able to review this?

    Note the check-in comment, I haven’t implemented a new parameter. Instead, what you should be able to do now, is simply set number of posts to 1. Any items under the category will be moved to under this single post.

    I’d appreciate it if you could test this. If approved, you can locally keep the development version until I bring out a new update that contains this new functionality as well. (I have two more things I want to do before I mark the plugin as version 1.0.0)

    I’d love to hear from you.

    Kind regards,
    Diana

    Thread Starter swinggraphics

    (@swinggraphics)

    Hi Diana,

    This is how I configured the menus. This is what I get out. The desired result is to have the top item link to the first post (not the category) while displaying the title of the category (“Reviews” in this case).

    Compare to menu config with custom code and output with custom code, where the top item label is “Campus & Plazas”, which links to Penn Park—the first post in that category.

    Hi swinggraphics,

    thank you for testing!

    As I see from your screenshot, the category menu item does become a link to the first post, right? You’re right about the title, my fault, that obviously isn’t handled well. I’ll fix that ASAP.

    What I find weird is that you get the first post three times: once in the category link, and twice in the dropdown list, while the query to retrieve the posts hasn’t changed at all in this update.. :S

    Are you using custom taxonomies?

    Kind regards,
    Diana

    Thread Starter swinggraphics

    (@swinggraphics)

    The first post in the category appears twice, which is what I was going for: at the top level, and as the first item in the submenu. The second post in the category has a similar title to the first. If I could control the title of the first post, it would be perfect, thanks!

    Hi swinggraphics,

    thank you for clarifying, glad that the duplicate posts isn’t an issue caused by my code.

    If you grab the latest development version, the correct title should be displayed. Would you be so kind to review this again? I hope this resolves your question.

    Kind regards,
    Diana

    Thread Starter swinggraphics

    (@swinggraphics)

    Excellent!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Replace category link with link to first post’ is closed to new replies.