下面是一个用于展示2个横幅的功能shortcodes + $atts . 两个横幅中的每一个都必须有自己的短代码来显示它。这样我就可以把它们放在页面上我想要的地方。
结果我需要:[show_homepage_banner banner_id=1] + [show_homepage_banner banner_id=2]
使用下面的GentlemanMax代码,我没有得到任何结果。但表中有3条记录与查询匹配。查询结果如下:
Array ( [banner_id] => 2 ) SELECT * FROM wp_rdp_banners WHERE banner_type = "1" AND end_date >= CURDATE() AND banner_id=%d LIMIT 2Array ( [banner_id] => 1 ) SELECT * FROM wp_rdp_banners WHERE banner_type = "1" AND end_date >= CURDATE() AND banner_id=%d LIMIT 2Array ( [banner_id] => 2 ) SELECT * FROM wp_rdp_banners WHERE banner_type = "1" AND end_date >= CURDATE() AND banner_id=%d LIMIT 2
这里是print\\u r的结果($atts)
Array ( [banner_id] => 2 ) Array ( [banner_id] => 1 ) Array ( [banner_id] => 2 )
下面是表格中的横幅:
banner_id banner_type picture end_date
61 1 b1.png 22-8-2016
43 1 cblue.png 16-8-2016
60 1 b3.png 16-8-2016
我必须修复什么?
function show_homepage_banner_function($atts) {
$atts = shortcode_atts( array(\'banner_id\' => NULL), $atts );
$content = \'\';
global $wpdb;
$upload_url = \'\';
$upload_dir = wp_upload_dir();
$target_dir = $upload_dir[\'basedir\'];
$upload_url = $upload_dir[\'baseurl\'].\'/banners/\';
$query = \'SELECT * FROM wp_rdp_banners WHERE banner_type = "1" AND end_date >= CURDATE()";
$query .= isset($atts[\'banner_id\']) ? \' AND banner_id=%d\' : \' ORDER BY RAND()\';
$query .= \' LIMIT 2\';
$ban= $wpdb->get_row($wpdb->prepare($query, $atts[\'banner_id\']));
if(count($result)) {
$content = \'<div class="rdp-banner-1"><a href="\'.$ban->banner_link.\'"><img src="\'.$upload_url.$ban->picture.\'" ></a></div>\';
}
return $content;
}
add_shortcode(\'show_homepage_banner\', \'show_homepage_banner_function\');
最合适的回答,由SO网友:TheGentleman 整理而成
上面的函数根本不使用id att。这可能是你想要的,但我不能百分之百确定你想要什么。
function show_homepage_banner_function($atts) {
$atts = shortcode_atts( array(\'id\' => NULL), $atts );
$content = \'\';
global $wpdb;
$upload_url = \'\';
$upload_dir = wp_upload_dir();
$target_dir = $upload_dir[\'basedir\'];
$upload_url = $upload_dir[\'baseurl\'].\'/banners/\';
$query = "SELECT * FROM {$wpdb->prefix}banners WHERE banner_type = "1" AND end_date >= CURDATE()";
$query .= isset($atts[\'id\']) ? \' AND id=%d\' : \' ORDER BY RAND()\';
$query .= \' LIMIT 1\';
$ban= $wpdb->get_row($wpdb->prepare($query, $atts[\'id\']));
if(count($result)) {
$content = \'<div class="rdp-banner-1"><a href="\'.$ban->banner_link.\'"><img src="\'.$upload_url.$ban->picture.\'" ></a></div>\';
}
return $content;
}
请注意,我将此设置为在未指定id的情况下拾取随机横幅。我还假设数据库中的id列实际上是“id”。