为什么此快捷代码函数以错误的顺序返回数据?
我一直在尝试创建一个插件,该插件需要一些ACF选项页面字段,并创建一个快捷代码,该快速代码输出附加到短代码中属性的广告代码,
例如。 [awin id =“ top_banner”]理想情况下将返回该ID输入的代码,
而[awin id =“ midder_banner”]或[awin id =“ bottom_banner”]将分别返回每个横幅的代码页面。
我还添加了通过ACF布尔拨动开关显示和隐藏广告的功能。
我遇到的问题是,如果隐藏广告或短代码不在页面中,它将输出此ID的返回,但是在短码的下一个实例中(不论ID)
示例示例< /strong>
说“ midder_banner”是隐藏的(或者在页面上没有出现的短代码)时,当您向下滚动到bottom_banner的输出时,您会看到“ middle_banner”的输出。当您实际期望看到bottom_banner的输出时。
更新:奇怪的是,ID始终是正确的(我将其作为返回范围内的DIV输出),这始终显示从短代码中显示正确的ID。因此,问题是返回的广告代码。
下面是 问题是否有助于上下文。
// THIS LOOPS THROUGH ADS AND CREATES SHORTCODE ID's within attributes / and assigned ad code to return
// WP Shortcode to display text on page or post.
// [awin id="top_banner"]
function awin_func( $atts, $content = null, $tag = '' ) {
if( have_rows('awin_ads', 'option') ):
while( have_rows('awin_ads', 'option') ): the_row();
$embed_code = get_sub_field('awin_ad_embed_code');
$short_code = get_sub_field('shortcode');
// $short_code = preg_replace('/\s+/', '_', $short_code); /replaces spaces with underscores from input
$is_live = get_sub_field('is_showing');
$atts = shortcode_atts( array(
'id' => $short_code,
'embedded_code' => $embed_code,
), $atts, $tag );
extract($atts);
$output = '';
if($is_live == true) {
$output .= '<div id="' . $id . '" style="color:#a0a0a0; text-align:center;"> <hr><p style="margin:0;">Advert</p><br>' . $embedded_code . '<hr></div>';
}
return $output;
endwhile;
endif;
}
// this makes it init the function after wp has fully loaded
function shortcodes_init(){
add_shortcode( 'awin', 'awin_func' );
}
add_action('init', 'shortcodes_init');
?>
但是,有趣的是,当您检查前端的代码时,ID属性始终将通过正确地传递到输出内的DIV ID。这就是真正使我感到困惑的原因!
以下所有帮助将受到最大的赞赏
完整的插件代码:
<?php
/*
Plugin Name: Keep Digital Awin Shortcode Generator
Plugin URI: https://bitbucket.org/keep-digital/awin-plugin
description: This creates shortcodes for awin ads so you can use them around the site
Version: 1.4
Author: Dan Eland
Author URI: https://keepdigital.co.uk
License: GPL2
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// THIS CHECKS ACF IS INSTALLED OR USES ITS OWN LOCAL VERSION
// Optionally you can use below to check by plugin location:
// if((is_plugin_active('advanced-custom-fields/acf.php'))|| (is_plugin_active('advanced-custom-fields-pro/acf.php'))):
// Check if ACF Classes exists:
if(!class_exists('acf_pro') || !class_exists('acf')):
// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', plugin_dir_path( __FILE__ ) . 'acf/' );
define( 'MY_ACF_URL', plugin_dir_url( __FILE__ ) . 'acf/' );
// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );
// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
return MY_ACF_URL;
}
// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
return false;
}
endif;
//THIS ADDS A PLUGIN OPTIONS PAGE
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'AWIN Ads Shortcode Generator',
'menu_title' => 'AWIN Ads Shortcode',
'menu_slug' => 'awin-ads-generator',
'capability' => 'edit_posts',
'redirect' => true
));
}
// adds custom fields to options page
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_625809450c759',
'title' => 'AWIN Shortcode Generator',
'fields' => array(
array(
'key' => 'field_625809567e7ec',
'label' => 'AWIN ads',
'name' => 'awin_ads',
'type' => 'repeater',
'instructions' => 'Create your AWIN ad \'areas\' here. You can create as many ad \'areas\' as you require throughout the site. When you want to embed your ad \'area\' in the site, simply place your shortcode anywhere on your site within a \'text\' container, and drop the AWIN embed code into the embed code field.
You can update the ad code within any \'area\' by simply pasting new AWIN ad code into the embed field for that \'area\'
You can also hide the ad \'areas\' if you\'re between ads by using the show/hide toggle on the ad \'area\'',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_625809827e7ed',
'label' => 'AWIN Ad Embed Code',
'name' => 'awin_ad_embed_code',
'type' => 'textarea',
'instructions' => 'please paste in the Ad code from AWIN directly in here',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '55',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'new_lines' => '',
),
array(
'key' => 'field_625809c77e7ee',
'label' => 'Ad Area Name',
'name' => 'shortcode',
'type' => 'text',
'instructions' => 'What you\'d like to call your AWIN Ad Area e.g. calling it top_banner would result in creating the following shortcode: [awin id="top_banner"]',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '30',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => 'e.g. top_banner',
'prepend' => '[awin id="',
'append' => '"]',
'maxlength' => '',
),
array(
'key' => 'field_625819ba74b72',
'label' => 'Show / Hide Ad Area',
'name' => 'is_showing',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '15',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => 'Ad Live',
'ui_off_text' => 'Ad Hidden',
),
),
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'awin-ads-generator',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
endif;
// THIS LOOPS THROUGH ADS AND CREATES SHORTCODE ID's within attributes / and assigned ad code to return
// WP Shortcode to display text on page or post.
// [awin id="top_banner"]
function awin_func( $atts, $content = null, $tag = '' ) {
if( have_rows('awin_ads', 'option') ):
while( have_rows('awin_ads', 'option') ): the_row();
$embed_code = get_sub_field('awin_ad_embed_code');
$short_code = get_sub_field('shortcode');
// $short_code = preg_replace('/\s+/', '_', $short_code); /replaces spaces with underscores from input
$is_live = get_sub_field('is_showing');
$atts = shortcode_atts( array(
'id' => $short_code,
'embedded_code' => $embed_code,
), $atts, $tag );
extract($atts);
$output = '';
if($is_live == true) {
$output .= '<div id="' . $id . '" style="color:#a0a0a0; text-align:center;"> <hr><p style="margin:0;">Advert</p><br>' . $embedded_code . '<hr></div>';
}
return $output;
endwhile;
endif;
}
// this makes it init the function after wp has fully loaded
function shortcodes_init(){
add_shortcode( 'awin', 'awin_func' );
}
add_action('init', 'shortcodes_init');
?>
I've been attempting to create a plugin that takes some ACF options page fields, and creates a shortcode which outputs an advertising code attached to an attribute within the shortcode
e.g. [awin id="top_banner"] would ideally return the code entered for that ID
whereas [awin id="middle_banner"] or [awin id="bottom_banner"] would return the code for each banner respectively, wherever the shortcode was found on the page.
I also added the ability to show and hide ads via an ACF boolean toggle switch.
The problem I'm having is that if an ad is hidden or the shortcode isn't within the page, it's outputting the return of this ID, but in the next instance of the shortcode (regardless of the ID)
Example
Say the "middle_banner" was hidden (or that shortcode didn't appear on the page) when you scroll down to the output for bottom_banner, you see the output for "middle_banner" instead. When you'd actually expect to see the output for bottom_banner.
Update: Oddly enough, the ID's are always correct (I've outputted this as the ID of the DIV within the return) and this always shows the correct ID from the shortcode. So it's just the returned ad code that is the issue.
My code that handles the creation of the shortcodes is below - I hope I've explained it clearly enough, I've also included the full plugin code at the end of the question if it helps for context.
// THIS LOOPS THROUGH ADS AND CREATES SHORTCODE ID's within attributes / and assigned ad code to return
// WP Shortcode to display text on page or post.
// [awin id="top_banner"]
function awin_func( $atts, $content = null, $tag = '' ) {
if( have_rows('awin_ads', 'option') ):
while( have_rows('awin_ads', 'option') ): the_row();
$embed_code = get_sub_field('awin_ad_embed_code');
$short_code = get_sub_field('shortcode');
// $short_code = preg_replace('/\s+/', '_', $short_code); /replaces spaces with underscores from input
$is_live = get_sub_field('is_showing');
$atts = shortcode_atts( array(
'id' => $short_code,
'embedded_code' => $embed_code,
), $atts, $tag );
extract($atts);
$output = '';
if($is_live == true) {
$output .= '<div id="' . $id . '" style="color:#a0a0a0; text-align:center;"> <hr><p style="margin:0;">Advert</p><br>' . $embedded_code . '<hr></div>';
}
return $output;
endwhile;
endif;
}
// this makes it init the function after wp has fully loaded
function shortcodes_init(){
add_shortcode( 'awin', 'awin_func' );
}
add_action('init', 'shortcodes_init');
?>
Funnily enough, however, when you inspect the code on the front end, the id attribute is always been passed through to the div id within the output correctly. This is what's really confusing me!
Any help would be most appreciated
Full Plugin code below:
<?php
/*
Plugin Name: Keep Digital Awin Shortcode Generator
Plugin URI: https://bitbucket.org/keep-digital/awin-plugin
description: This creates shortcodes for awin ads so you can use them around the site
Version: 1.4
Author: Dan Eland
Author URI: https://keepdigital.co.uk
License: GPL2
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// THIS CHECKS ACF IS INSTALLED OR USES ITS OWN LOCAL VERSION
// Optionally you can use below to check by plugin location:
// if((is_plugin_active('advanced-custom-fields/acf.php'))|| (is_plugin_active('advanced-custom-fields-pro/acf.php'))):
// Check if ACF Classes exists:
if(!class_exists('acf_pro') || !class_exists('acf')):
// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', plugin_dir_path( __FILE__ ) . 'acf/' );
define( 'MY_ACF_URL', plugin_dir_url( __FILE__ ) . 'acf/' );
// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );
// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
return MY_ACF_URL;
}
// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
return false;
}
endif;
//THIS ADDS A PLUGIN OPTIONS PAGE
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'AWIN Ads Shortcode Generator',
'menu_title' => 'AWIN Ads Shortcode',
'menu_slug' => 'awin-ads-generator',
'capability' => 'edit_posts',
'redirect' => true
));
}
// adds custom fields to options page
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_625809450c759',
'title' => 'AWIN Shortcode Generator',
'fields' => array(
array(
'key' => 'field_625809567e7ec',
'label' => 'AWIN ads',
'name' => 'awin_ads',
'type' => 'repeater',
'instructions' => 'Create your AWIN ad \'areas\' here. You can create as many ad \'areas\' as you require throughout the site. When you want to embed your ad \'area\' in the site, simply place your shortcode anywhere on your site within a \'text\' container, and drop the AWIN embed code into the embed code field.
You can update the ad code within any \'area\' by simply pasting new AWIN ad code into the embed field for that \'area\'
You can also hide the ad \'areas\' if you\'re between ads by using the show/hide toggle on the ad \'area\'',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_625809827e7ed',
'label' => 'AWIN Ad Embed Code',
'name' => 'awin_ad_embed_code',
'type' => 'textarea',
'instructions' => 'please paste in the Ad code from AWIN directly in here',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '55',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'new_lines' => '',
),
array(
'key' => 'field_625809c77e7ee',
'label' => 'Ad Area Name',
'name' => 'shortcode',
'type' => 'text',
'instructions' => 'What you\'d like to call your AWIN Ad Area e.g. calling it top_banner would result in creating the following shortcode: [awin id="top_banner"]',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '30',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => 'e.g. top_banner',
'prepend' => '[awin id="',
'append' => '"]',
'maxlength' => '',
),
array(
'key' => 'field_625819ba74b72',
'label' => 'Show / Hide Ad Area',
'name' => 'is_showing',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '15',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => 'Ad Live',
'ui_off_text' => 'Ad Hidden',
),
),
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'awin-ads-generator',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
));
endif;
// THIS LOOPS THROUGH ADS AND CREATES SHORTCODE ID's within attributes / and assigned ad code to return
// WP Shortcode to display text on page or post.
// [awin id="top_banner"]
function awin_func( $atts, $content = null, $tag = '' ) {
if( have_rows('awin_ads', 'option') ):
while( have_rows('awin_ads', 'option') ): the_row();
$embed_code = get_sub_field('awin_ad_embed_code');
$short_code = get_sub_field('shortcode');
// $short_code = preg_replace('/\s+/', '_', $short_code); /replaces spaces with underscores from input
$is_live = get_sub_field('is_showing');
$atts = shortcode_atts( array(
'id' => $short_code,
'embedded_code' => $embed_code,
), $atts, $tag );
extract($atts);
$output = '';
if($is_live == true) {
$output .= '<div id="' . $id . '" style="color:#a0a0a0; text-align:center;"> <hr><p style="margin:0;">Advert</p><br>' . $embedded_code . '<hr></div>';
}
return $output;
endwhile;
endif;
}
// this makes it init the function after wp has fully loaded
function shortcodes_init(){
add_shortcode( 'awin', 'awin_func' );
}
add_action('init', 'shortcodes_init');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在ACF Pro支持下向Alvin大喊,花时间查看此问题并提供解决方案:
“当未通过 https://codex.wordpress.org/shortcode_api。”
他还以一些逻辑写作,根据输出的一些逻辑, ID,然后在发现数据后添加休息时间以停止代码。
工作!
Shout out to Alvin at ACF Pro support for taking the time to look over this and offer a solution:
He said "attributes are used to set a default value when an attribute is not passed as discussed at https://codex.wordpress.org/Shortcode_API."
He also has written in a bit of logic that changes the output depending on the ID, and then adds a break afterwards to stop the code once the data has been found.
Works a treat!