• Resolved smittyhead

    (@smittyhead)


    I’m trying to add a switch that tells if a text field is a link or not then using the code in the block like below. It works fine except it outputs plain text of the two fields as well as the html in the echo. I’m not sure what i’m doing wrong?

    
    <div><?php block_field( 'number' ) ?></div>
    <?php
    if ( block_value( 'title-link' ) ) {
    	echo "<a href='#'>" . block_field( 'title' ) . "</a>";
    }else {
        echo "<div>" . block_field( 'title' ) . "</div>" ;
    }
    ?> 
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Luke Carbis

    (@lukecarbis)

    @smittyhead The issue is because you’re using echo as well as block_field(), which also echoes. You could reformat your template like this:

    
    <?php
    if ( ! empty( block_value( 'title-link' ) ) ) {
    	echo '<a href="' . block_value( 'title-link' ) . '">' . block_value( 'title' ) . '</a>';
    } else {
    	echo '<div>' . block_value( 'title' ) . '</div>';
    }
    ?>
    

    Or, alternatively…

    
    <?php if ( ! empty( block_value( 'title-link' ) ) ) : ?>
    	<a href="<?php block_field( 'title-link' ); ?>"><?php block_field( 'title' ); ?></a>
    <?php else : ?>
    	<div><?php block_field( 'title' ); ?></div>
    <?php endif; ?>
    
    Thread Starter smittyhead

    (@smittyhead)

    Thank you so much! Sorry for such a noob question.

    Plugin Author Luke Carbis

    (@lukecarbis)

    Not a problem at all!

    Thread Starter smittyhead

    (@smittyhead)

    Hi, sorry, but having this same issue in trying to solve another problem for a different post but since was relevant to this issue thought it would be better to move to this post.

    Instead of an if statement i’m setting up variables but I cannot get ’empty’ to work with that, i’m getting the output for the block_value, this is the setup:

    
    <?php
    $image_url = block_field( 'image-1' );
    $image_id = pippin_get_image_id($image_url);
    $image_med = wp_get_attachment_image_src($image_id, 'medium_large');
    ?>
    
    • This reply was modified 7 years ago by smittyhead.
    Plugin Author Luke Carbis

    (@lukecarbis)

    @smittyhead Same problem again:
    $image_url = block_value( 'image-1' );

    Thread Starter smittyhead

    (@smittyhead)

    Damn, you know, i glossed over that part! Thanks for your patience.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Toggle issue’ is closed to new replies.