Skip Navigation

[Gelöst] How to conditionally display the content based on current post terms assignment

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
How to conditionally display the content based on current post terms assignment

Solution:
You will have to write custom shortcode to get posts count based on current post term assignment and then you can use this custom shortcode with conditional block.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/view-block-check-for-no-results-and-how-to-alter-the-default-text/#post-2377057

Relevant Documentation:
- https://toolset.com/course-lesson/using-toolset-conditional-block/

This support ticket is created vor 2 Jahren, 6 Monaten. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 9 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Martin vor 2 Jahren, 6 Monaten.

Assistiert von: Minesh.

Author
Artikel
#2376177

Hi,

I have a CPT template with a view block that shall show related posts to this CPT (CPT "Brands" and posts that are related to this brand...).

Sometimes there is a CPT bost (a brand) which is not connected to a post so far.

A): I like to use a conditional block that checks if one or more posts are related (only then the view block and some heading blocks before getting shown. If no related posts exist, nothing shall be shown.

With the classic editor, I can achieve this with some code like

[wpv-conditional if="( '[wpv-post-title item='@relationship-slug.parent']' ne '' )"]
          ... has parents ...
[/wpv-conditional]

How can I do this with the conditional block?

B): Currently the view block displays "No Results" when there aren't any. How can I alter this default text to something more meaningful (and in German)? In the Block section on the right hand, I do not see any option for this...

Thanks!
Maritn

#2376377

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

A): I like to use a conditional block that checks if one or more posts are related (only then the view block and some heading blocks before getting shown. If no related posts exist, nothing shall be shown.

With the classic editor, I can achieve this with some code like

[wpv-conditional if="( '[wpv-post-title item='@relationship-slug.parent']' ne '' )"]
... has parents ...
[/wpv-conditional]

How can I do this with the conditional block?
==>
To query the child post we will require to create a block view to get total count of child post but for display the only child post count we can use it but to display information conditionally, either we will require to use the custom shortcode that should return the child post count and then we can use that custom shortcode with conditional block.

If you can tell me what section and on what post you want to hide when there is no parent post attached, I can review your current setting and then guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

B): Currently the view block displays "No Results" when there aren't any. How can I alter this default text to something more meaningful (and in German)? In the Block section on the right hand, I do not see any option for this...
==>
You should select the "View Loop" from your view block and then on right sidebar, you should select the "Loop template" and within that section you can change the "No Results" found text. Please check the following screenshot:
- versteckter Link

#2376439
View Selection - post to a CPT according tpo the same Taxonomy tag.JPG

Hi MIneshm

thanks for the fast response. Ad B) thanks for the direction to "View Loop" section. I was looking at the "View" section above... I will not forget this 😉

Ad A) I found (your) support ticket:
https://toolset.com/forums/topic/view-to-display-child-post-count-not-working-when-logged-out/

And assumed to use a variation of the code:

function func_get_total_count( $atts ){
global $post;

$childs = toolset_get_related_posts(
$post->ID, // the parent post
array('cb-brand','post'), // the relationship slug
'parent', // the $post->ID role in this relationship
1000, // the maximum number of results
0, // the offset
array(), // additional query arguments
'post_id', // return format
'child', // role to return
'',
'ASC'
);
return count($childs);
}
add_shortcode( 'show_total_count', func_get_total_count' );

I assume that " array('cb-event','post'), // the relationship slug" is not correct somehow. And I just viewed the settings of the view and saw that the posts related to a brand are not set via a relationship but the the viewa grab all posts that relate to the same taxonomy tag (!).

So, e.g. if posts are set to the taxonomy tag "Las Vegas Show" and the brands is also set to the same tag, post shall (and do= show up, if thewre are any tagged.

So, as I d not use a relationship but the taxonomy, how might I get the code to work?

I attached a screenshot of the seleciotn part of the view for better understanding (though it is German... says:

Taxonomy filter

Select posts with taxonomy:
Categories same as the page on which this view is displayed

#2376467

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

If you are not using post relationship then its totally different story.

As you said you are using taxonomy, I will require to review your setup and I would like to know what section you want to show/hide based on what condition and on what page/post.

Can you please share admin access details and problem URL where you want to show/hide section based on taxonomy?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2376487

Dear Minesh,

as the website is fully operating, I will not be able to grant an admin access. Unfortunately, I would welcome your help.

#2376989

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Here is a sandbox site I've setup for you, you can login to it using the following link:
=> versteckter Link

It it possible for you to setup your structure on this site (there are enough post types and taxonomies already created), once you setup your structure there you can tell me with what post type and taxonomy you want to compare the things and where you want to display it conditionally.

#2377017

Dear Minesh,

you are great - I just replicated my case! Saves us a few 100 GBs tp share from a backup...

You will find a CPT "CB Event" with taxonomy "Category". Now I can tag an event with a certain category (e.g. "USA" or "Europe"). If standard posts are tagged too, a container (in soft red) shall show up (and does).

If no posts are tagged, the container shall NIOT show up. How can I achieve this?

#2377019

hint: I added two links into the menü so that you can call each of the two events (with/without corresponding tagged posts).

#2377057

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

I've added the following custom shortcode that will return 1 if there are no posts found based on the current post terms otherwise it will return 0.
=> versteckter Link

add_shortcode('get_related_property_count','func_get_related_property_count');
function func_get_related_property_count(){
  global $post;
  
  $assigned_terms = wp_get_object_terms($post->ID,'category',array('fields'=>'slugs'));
  
  $assigned_posts = array();
  if(!empty($assigned_terms)) {
        $assigned_posts = get_posts(array(
        'post_type' => 'post',
        'numberposts' => -1,
        'tax_query' => array(array(
            'taxonomy' => 'category',
            'field' => 'slug', 
            'terms' => $assigned_terms
           ))));
  }
  
  if(count($assigned_posts)==0){
    return 1;
  }else{
    return 0;
  }
 
}

Then, I've register the above shortcode name: get_related_property_count - at:
Toolset => Settings => Front-end content tab v=> Third-party shortcode arguments and add the shortcode name there
=> versteckter Link

Then, with the content template I've added the conditional block and set the conditional block to use custom shortcode and set the condition and moved the container you created that displays the content within the conditional block:
=> versteckter Link

Please check the following screenshot: versteckter Link

Can you please confirm it works as expected:
- versteckter Link
- versteckter Link

#2377097

Perfekt. The short code works perfectly! Great help in the details. Thanks for your time and patience! And it helps to deal with similar situations by my own.