-1

Here below a function for the presentation of 2 banners with shortcodes + $atts . Each of the two banners must get there own shortcode to show it. So i can place them where i want on the page.

Result I need: [show_homepage_banner banner_id=1] + [show_homepage_banner banner_id=2]

With the below code of GentlemanMax I get no results. But there are 3 records in table that match the query. Here the query result:

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

Here the result of print_r($atts)

Array ( [banner_id] => 2 ) Array ( [banner_id] => 1 ) Array ( [banner_id] => 2 )

Here the banners in the table:

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

What do I have to fix?

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');
  • Both of your functions have the same name? – TheGentleman Aug 12 '16 at 15:54
  • changed but no result as needed – Hermants Aug 12 '16 at 16:34
  • 1
    Can you update the above as well? Are you passing the atts from one function to the other? – TheGentleman Aug 12 '16 at 16:42
  • I have no experience with this function. Not mines. But need to split the results of the query into 2 different [shortcodes] so I can place the images at different positions on the site. If you can help me fixing the function(s) would be great. – Hermants Aug 12 '16 at 16:45
0

The functions you have above don't use the id att at all. This might be what you want, but I'm not 100% sure what you're going for.

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

Note, I set this up to pick a random banner if no id is specified. I also assumed that the id column in the database is literally "id".

  • Hi, thanks for your suggestions. I added it in my question be course I did not get any banner with [show_homepage_banner banner_id=1] + [show_homepage_banner banner_id=2]. I have included the print_r() to show what is going on. Maybe you see the problem. – Hermants Aug 13 '16 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.