Zli
Zli

Reputation: 127

If else statement in WordPress theme

I use WooCommerce and I have one button on my index page, where I want to show different message for members and guests.

I have a lottery system and only registered users can use it. I want to put text as follows:

My code:

if ( ! empty( $link ) && ! empty( $label ) ) {
    echo '<a href="' . esc_url( $link ) . '" class="section-scroll btn btn-border-w btn-round">' . wp_kses_post( $label ) . '</a>';
}

The code for my funtcion would be:

if ( is_user_logged_in() ) {
    echo 'Lottery';
} else {
    echo 'Register';
}

Now how could I put this code to be variable and put it instead of $label variable. Is it possible with if else statement?

Upvotes: 1

Views: 84

Answers (1)

Marleen
Marleen

Reputation: 2724

Specify a separate $label and $link based on whether user is logged in or not:

if ( is_user_logged_in() ) {
    $label = 'Lottery';
    $link = 'lotteryLink';
} else {
    $label = 'Register';
    $link = 'registerLink';
}

And after that use the existing code to display them:

if ( ! empty( $link ) && ! empty( $label ) ) {
echo '<a href="' . esc_url( $link ) . '" class="section-scroll btn btn-border-w btn-round">' . wp_kses_post( $label ) . '</a>';
}

Upvotes: 1

Related Questions