Skip Navigation

[Resolved] Question about identifying the page that a view is being called from

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to be able to know what page a View is being displayed on.

Solution: Use the $current_page operator with any Views shortcodes to get information about the page containing the View.

Relevant Documentation: https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

This support ticket is created 5 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by josephC-5 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1340419

Hi there,

I'm making a website for an art gallery. Every artist (custom post type) is connected to some artworks (another custom post type). Each artwork has a repeatable field group, so that a given artwork can be associated with multiple images.

On each artist's page, there's a grid of thumbnails. The thumbnails originate from the first item in the repeatable field group of each artwork post.

In order to display the first item in the repeatable field group, I ended up having to create a view nested inside another view. Here's my question: is there a way to tell / test what page a view is being executed on? I want to change some CSS classes in the view's output based on the result of some conditionals.

Thanks!

Saul

#1340601

Hi, you can use the special "$current_page" operator to set the context of a shortcode dynamically based on the page being loaded. Place this in any post View's loop to see how it works:

Current page: [wpv-post-id item="$current_page"]<br />
Current post in the loop: [wpv-post-id]

This technique works with Types field shortcodes as well. Let me know if you have questions about this implementation and I can give you more feedback.

Documentation for the "item" attribute:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

#1340621

Christian,

Thanks for your response. I think what I need to do is to determine whether a view has been called from a specific post type rather than a given post ID. Is that possible?

Saul

#1341307

Yes, building on the previous example, you can use the $current_page operator in the wpv-post-type shortcode to determine the post type slug of the current page.

Current page post type slug: [wpv-post-type item="$current_page"]
Current loop item post type slug: [wpv-post-type]

https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153408

#1343549

Yes, this was the magic snippet I needed!

Thanks so much, Christian!

Saul