Saturday, June 25, 2022
HomeWordPress Developmentsuperior customized fields - How can I cover the ACF shortcode when...

superior customized fields – How can I cover the ACF shortcode when empty


I’m going through a difficulty. I’m utilizing a snippet from github. It renders the URL with some attributes. Nevertheless, when empty, the brief code remains to be displaying and isn’t being hidden. Right here it’s:

/*****************************************************************************************************************************************************
 *
 *  Utilization: [acf_href href_before="mailto:" href="acf:field_name" text="acf:field_name"]
 *
 *    acf:fieldname will retrieve the worth of the required "fieldname" and use that.
 *    get:url_variable will seize a variable from the URL and use that.
 *
 *      [acf_href href="acf:my_link" text="Link to web site"]
 *      [acf_href href_before="mailto:" href="acf:email_address" text="acf:user_name" post_id="get:post_id"]
 *
 *****************************************************************************************************************************************************/

/*---------------------------------------------------------------------------------------------------------------------------------------------------*/

perform acf_field_from_string( $string, $post_id ) {
    if ( substr( $string, 0, 4 ) == 'acf:' ) {      // ACF discipline identify
        $string = get_field( substr( $string, 4 ), $post_id );
    } elseif ( substr( $string, 0, 4 ) == 'get:' ) {
        $string = $_GET[substr( $string, 4 ) ];     // $_GET variable
    }
    return $string;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------*/

add_shortcode( 'acf_href', 'tscpl_sc_acf_href' );
perform tscpl_sc_acf_href( $atts ) {
    extract( shortcode_atts( array(
        'href_before' => '',
        'href' => null,
        'href_after' => '',
        'textual content' => null,
        'title' => null,
        'alt' => null,
        'goal' => '_blank',
        'rel' => 'nofollow',
        'post_id' => null,
        'post_type' => 'submit',                      // default to POST if not specified
        ), $atts ));

    if ( empty( $post_id ) ) {                      // contained in the loop
        $post_id = get_the_id();
    } elseif ( is_numeric( $post_id ) == false ) {  // slug
        $post_id = acf_field_from_string( $post_id );
        if ( empty( $post_type ) )
            $post_type = get_post_type();
        $submit = get_page_by_path( $post_id, OBJECT, $post_type );
        $post_id = $post->ID;
    } else {
        $post_id = (int) $post_id;
    }

    if ( empty( $href ) )
        return;

    $href           = acf_field_from_string( $href, $post_id );
    $href_before    = acf_field_from_string( $href_before, $post_id );
    $href_after     = acf_field_from_string( $href_after, $post_id );
    $textual content           = acf_field_from_string( $textual content, $post_id );
    $title          = acf_field_from_string( $title, $post_id );
    $alt            = acf_field_from_string( $alt, $post_id );

    return '<a href="' . $href_before . $href . $href_after . '" title="' . $title . '" alt="' . $alt . '" goal="' . $goal . '" rel="' . $rel . '">' . $textual content . '</a>';

}

Now, when the ACF discipline is empty it is rendering like this.

Learn the entire story right here.

Kindly information what I’m lacking right here.

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments