A
A
Alex-Broudy2020-04-22 21:44:05
WordPress
Alex-Broudy, 2020-04-22 21:44:05

How to display the value of one custom ACF field if a value is selected in another field of type "Select" (select)?

Hello!

I use the ACF plugin to work with custom fields.
I created fields for displaying links in the page template and placed them in variables:

$gotlink_1 = get_field('mylink_1',$block_id);
$gotlink_2 = get_field('mylink_2',$block_id);
$gotlink_3 = get_field('mylink_3',$block_id);
$gotlink_4 = get_field('mylink_4',$block_id);
$gotlink_5 = get_field('mylink_5',$block_id);


also created an arbitrary field of the "Select (select)" type with the output of the value and placed it in the variable $selected_link

$selected_link = get_field('select_link',$block_id); // поле выбор (select)


and wrote down the values ​​there:

Link 1
Link 2
Link 3
Link 4
Link 5

- - -

and here is the $main_link variable for which you need to create a condition and check, namely:

if the $selected_link field is not empty and has the value Link 1, then in the variable $main_link we display the value of the $gotlink_1 variable

similarly for the others:

if the $selected_link field is not empty and has the value Link 2, then in the $main_link variable we display the value of the $gotlink_2 variable
if the $selected_link field is not empty and has the value Link 3, then in the variable $ main_link output the value of the $gotlink_3 variable,
and so on.

In the template itself, I output like this and everything works:

<?php if( $selected_link == 'Ссылка 1' ): ?><? echo $gotlink_1 ?><?php endif; ?>
<?php if( $selected_link == 'Ссылка 2' ): ?><? echo $gotlink_2 ?><?php endif; ?>
<?php if( $selected_link == 'Ссылка 3' ): ?><? echo $gotlink_3 ?><?php endif; ?>
<?php if( $selected_link == 'Ссылка 4' ): ?><? echo $gotlink_4 ?><?php endif; ?>
<?php if( $selected_link == 'Ссылка 5' ): ?><? echo $gotlink_5 ?><?php endif; ?>


But in every place where I need to specify a link, this is too cumbersome

. In simple words, what needs to be implemented?

There are fields with links.
In the template where the $main_link variable is specified, you need to display only one link from the list, the name of which was selected in the select in the admin panel in an arbitrary field $selected_link = get_field('select_link',$block_id);

Of course, I could put the list of links right into the select and display it where I want, but on one page I will need to display 2-3 links regardless of the select, so I got so confused

Where to display the resulting value:
<a href="<?php echo $main_link ?>">Перейти к просмотру</a>


I would appreciate help or advice.
PS: I tried to implement a check and set a condition using ternary operators, but something does not work out at all ..

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question