• In my installation, I am required to add a label (think of it like a category, but not a WP cat) to both the blog post’s title, and content. I wrote a plugin that adds a custom field to the title and content using the selected label, and it has been working fine for years, except in the menus.

    Example: Say that I have three labels, L1, L2, and L3. When I create a new post, I click a button to give the title a label, say L1. Then I click another button to give the content a label, say L2.

    When I publish the post, my plugin hooks into the save_post function and adds the label to the posts meta table as a custom field.

    When the post is displayed, the label is displayed via add_filter(‘the_content’, ‘customcontent’);

    The problem is that when the post titles are retrieved from the db on the wp-admin/nav-menus.php page, it doesn’t pull the custom field and only shows the default label.

    When I create and apply the menu, then go to the main page, the value displayed in the custom field/label on the menu is the value of the label on the top post.

    Any ideas on how to fix this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Not sure how it adds the label to the title when you’re hooking in to the_content filter. I believe that should be only filtering on post_content field and not post_title

    Thread Starter danhgilmore

    (@danhgilmore)

    Ah, I messed that up, William, you’re right. I use the the_title filter for the title, and the_content filter for the content.

    Try adding another hook on the filter nav_menu_attr_title to fire the same function you use on the front-end and see if that works for you.

    Thread Starter danhgilmore

    (@danhgilmore)

    I think this is what you mean?

    add_filter('the_title', 'my_the_title');
    add_filter('nav_menu_attr_title', 'my_the_title');

    the first filter fires and works, but the second does not.

    Hmm, after looking at this a little more, seems like the_title should be working. It looks like it uses get_the_title() which is filtered by the_title.

    The wp-admin/nav-menu.php code is all over the place though, so it’s kinda hard to follow.

    Actually, where is that add_filter() call located? Is it in a functions.php file for a theme? Is it in a plugin?

    Thread Starter danhgilmore

    (@danhgilmore)

    It’s in a MU-plugin

    Thread Starter danhgilmore

    (@danhgilmore)

    UPDATE: It seems that when the page is rendered, the nav menu items are filtered via the_title just like the actual titles are in the loop. Is there a way to filter just the posts, and not nav menu items?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom Fields and menu items’ is closed to new replies.