在ACF中使用自定义域以快捷代码显示按钮链接

时间:2019-08-27 作者:ruby

我使用ACF插件显示自定义帖子类型(历史记录),在那里我为按钮和按钮链接添加了一个自定义字段。现在,我创建了一个历史帖子类型的快捷码,但我不知道如何添加自定义FID中使用的按钮链接请在此处解决我的代码:

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;
    }

1 个回复
最合适的回答,由SO网友:sagar 整理而成

如果您成功检索到这两个变量,请尝试此操作我已重新更正了您的代码

 $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;
}

相关推荐

Shortcode to show the code

如何创建一个短代码(或类似的代码)来在页面的某个地方显示页脚?我试过了,但没有结果。。。function show_footer() { return get_footer(); } add_shortcode( \'show_f\', \'show_footer\' ); 或者这个。。。function show_footer() { get_footer(); } add_shortcode( \'show_f\', \'sho