Skip Navigation

[Resuelto] Need to Strip Views Formatting

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to use a View to output a classname for an HTML element. Different classnames should be displayed depending on whether or not any results are found by the View. In order for this to work correctly, no extra markup or spaces should be included in the View output.

Solution:
- Use output="raw" on the View that outputs your class name:

<span class="[wpv-view name='my-view-slug' output='raw']">content</span>

- Add the following filter code to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '14' ) { //Please adjust to your Views ID
        $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 {
            // handle no results found section
            $start = strpos( $out, '>' );
            if ( $start !== false) {
                $out = substr( $out, $start + 1 );
                $end = strpos( $out, '<' );
                $out = trim(substr( $out, 0, $end ));
            }
        }
    }
    return $out;
}

- Remove all the spaces from your loop output editor so everything is on one line:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>classname-a</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]classname-b[/wpv-no-items-found][wpv-layout-end]

- Finally, remove the filter meta shortcode from the Filter and Loop Output Integration Editor panel of your View:

[wpv-filter-meta-html]
This support ticket is created hace 6 años, 11 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Etiquetado: ,

This topic contains 12 respuestas, has 2 mensajes.

Last updated by Sean hace 6 años, 10 meses.

Assisted by: Christian Cox.

Autor
Mensajes
#520362

Fairly simple: I'm using some conditional statements to change the css and text on a page. For example: If a parent post type has no children, then return a red button. Otherwise, green.

<span class="[wpv-view name="position-colors"]">

Simple. And it works. Except that it returns my code with a bunch of extra formatting. I want the straight up raw text.
That view looks like this:

[wpv-layout-start]
	[wpv-items-found]
[wpv-conditional if="( '[wpv-found-count]' eq '0' )"]redbutton[/wpv-conditional][wpv-conditional if="( '[wpv-found-count]' ne '0' )"]greenbutton[/wpv-conditional]
<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]<em>(No Openings)</em>[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

How can I strip all the excess code it generates? I just want the word "redbutton" or "greenbutton" to be returned.

#520397

Sure, there's a filter that you can apply to generate uncommented raw text output. It's discussed in some other tickets on the forum as a way to generate dynamic options for a generic field - basically stripping the extra code to create a JSON structure - but it works fine for basic text as well.

Add the following code to your theme's functions.php file:

/* --------------------------------------------- */
// LOOP RAW TEXT OUTPUT FOR JSON OPTIONS
// keywords: raw, shortcode, json, options, select, view, output, loop
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '999' ) { //Please adjust to your Views ID
        $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 );
        }
    }
    return $out;
}

Replace '999' with the numeric ID of the View you want to filter, and check the results.

#520404

Nope. It's different, but is still outputs a lot:

<span class=​"<form autocomplete=" off" name=​"wpv-filter-1011-CTID20" 
action=​"/​as-positions/​?​wpv_view_count=1011-CTID20" 
method=​"get" data-viewnumber=​"1011-CTID20" data-viewid=​"1011" 
data-viewhash=​"eyJuYW1lIjoicG9zaXRpb24tY29sb3JzIn0=" 
data-viewwidgetid=​"0" data-orderby data-order data-orderbyas 
data-orderbysecond data-ordersecond 
data-parametric=​"{"query":​"normal","id":​"1011","view_id":​"1011",
"widget_id":​0,"view_hash":​"1011-CTID20","action":​
"\/​as-positions\/​?​wpv_view_count=1011-CTID20","sort":​{"orderby":​"",
"order":​"","orderby_as":​"","orderby_second":​"","order_second":​""}​,"orderby":​"",
"order":​"","orderby_as":​"","orderby_second":​"","order_second":​"","ajax_form":​"",
"ajax_results":​"","effect":​"fade","prebefore":​"","before":​"","after":​"","attributes":​[]​,
"environment":​{"current_post_id":​0,"parent_post_id":​0,"parent_term_id":​20,
"parent_user_id":​0,"archive":​{"type":​"","name":​"","data":​[]​}​}​,"loop":​{"type":​"",
"name":​"","data":​[]​,"id":​0}​}​" data-attributes=​"[]​" 
data-environment=​"{"current_post_id":​0,"parent_post_id":​0,"parent_term_id":​20,
"parent_user_id":​0,"archive":​{"type":​"","name":​"","data":​[]​}​}​" >​

I even simplified my view too:

[wpv-layout-start]
	[wpv-items-found]
greenbutton
<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]redbutton[/wpv-no-items-found]
[wpv-layout-end]
#521192

Okay a couple more things.
- Use output="raw" on your View:

<span class="[wpv-view name='position-colors' output='raw']">....

Also, I realized that the filter code doesn't account for the "no items found" case, so I've made a modification to handle that:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '14' ) { //Please adjust to your Views ID
        $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;
}

Then I updated the View like so:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>redbutton</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]greenbutton[/wpv-no-items-found][wpv-layout-end]

Please give this a shot and let me know how it works for you.

#522250

Almost there.

"redbutton" or anything else, can't be within the loop. Otherwise it'll return one time for each position that it finds. I just need it once, otherwise the CSS won't work.

#522319

In that case you can adjust your shortcode to move "redbutton" outside of the <wpv-loop> (just before the opening loop tag will be fine):

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start -->redbutton<wpv-loop></wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]greenbutton[/wpv-no-items-found][wpv-layout-end]

The filter will still work as intended.

#522325

I must be missing something. No matter how I shift things around, almost all of the listings come up one color (while the first entry is blank, mysteriously).

Since it's a view within a view, I used the View ID of position-color View. This results in all the same colors.
When I use the View ID of the overall View (the one displaying post types and their information, it returns the correct red vs green text, but only as the code along with all the views junk surrounding it.
When I try both Views, it doesn't help.

Any clues?

#522550

It might be best for me to take a look under the hood to understand your setup. May I request access to your wp-admin area? Private reply fields are enabled here.

#522709
recursive.png

Hi, I'm looking but it's difficult to see what's going on because it looks like your Loop Output is recursing inside itself. Take a look at the console inspector screenshot here - each "redbutton" element is nested inside the previous one. I tried a couple of changes but wasn't able to figure this out within a few minutes. Can you resolve this nesting issue so I can continue debugging?

#522746

The span was interfering with the Divi toggling. I've fixed the recursive bit and changed from colors to mere text.

Everything works except for when the result is empty.

Look at View: Simple Position Count for what I'm dealing with.

#523077

Okay thanks. I have been investigating but I don't see anything obviously wrong, and the exact same conditionals work on my own local site. I'm going to make a clone using the already installed Duplicator plugin so I can run some additional tests locally, and try disabling plugins and themes to isolate the problem. Please standby and I will update you when I have some more information.

#523091
Screen Shot 2017-05-11 at 10.10.43 AM.png

Ah, I didn't notice before but it looks like the filter controls in your View were breaking the logic used for the raw text output. I have removed "[wpv-filter-meta-html]" from the Filter and Loop Output Integration Editor section of your AS Positions View. Let me know if I can be of further assistance.

#523158

Ah, great. Many thanks.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.