0

I am building a website that will display posts in a list fashion, and will have a toggle to switch the view to a grid. There will be slightly different content displayed, so I will need to have two different loops. What is the best practices for handling something like this? Should I have a parameter added to the page like ?view=list and ?view=grid and then have a conditional in the template looking for the URL parameter - or is there a better way of handling this within WordPress?

1

If the content does not change you could surly do this with CSS. The fact that in the grid view the content itself will be different,if even slightly, means you will need to alter the loop or make a new one.

I would suggest having a look at get_template_part, this and this to see how you could use get_template_part.

So just have an event listener on the button that changes the view and according to that value serve a template part for the view. This means you will most likely want to do this with AJAX or API calls to not reload the entire page but just the changing template.

I would refrain from adding parameters to the url, unless you need the grid view linkable, meaning someone that enters the site on that link and not on the index page will have that view.

Last thing, does changing of the layout per user enhance the experience or usability of the site?

| improve this answer | |
  • I would not want the switch to happen with JavaScript, it would need to be a page reload. I'm already using template parts to serve the different loops. I'm ok with not using parameters, but how would I get the template to load the loop without parameters? – Zach Russell Jun 21 '17 at 16:10
  • a) What is the reload for? b) How else if not Js in client or Node on server are you going to detect a view change? I mean if it has to be a reload you can check for the reload event and load the template then? Location.reload() – lowtechsun Jun 21 '17 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.