• Resolved markburton52

    (@markburton52)


    I’m trying to format the single location page format so that if I drop a website address into the attribute box in the event post editor, it shows as a hyperlink and if none is specified, it shows ‘no website’. I can’t figure out how to do that. I can do this:

    <a href="#_LATT{website}">website</a>

    But if there is no website given, it will still say “website” and link back to the current post. How do I specifiy the null alternative as an option.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • You’ll need to create conditional placeholders for $_LATT{website} by adding the following code snippet:

    add_filter('em_location_output_show_condition', function($show_condition, $condition, $key, $location) {
    if ($condition == "has_latt_website" && !empty($location->location_attributes['website'])) {
    return true;
    }
    else if ($condition == "no_has_latt_website" && empty($location->location_attributes['website'])) {
    return true;
    }
    return $show_condition;
    }, 10, 4);

    Then you can use this:

    {has_latt_website}
    <a href="#_LATT{website}">website</a>
    {/has_latt_website}
    {no_has_latt_website}
    no website
    {/no_has_latt_website}
    Thread Starter markburton52

    (@markburton52)

    Thank you, that’s perfect!

    Thread Starter markburton52

    (@markburton52)

    @joneiseman I’ve tried to clone and edit your code to do similar with a link in the event posts:

    add_filter('em_event_output_show_condition', function($show_condition, $condition, $key, $EM_Event) {
    if ($condition == "has_link_to_book" && !empty($event->event_attributes['link to book'])) {
    return true;
    }
    else if ($condition == "no_has_link_to_book" && empty($event->event_attributes['link to book'])) {
    return true;
    }
    return $show_condition;
    }, 10, 4);

    and in the single event page template

    {has_att_link to book}
    <a href="#_ATT{link to book}">link to book</a>
    {/has_att_link to book}
    {no_has_att_link to book}
    no booking needed
    {/no_has_att_link to book}

    But the link isn’t coming through to the front end. Can you spot an error in the code?

    (We aren’t using the inbuilt EM booking functionality but rather links to external sites for that.)

    Thanks

    This was my mistake. The function uses $EM_Event as the name of the argument but the code is using $event. Change the argument declaration to $event instead of $EM_Event. Here’s the corrected version:

    add_filter('em_event_output_show_condition', function($show_condition, $condition, $key, $event) {
    if ($condition == "has_link_to_book" && !empty($event->event_attributes['link to book'])) {
    return true;
    }
    else if ($condition == "no_has_link_to_book" && empty($event->event_attributes['link to book'])) {
    return true;
    }
    return $show_condition;
    }, 10, 4);
    Thread Starter markburton52

    (@markburton52)

    Thanks – not your mistake but mine, but I’m getting this output on the front page.

    {has_att_link to book} link to book {/has_att_link to book} {no_has_att_link to book} no booking needed {/no_has_att_link to book}

    The template formatting is identical to that used for the previous location one (with the obvious changes).

    Change this in your formatting:

    {has_att_link to book} link to book {/has_att_link to book} {no_has_att_link to book} no booking needed {/no_has_att_link to book}

    To this:

    {has_link_to_book} link to book {/has_link_to_book} {no_has_link_to_bookbook} no booking needed {/no_has_link_to_book}
    Thread Starter markburton52

    (@markburton52)

    Sorry no that didn’t work. Here is the working LATT one and the non-working ATT one. Removing the att_ as suggested made no diffrence in the ATT version – is something still wrong in the php snippet?

    {has_att_link to book}
    <a href="#_ATT{link to book}">link to book</a>
    {/has_att_link to book}
    {no_has_att_link to book}
    no booking needed
    {/no_has_att_link to book}

    {has_latt_website}
    <a href="#_LATT{website}">website</a>
    {/has_latt_website}
    {no_has_latt_website}
    no website
    {/no_has_latt_website}

    You actually don’t need any code snippet for this. Here’s the documentation:

    https://wp-events-plugin.com/documentation/conditional-placeholders/#general

    has_att_XEvent?has?a specific custom field named?X. Spaces should be replaced with?_?underscores, for example?{has_att_More_Information}?for?More Information.

    So for your example:

    {has_att_link_to_book}
    <a href="#_ATT{link to book}">link to book</a>
    {/has_att_link_to_book}
    {no_has_att_link_to_book}
    no booking needed
    {/no_has_att_link_to_book}
    Thread Starter markburton52

    (@markburton52)

    Thanks – very helpful. I’ve got it working now. Note that there should be no ‘-has’ in the expression for the null case:

    {has_att_link_to_book}
    <a href="#_ATT{link to book}">link to book</a>
    {/has_att_link_to_book}
    {no_att_link_to_book}
    no booking needed
    {/no_att_link_to_book}
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.