Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Fillter the current URL on subpages WordPress

EDIT: I am searching for a solution myself and the best I have come up with is the following type of script:

- First, check if the current URL looks like this someurl.com/XXXXX/page/Y/ (where XXX is a random string and Y is a random number)
- After that use http://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_css_class to add the class "current-menu-item" to all menu items with pointing to someurl.com/XXXXX/

This should, at least partially, solve the problem and I think it is also easier to do.

Y


Hi everyone,

I think, that this should be easy for people with some php knowledge. We are working on a site which relies on the current page marker and displays a couple of menus depending on it. It works fine, but we have a problem with pagination on archive pages. While the current menu item is detected properly on the first page of the archive, for example:

www.blog.com/archive/

it breaks as soon as one moves to the second paginated page. Pagination uses the following structure:

www.blog.com/archive/page/2/
www.blog.com/archive/page/3/
www.blog.com/archive/page/4/
...

What I need is a filter which changes the current menu item url for

www.blog.com/XXXXX/page/Y/

to

www.blog.com/XXXXX/

(XXXX will vary, we use a couple of different taxonomies and post types).

Thanks in advance!

Y

Answers (2)

2012-12-28

Arnav Joy answers:

so you do not want pagination?


Yavor Trampov comments:

I do want pagination to work, URL should also stay the same. The only thing that needs to change is that

www.blog.com/archive/page/2/

should be equal to

www.blog.com/archive/

<strong>when comparing to the menu structure in order to attach the current menu item class only.</strong>

So, in menus on www.blog.com/archive/page/2/ the menu item for www.blog.com/archive/ should have the class .current-menu-item


Arnav Joy comments:

can you share url of your site?

2012-12-28

Duncan O'Neill answers:

Hi there,

in theory, the menu item for www.blog.com/archive/ should have the class current_page_ancestor. If that's the case, then all you'd need to do is update the css in your theme's style.css page. Find references to .current-menu-item, and add another css selector to that declaration. e.g;


.current-menu-item {font-weight:bold;}


would become


.current-menu-item,
.current_page_ancestor {font-weight:bold;}


I can do this for you if you want to message me with FTP details.

best,


Yavor Trampov comments:

Hi,

unfortunatelly this is not the case, but here's a bit more info - started digging into this and realized, that I missed quite a few things.

We are using two plugins to extend the basic functionality of wp_na_menu:
http://wordpress.org/extend/plugins/codepress-menu/
and
http://wordpress.org/extend/plugins/advanced-menu-widget/screenshots/
This is just FYI - I turned both of them off to test and the result was the same.

What happens in my opinion - WP is changing the ?paging=2 to /page/2/ based on permalinks settings. Thus, on terms of comparint to menu items the page is completly new. What we need to do is filter the URL before it gets compared to menu items and remove the pagination attributes. I think, that this is a similar solution, but I could not get it to work:
http://www.rarescosma.com/2010/11/add-a-class-to-wp_nav_menu-items-with-urls-included-in-the-current-url/

I uploaded the example to a staging server, so you will be able to see it better. This is the archive page:
http://plaut.ahorn-design.com/downloads/

At the top, the text "Presse" and the links below it are actually the current branch of the main menu. When you click on the pagination links at the bottom, you see that this menu disappears. The URL changes to http://plaut.ahorn-design.com/downloads/page/2/ This is what I am trying to avoid.


Duncan O'Neill comments:

Do you have a paged.php file in your theme folder? If so, try renaming it, to paged.bak.php, or whatever. Does that fix the problem?


Duncan O'Neill comments:

Just a bit more information re the above reply. Referring to this [[LINK href="http://codex.wordpress.org/images/1/18/Template_Hierarchy.png"]]template hierarchy diagram[[/LINK]] it looks possible, if not likely, that http://plaut.ahorn-design.com/downloads/page/2/ and http://plaut.ahorn-design.com/downloads/ are being controlled by two different files.