WordPress.org

Forums

WP MVC
interacting with the menu system (2 posts)

  1. royjanik
    Member
    Posted 1 year ago #

    So I opened an issue over on Github, but I'm not even sure it'd accomplish what I'm actually trying to do.

    I'm trying to figure out a way to add items dynamically to the navigation menu of my WordPress theme using WP MVC. I thought the best way to do that would be to create a custom layout that renders my data as li tags. And then use the output of that to put into the menu using the wp_nav_menu_items filter. But that proved problematic, because I can't just capture the output of the rendering.

    I have a solution now, but it involves forgoing the WP MVC framework and doing some sql queries and such outside the context of WP MVC.

    Is there a more kosher way of going about what I want?

    http://wordpress.org/extend/plugins/wp-mvc/

  2. tombenner
    Member
    Plugin Author

    Posted 1 year ago #

    To avoid the need to write SQL for this, I've added a function (in 1.1.4) called mvc_model, which lets you instantiate an MVC model anywhere in WP (e.g. in the theme). You can call find() on it to get objects and then iterate through them to create the HTML to pass into the wp_nav_menu_items filter:

    $venue_model = mvc_model('Venue');
    $venues = $venue_model->find(array('limit' => 5));
    $items_html = '';
    foreach ($venues as $venue) {
        $items_html .= '<li><a href="'.mvc_public_url(array('controller' => 'venues', 'id' => $venue->id)).'">'.$venue.'</a></li>';
    }

    I've also added another function called mvc_render_to_string() which lets you render a view to a string. The example in the documentation should show how to use it, but I haven't thoroughly tested it yet.

    If you have any questions about this or anything else, definitely just shoot me a line.

    Best,
    Tom

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags