I have looked at the documentation and searched around a bit and haven't found an answer yet. I'm wondering if I can insert PHP code into the view loop through the layout editor. I need to do some additional work with the layout and extra functionality that I need php to do. The easiest thing would be if I could just put php right into the loop to add the logic I need. If not, can I custom code the view or access the loop through php code and write my own view?
For a quick example, when looping through the posts associated with a view I need to check the title and based on the title, format certain posts differently. I also need to add a check/uncheck box next to each title that when clicked will submit the page (or run jquery code) and then change the image to the opposite. I also need to be able to hide/show things based on if the user is logged in or not. I am currently doing all of this is a custom page template but I want to do this using a view instead.
Is this possible?
Dear Diego,
You can achieve the last and first requirement using Views wpv-if condition, https://toolset.com/documentation/views-shortcodes/#wpv-if & https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ .
For the first one you need to write custom function and custom shortcode for checking Post Title format. Then you can check the custom shortcode using wpv-if.
You have to write custom functionality for the second, it will be little hard to achieve and we are almost helpless for that one.
For last one you can use wpv-current-user shortcode inside wpv-if for checking user is log-in or not, please check following links for more details https://toolset.com/documentation/views-shortcodes/#wpv-current-user
--
With Regards
Bigul
1. How could I write a custom function or custom shortcode that could be used with the wpv-if? Within the view HTML layout do I have access to the individual post IDs and can I call the built-in wordpress php functions to get the category and title and whatnot?
2. I think what I really need to do is basically hand-code a view in php using a custom post type template. So I would essentially just be using the view to get me the array of posts that are associated with a particular custom post type entry. I looked at the documentation again and saw that in php this method is available: get_view_query_results() where you pass in the ID of the view. I thought that by using this it would essentially give me the list of posts that that view would show. But I can only call this after the View is displayed on the page...so essentially it is displaying it twice. Is there a way I can get the array of posts in a particular view and display that just through php? Would I have to make direct SQL calls to the wordpress tables?
Dear Diego,
Please share me more information about the steps of "Post Title" checking, I will help you to write a short code. You can write a function for Post Title checking in functions.php of your theme. Then add the function to shortcode and use that shortcode in wpv-if condition.
You can display Views output by using code, please refer this tutorial https://toolset.com/documentation/user-guides/views-api/
–
With Regards
Bigul
Hi Bigul,
I tried using the get_view_query_results() to loop through the post IDs associated with my view in my post template but the problem is that the View itself is already displayed in the post since the shortcode is in the content of my post. If I remove the shortcode from my post and then try to use the get_view_query_results() it just displays all of my posts, not only the ones that are filtered by the view.
Is there a way to make this work? I feel like doing it this way may be the only way for me to make it work. I don't think I will be able to do everything I need to by creating shortcodes.
As I mentioned originally one of the key things I need to do is have a checkbox next to each post displayed. When it loops through, it looks up in a table if that post has been viewed, so I need to know the post ID during the loop so I can do the lookup. And then clicking that checkbox would fire a javascript function that posts the page so it can update that same table to check/uncheck that setting. So I suppose I could use the View the regular way if I could create a short code that accepts a post ID as a parameter and does the lookup. But would I have access to the post ID within the view loop in order to do that?
Also, as for the post title stuff, the videos that I am displaying are sometimes in a series. So there could be 10 videos in a series. The post titles are all the same except with "(Part 1)" or "(Part 2)" etc at the end of it. So I would need to be able to check the title and if it was the same series display the title only once and then display links to each part with only the text "Part 1" or "Part 2".
Dear Diego,
You can use Custom ShortCode for checking the Post Title format and it will be inside the Views Layout(Views Edit screen) like this https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#Checking for empty fields.
For checkbox requirement, it will better to keep the Post ID as checkbox value. You don't need to write short code for this, you can use View Post ID shortcode for this.
Please let me know your feedback.
--
With Regards
Bigul
Hi again,
Its not an actual html checkbox. I am using conditional images. I have actually just written a shortcode that works and accepts the parameters of post_id and user_id.
If my shortcode looks like this: [viewedpost user_id="" post_id=""] how do I embed the view shortcodes for post id and user id in there as the parameters?
I tried this [viewedpost user_id="[wpv-current-user info="id"]" post_id="[wpv-post-id]"] but this does not work. It doesn't recognize the embedded shortcodes as parameters.
I figured out how to get the nested shortcodes working. I may still have an issue with formatting by category so I will try a couple things and post back again. Thanks.
Dear Diego,
Please try following code and hope it will work for you.
[viewedpost user_id='[wpv-current-user info="id"]' post_id='[wpv-post-id]']
--
With Regards
Bigul
Yep that's what I did and it did work. Thanks for all of your help!