How to set the WP Query Tag Parameter to Current Page Slug in Oxygen
In the Oxygen Facebook group a user asks:
Hi guys, I’m trying to recreate this situation. I have two pages, “Color”” and “Names” and I created a dedicated template for these pages where I applied a Repeater.
I would like that the Repeater shows posts tagged “color” if the visited page is Color and posts tagged “names” if the visited page is Name.
I know I wouldn’t need a template for just two pages, but this is just a simpler example. In our case we would have several pages. Since we happen to make same design changes to the repeater div, it’s becoming pretty unconvenient to do these edits for every page’s repeater.
So that would create one for all.
Thank you!
This can be solved by declaring a custom function that outputs the slug and calling this function as the value of tag
WP query parameter in Easy Posts/Repeater’s advanced query in Oxygen.
Note: A better solution would be to use the standard tag archive pages with the default WP query instead of static Pages. That said, there are some situations where it is necessary to use Pages. Hence this tutorial.
Step 1
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: [Function] Get Slug
Code:
function wpdd_get_slug() {
return basename( get_permalink() );
}
Set the snippet to run only on the front end. Save changes and activate.
If the URL of a Page is example.com/page-a/, basename( get_permalink() )
will be page-a
.
Ensure that pretty permalinks structure is set – this would typically be Post name.
Step 2
In the Oxygen Template, add an Easy Posts or Repeater.
Select Advanced Query.
post_type: post
tag: Click data > PHP Function Return value > Function Name: wpdd_get_slug > INSERT.
Once done it should look like this:
The value of tag key would be.
Configure the rest of the Easy Posts/Repeater component as needed.
That’s it! With this in place, the posts that have been tagged with “Color” will automatically appear when the Page titled “Color” is viewed and posts that have been tagged with “Names” will automatically appear when the Page titled “Names” is being viewed and so on.