Skip Navigation

[Resolved] Show/Hide Views if page is a post archive or if page is a category archive.

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

Problem:
The user is using the same archive template for the custom post type and certain taxonomies. He uses two views he would like to use inside of the archive template and display one of them for post type archive page and the other for the taxonomy archive pages.

Solution:
This can be implemented using the Conditional block, but you will need to create, and register, a custom function to be used inside the conditional block. Check the article below

The function can use WordPress core functions to check if the current page is the post type archive or the taxonomy archive.

Relevant Documentation:

100% of people find this useful.

This support ticket is created 3 years, 8 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.

Our next available supporter will start replying to tickets in about 1.37 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1984611

I have a post type called Projects which has categories called Project Category.

I have 2 slightly different views at the top of an archive page and that archive page is assigned to both Projects post archive and Project Category archive.

I want one of those views to show on the Project posts archive page and the other to be hidden. On the Project Category archive I want that reversed.

I've been trying to figure out how to do this using the Conditional block but can't figure it out.

Is this possible?

Thanks

#1984883

Hello and thank you for contacting the Toolset support.

This can be implemented using the Conditional block, but you will need to create, and register, a custom function to be used inside the conditional block. Check this article https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/

The function can use WordPress core functions to check if the current page is the post type archive or the taxonomy archive:
- https://developer.wordpress.org/reference/functions/is_post_type_archive/
- https://developer.wordpress.org/reference/functions/is_tax/

I hope this helps. Let me know if you have any questions.

#1985209

Thanks. I was already trying that last night before posting but couldn't figure out the custom function code. Guess I needed some sleep as it was much simpler today.

For those with the same question, add the following to your child theme's functions.php file (or the custom code section in Toolset Settings which would probably work too):

/* Detect if page is a post archive. Used with Toolset Conditional */
function myfunc_is_post_type_archive(){
	return is_post_type_archive(); /* Returns TRUE 1 or FALSE 0 */
}

"myfunc" is just text so that I know the function was created by me and so it won't conflict with existing functions. Change to whatever you like.

Register the name of this function "myfunc_is_post_type_archive" in Toolset Settings -> Front-end Content -> Functions inside conditional evaluations. Once you do this, the function will be available inside the Toolset Conditional block.

To check if the current page is a post type archive, in the Conditional block's "Edit conditions" screen, choose "Custom function" from the first left hand menu, then in the second select your custom function "myfunc_is_post_type_archive". You can leave the parameters field empty. Set the middle option to "=". Then set the right hand menu to "Static value" and the resulting Value field to either 1 (true - the current page is a post type archive) or 0 (false - the current page is NOT a post type archive).

I found you could also register the native WP function is_post_type_archive in Toolset(avoiding the need to create a custom function in functions.php). Then follow the same steps as above except instead of leaving the parameters field blank, fill it with 2 empty parameters...

'', ''

Hope this helps.

Please mark this as resolved.

#1985535

Thank you for sharing your finding with our users. All the best for your project.