Skip Navigation

[Résolu] Problem with conditional testing a View’s output

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a View that returns some text. I would like to use the output of that View in a conditional statement on another part of my site. I have already added the raw output PHP filter, and the output contains the correct text if I display the View on the front-end. However, the conditional still fails because of empty space in the View's output.

Solution: Be sure to remove all unnecessary empty space in the Loop Output editor of your View. The code should appear with no line breaks like this:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-field name="wpcf-price"]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

Empty space surrounding the output will cause the conditional to fail.

This support ticket is created Il y a 7 années. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : ,

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par toolset-dave Il y a 6 années et 12 mois.

Assisté par: Christian Cox.

Auteur
Publications
#596228

Hi, I have a view (nejnizsi-cena-varianty-zazitku, ID 972) for displaying the lowest price from all the prices which are children of parent CPT. I want to conditionally display "free" if the price is 0.

Amount: [wpv-view name='nejnizsi-cena-varianty-zazitku']<br>
Conditional: [wpv-conditional if="( '[wpv-view name='nejnizsi-cena-varianty-zazitku']' eq '0' )"]free[/wpv-conditional]

The code above displays correct amount, but if the amount is zero, it will not show the text "free".

I also use this shortcode for cleaning up the output of view:

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 880,881,972,975 );
  if ( in_array( $id, $ids ) ) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}
#596339

Hi, can you inspect the page source and copy + paste the HTML generated for this section?

Amount: [wpv-view name='nejnizsi-cena-varianty-zazitku']<br>

I'd like to see exactly what is generated by your View on the front-end.

#596354

Hi, it´s this output if there is any price to show:

Amount: 
	
		0
	
	<br>

If there is no price:
[php]
Amount: <br>
[php]

#596423

Okay I can see that there's still some empty space around the output of the View, which is causing the conditional to fail. To fix this, you must eliminate all extra spaces and line breaks from the View's output. Your Loop Output should be one continuous line with no spaces or line breaks, like this:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-field name="wpcf-price"]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

Notice that I'm using wpv-post-field instead of a types field shortcode. The wpv-post-field shortcode will return the raw data for this field without not any extra markup. Replace "wpcf-price" with the slug of your price field. Use the "wpcf-" prefix, so if your field slug is "the-price" then you should use "wpcf-the-price".

Make sure there is no empty space or empty lines at the beginning or end of the Loop Output editor. Then remove the shortcode [wpv-filter-meta-html] from the Filter and Loop Output Integration Editor, and make sure there is no empty space in this area as well. This should remove all the empty space around the View output, so it should begin to function correctly in your conditional.

#596443

Thank you for your help, it works nice.