Visibilety Condition For Query Count

Hi,

I am looking for a way to set the visibility conditions of my query items. What I want is to show the layout for even query items and hide for odd ones. So something like this:

grafik

Does Cwicly have a solution to this problem?

Cheers

Or is this possible with a Function return?

grafik

I haven’t worked with those jet… how would I register that funktion?

add_filter( 'example_filter', '_return_query_id' );
function _return_query_id() {
    return true;
}

I am grateful for any tips :blush:

Hi @T-low,

You don’t need to register a filter for this, simply define the function you want to call:

function _return_query_id() {
    return true;
}

Also, please see this tip to choose the optimal way for you to define your custom code for your specific requirements:

1 Like

Thanks for the help @StrangeTech :+1:

I actually tried a simple function, but it didn’t work because I made the mistake again of thinking that all values are equal… i should have run a debug :face_exhaling:

With the adjustment of my previous conditions, everything worked well.

grafik

Now I am missing a method to pass on the loop ID of the Query.

function is_query_even($loopID)
{
    if ($loopID % 2 == 0)
        return true;
    else
        return false;
}

I only know of get_the_ID() which gives me the post ID.
Is there a function for the query loop ID?

Cheers

If you add a parameter to your function, you can pass the either the loop index or loop position as an argument:

is_query_item_index_even($loopItemIndex}) {
    if ($loopItemIndex % 2 == 0)
        return true;
    else
        return false;
}

Screenshot 2023-09-20 at 09.14.02

Depending on your specific needs, you can either use the index or the position, depending on whether you want the paginated index or the overall position within the sorted query items, as so:

// Uses the index:
is_query_item_index_even({loop-index})

// Uses the position:
is_query_item_index_even({loop-position})

Please note: I haven’t tested this code with the latest Cwicly version.

I tried this before and thought they are a part of the dynamic content and don’t work with the function return… Well they don’t work on my end because they pass on the dynamic content as string → “{loop position}”. :thinking:

There is most likely a way to do this using a workaround, unfortunately, I don’t have time to experiment with it right now. Let me know if you come up with a solution in the meantime.

Haven’t come across a solution yet. I’ve looked at some wordpress and ACF code, but my experimental code didn’t produce any results… probably not surprising given my experience :sweat_smile:
I would be interested to know what is behind the dynamic insert {loop position} code?
Is there anywhere to check out the code? Did I insert the snippet incorrectly?

ACF get_row_index() continuously displays a 1 on each loop?

Would love any link where I can read up a solution :heart:

is_query_item_index_even(what here to pass on the query loop id?)

Would like to play the @Louis Joker card :face_with_peeking_eye:

Well I found some code in the core files ,which would solve whats behind the:

case 'loop-index':
			$repeater_id = '';
			$query_id    = '';
			if ( isset( $block->context['product_index'] ) ) {
				$repeater_id = $block->context['product_index'];
			}
			if ( isset( $block->context['taxterms_index'] ) ) {
				$repeater_id = $block->context['taxterms_index'];
			}
			if ( isset( $block->context['repeater_row'] ) ) {
				$repeater_id = $block->context['repeater_row'];
			}
			if ( isset( $block->context['query_index'] ) ) {
				$query_id = $block->context['query_index'];
			}
			if ( $query_id && ! $repeater_id ) {
				$value = $query_id;
			}
			if ( ! $query_id && $repeater_id ) {
				$value = $repeater_id;
			}
			if ( $query_id && $repeater_id ) {
				$value = '' . $query_id . $repeater_id . '';
			}

			break;

I am also able to display the loop ID with the cwicly code block

Simple:
echo "{loop-index}";

Or via a snippet of the found code:

$query_id = "";
if (isset($block->context["query_index"])) {
    $query_id = $block->context["query_index"];
}

echo "$query_id";

Maybe I really don’t understand the whole concept of sending a variable to my function… I have not been able to send anything except numbers and strings.
I need an eye opener here. :face_exhaling:

Cheers

Based on the Cwicly code and what you have observed, you should be able to change your function to something like the following:

is_query_item_index_even() {
    $loopItemIndex = 1;

    if ($block != null && isset($block->context["query_index"])) {
        $loopItemIndex = $block->context["query_index"];
    }

    return ($loopItemIndex % 2 == 0);
}

Then you can just call is_query_item_index_even() with no parameter from your visibility condition.

Let me know whether that works for you.

1 Like

Hi @StrangeTech,
thank you so much for your answer :heart:

Don’t I also have to pass the $block variable, since my function is_query_item_index_even() is external?

Otherwise the if statement will always return false and in the conclusion $loopItemIndex will return a 1. At least this is what happens on my side.

I am not sure if I wrote it clearly, but the two working parts of the code are in a Cwicly Code Block for testing purposes. Now I am not able to pass the required variables to my external function.

My thought process was that I have my external function:

function is_query_item_index_even($block) {

    $loopItemIndex = "";

    if (isset($block->context["query_index"])) {
        $loopItemIndex = $block->context["query_index"];
    }
    return ($loopItemIndex % 2 == 0);
}

and pass from the page I use the query the $block variable along like this: is_query_item_index_even($block)

An THIS is actually working !!! … for the cwicly block.
grafik

But it is not working for the conditioning or the dynamic inserter:
grafik

There the $block variable get’s send as string instead of an object?
Well as far as I read into the topic … have just a smidge of self tought code experience.

Cheers

Hi,

I don’t think the $block variable is included with Function return.
I’ll bring this up internally to see if it can be added.

Best regards,
Johnny

1 Like

@T-low, I quickly did a couple of minutes of testing, I have something working - it is not exactly clean, but it does what you want.

Here are the steps:

  1. Add a Code block outside of the Query block:
<?php
function set_current_block($block) {
    global $current_block;
    $current_block = $block;
}
function is_query_item_index_even() {
    global $current_block;
    
    $loopItemIndex = 1;

    if (isset($current_block->context["query_index"])) {
        $loopItemIndex = $current_block->context["query_index"];
    }
    return ($loopItemIndex % 2 == 0);
}
?>
  1. Add a Code block as the first element of the Query Template block:
<?php
set_current_block($block);
?>
  1. Wrap your query item content in.a Div block and give it the following visibility condition:

  2. Add the following relative style to your Query Template:

with a single style property - display: none;

1 Like

Hi @StrangeTech,
sorry for replying so late. Thank you for taking the time to share your setup, it’s a great example of what one can do with Cwicly.

I followed your steps and it worked fine in the backend. But it wasn’t showing up on the front end.
After doing a little checking, I think the relative style rules are all I’m looking for. The template only shows up when the query item is odd. :+1:

Did your solution allso work on the frontend?

Cheers

Hi @T-low,

It works fine for me on both the frontend and the backend.

Make sure the structure follows this outline where the Div block containing your query item having the visibility condition is under the Code block within the template:

Screenshot 2023-10-02 at 10.53.37

That’s great, if that works for you, perfect. The reason why I added the additional part with the visibility conditions is that prevents those elements from being rendered on the frontend, thus making the page load faster and also preventing the user from seeing them in the page source, which may have been a factor for you.

2 Likes