Skip Navigation

[Resolved] Conditional display for "$current_page" returns incorrect result

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 4 replies, has 3 voices.

Last updated by webD-3 6 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#901192
webd3-ivfa-district-map.png

This code works just fine on individual posts. But on the archive page for the post type, one of the individual posts is getting the current-menu-item class even though it is not the current page. Image of the archive page attached; see "District 17A" in the sidebar, where this loop output is displayed.

My template for this loop item:

<li class="menu-item [wpv-conditional if=([wpv-post-id]=[wpv-post-id id='$current_page'])]current-menu-item[/wpv-conditional]">[wpv-post-link]</li>

Note that "District Map" at the top of the list is properly receiving the current-menu-item class; I have that coded outside the loop.

#901268

$current_page will return the ID of the current page.
Archives are no pages, they have no ID in that sense.

Now, however that single item receives the wrong class, I can see all the others do not.
Hence this is an issue with the single post.

If you re-save or even re-create that post, is the issue persisting?

Then we need to know how you create the archive, what ID you expect, and which you get instead.
To do this you can simply compare the outputs of [wpv-post-id], [wpv-post-id id='$current_page'] in the related archive, before you add a condition by them.

You will immediately recognize that your archive is not a page and most likely you receive the wrong value from [wpv-post-id id='$current_page'].

I don't think this will work like this.
Rather probably you need to compare the current archive term or type with the title or slug of your posts.

#902200

The code I am using to detect the Archive page is outside the loop, and is different:

[wpv-conditional if="( '[wpv-archive-title]' eq 'Districts' )"]current-menu-item[/wpv-conditional]

So this (I believe) is totally independent of the issue I am having with District 17A receiving "current-menu-item" when viewing the Archive page.

I re-saved the District 17A post but the issue persisted.

When I view any of the individual district posts, only the post being displayed receives the current-menu-item class, including District 17A.

I have opted to work around the issue. My new loop item template:

<li class="menu-item [wpv-conditional if="( '[wpv-archive-title]' ne 'Districts' )"][wpv-conditional if=([wpv-post-id]=[wpv-post-id id='$current_page'])]current-menu-item[/wpv-conditional][/wpv-conditional]">[wpv-post-link]</li>

Since the problem was only happening on the Archive page, I'll just test to make sure we're not on an archive page, before adding current-menu-item to any loop item.

#902221

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

As Beda mention the [wpv-post-id id='$current_page'] won't work as intended since its on an archive page. You will need to use something else to determine the page. I see that you are using the [wpv-archive-title] this can be used for archives.

Please let me know if your final testing works and if any assistance is needed.

Thanks,
Shane

#902242

Shane,

My workaround seems to be fine. I'll detect the Archive page outside the loop, for "District Map" to get the proper current-menu-item class only on the archive page. And since the District posts are properly getting the current-menu-item class when viewing District posts, I'll just conditionally not perform that check on the archive page.