I have 6 Pages on the site, each of which are to include a Block that displays a View from my custom post type, with a Taxonomy filter to display only those posts from a certain category.
The slug of the Page and the slug of the Category match. I'm trying to find a way that I can create one single View, to use on all 6 pages, that will correctly display the posts from the corresponding category.
I could build 6 separate views, and use "Categories are any of the following", but didn't know if there would be a way to use "set by the page where this View is shown" or one of the other options, so that I only have one view to manage in the future and to make this more scalable when new pages/categories are added.
Hi,
Thank you for contacting us and I'd be happy to assist.
To achieve this, you can set the taxonomy filter in the "Query Filter" section to "Set by one View shortcode attribute" option, so that it can read the slug of the target category from the shortcode attribute "wpvcategory".
Screenshot: hidden link
To get the current page's slug, you can use the shortcode "wpv-post-slug".
( ref: https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-post-slug )
This can also be used in the shortcode of the view, like this:
( ref: https://toolset.com/documentation/user-guides/passing-arguments-to-views/ )
[wpv-view name="slug-of-the-view" wpvcategory="[wpv-post-slug]"]
Note: you'll replace "slug-of-the-view" with the actual slug of your view.
As a result, the view will read the current page's slug and will use it to filter the taxonomy by the same slug.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Waqar,
Thank you! I was trying to use one of the new Toolset Blocks and couldn't find a way to do it.
Going the old-fashioned way with a shortcode, as you suggested, worked great.
I do have one new issue, however. When I place the shortcode on my pages, I get the exact result I'm looking for. But, I tried saving the shortcode block as a Reusable Block, and when I did it returned the following:
<p><!--- View not found --->"]</p>
Perhaps this isn't a Toolset issue, now that we're moving into Reusable Blocks. For now I will just add the shortcode in separate block instances on each page. If this is something that could potentially be set up as a Reusable Block, however, that would be great.
Thanks for the update and glad that the view itself works.
I was able to reproduce the issue with the reusable block when it contains a shortcode within a shortcode. This has been shared with the concerned team for further review and I'll keep you updated on it, through this ticket.
For now, a workaround can be to create a custom shortcode that processes this nested shortcode and then returns its output.
Example:
add_shortcode( 'show_output_taxonomy_view', 'show_output_taxonomy_view_func');
function show_output_taxonomy_view_func() {
// get the page slug
$page_slug = do_shortcode("[wpv-post-slug]");
// output of the view with page slug as the attribute
$view_output = do_shortcode('[wpv-view name="slug-of-the-view" wpvcategory="'.$page_slug.'"]');
return $view_output;
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
You can then call this new shortcode [show_output_taxonomy_view] in a Shortcode block, which will save properly as a reusable block too.
regards,
Waqar
Waqar, thanks again. Glad to know that the shortcode-within-a-shortcode issue when placed in a Reusable Block is being addressed.
In the meantime, your suggestion to combine the nested shortcodes into a single shortcode via a function worked exactly as you said.
I now have this implemented, and should there be a new Product to add in the future, the client can just create a new Page and then create a new Category in the custom post type using the same slug, and then insert the resuable block.
Thank you very much!
I've received an update from the development team and this limitation of using shortcode-within-a-shortcode in reusable blocks is not specific to Toolset plugins.
This is something that is not generally supported by WordPress itself ( ref: https://codex.wordpress.org/Shortcode_API#Square_Brackets ) and with Gutenberg blocks, this becomes even limited.
Until WordPress starts offering support for this in the core, workarounds similar to the one that we've discussed will need to be used.
I hope this clarifies.