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.
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.
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]
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.
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.
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.
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?
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.
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?
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.
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.
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.