Button link display in shortcodes using custom field in ACF

I used the ACF plugin to display custom post type( history ) there I add a custom field for button and button link. Now

I used the ACF plugin to display custom post type( history ) there I add a custom field for button and button link. Now I create a shortcode of history post type but I don't know how I can add button link that I used in custom fied please solve this here my code :

add_shortcode( 'history_shortcodes', 'history_shortcodes_post_type' );
    function history_shortcodes_post_type(){
        $args = array(
            'post_type' => 'history',
            'post_status' => 'publish'
        );

        $string = '';
        $h_query = new WP_Query( $args );
        if( $h_query->have_posts() ){
            $string .= '<div class="main_history">';
            while( $h_query->have_posts() ){
                $h_query->the_post();

                $h_button_field = get_field('history_button_title'); // used to display the custom field 
                if( !empty($h_button_field) ): endif;
                $h_link = get_field('history_button_url'); // used to display the custom field 
                if( $h_link ):  endif;

                $string .= '<div class="col-sm-6">' . '<div class="single_history_img">' . get_the_post_thumbnail() . '</div>' . '</div>' . '<div class="col-sm-6">' . '<div class="single_history_content">' . '<div class="head_title">' . '<h2>' . get_the_title() . '</h2>' . '</div>' . '<p>' . get_the_content() . '</p>' . '<a href="" class="btn btn-lg">' . $h_button_field . '</a>' . '</div>' . '</div>';

            }
            $string .= '</div>';
        }
        wp_reset_postdata();
        return $string;
    }

I used the ACF plugin to display custom post type( history ) there I add a custom field for button and button link. Now I create a shortcode of history post type but I don't know how I can add button link that I used in custom fied please solve this here my code :

add_shortcode( 'history_shortcodes', 'history_shortcodes_post_type' );
    function history_shortcodes_post_type(){
        $args = array(
            'post_type' => 'history',
            'post_status' => 'publish'
        );

        $string = '';
        $h_query = new WP_Query( $args );
        if( $h_query->have_posts() ){
            $string .= '<div class="main_history">';
            while( $h_query->have_posts() ){
                $h_query->the_post();

                $h_button_field = get_field('history_button_title'); // used to display the custom field 
                if( !empty($h_button_field) ): endif;
                $h_link = get_field('history_button_url'); // used to display the custom field 
                if( $h_link ):  endif;

                $string .= '<div class="col-sm-6">' . '<div class="single_history_img">' . get_the_post_thumbnail() . '</div>' . '</div>' . '<div class="col-sm-6">' . '<div class="single_history_content">' . '<div class="head_title">' . '<h2>' . get_the_title() . '</h2>' . '</div>' . '<p>' . get_the_content() . '</p>' . '<a href="" class="btn btn-lg">' . $h_button_field . '</a>' . '</div>' . '</div>';

            }
            $string .= '</div>';
        }
        wp_reset_postdata();
        return $string;
    }
Share Improve this question edited Aug 27, 2019 at 9:06 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Aug 27, 2019 at 7:22 rubyruby 35 bronze badges 2
  • I don't know why can't you get $h_link in your anchor tag.. Can you please first vardump the value of $h_button_field and $h_link – sagar Commented Aug 27, 2019 at 7:47
  • I try to add this in anchor tag '<a href="$h_link" class="btn btn-lg">' but it not working, i dont know the right way to add this – ruby Commented Aug 27, 2019 at 8:14
Add a comment  | 

1 Answer 1

Reset to default 0

Try this I have recorrected your code, if you have successfully retrieved those two variables

 $string = '';
$h_query = new WP_Query( $args );
if( $h_query->have_posts() ){
    $string .= '<div class="main_history">';
    while( $h_query->have_posts() ){
        $h_query->the_post();

        $h_button_field = get_field('history_button_title'); // used to display the custom field 
        if( !empty($h_button_field) ): endif;
        $h_link = get_field('history_button_url'); // used to display the custom field 
        if( $h_link ):  endif;

        $string .= '<div class="col-sm-6">' . '<div class="single_history_img">' . get_the_post_thumbnail() . '</div>' . '</div>' . '<div class="col-sm-6">' . '<div class="single_history_content">' . '<div class="head_title">' . '<h2>' . get_the_title() . '</h2>' . '</div>' . '<p>' . get_the_content() . '</p>' . '<a href="'.$h_link.'" class="btn btn-lg">' . $h_button_field . '</a>' . '</div>' . '</div>';

    }
    $string .= '</div>';
}
wp_reset_postdata();
return $string;

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208923a4616726.html

相关推荐

  • Button link display in shortcodes using custom field in ACF

    I used the ACF plugin to display custom post type( history ) there I add a custom field for button and button link. Now

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信