简体   繁体   中英

Pass arguments from php function issue

I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.

First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two] .

Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two) . But i dont get that.

Please guide me, how can i do this.

Here is my Code That works for shortcode:

<?php
function wp_foo_sliders($atts) {
    global $wpdb;
    $tbl_sliders = $wpdb->prefix . "foo_sliders";

    ob_start();
    extract(shortcode_atts(array(
        'slider' => '',
                    ), $atts));

    $get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
    <div class="foo_main_slider">
    <?php
        foreach ($get_sliders as $get_slider) {
            $slider_id = $get_slider->slider_id;
    ?>
    <div class="foo_slider_img">
    <?php
        $get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where 

slider_id = $slider_id Order By image_order ASC");

        foreach ($get_slider_image as $foo_img) { 
    ?>
        <img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
     <?php
        }
        }
        return ob_get_clean();
}

add_shortcode("foo_slider", "wp_foo_sliders");
?>

I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?> , in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.

Edit : I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?> , only shortcode is working, snippet not work.

What i make mistake please help me.

When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.

Try refactoring your code

if( is_array( $atts ) ) {
    //Called using shortcode so $atts is an array
    extract(shortcode_atts(array(
        'slider' => '',
                    ), $atts));

} else {
    //Function called directly so $atts is a string
    $slider = $atts;
}

So i get my solution by my own.

I make a new function for this:

<?php
function foo_sliders($foo_short_name) {
    global $wpdb;
    $tbl_sliders = $wpdb->prefix . "foo_sliders";

    $get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
    <div class="foo_main_slider">
    <?php
        foreach ($get_sliders as $get_slider) {
            $slider_id = $get_slider->slider_id;
    ?>
    <div class="foo_slider_img">
    <?php
        $get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where 

slider_id = $slider_id Order By image_order ASC");

        foreach ($get_slider_image as $foo_img) { 
    ?>
        <img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
     <?php
        }
        }
    ob_start();
    return $foo_short_name;
    return ob_get_clean();
}

?>

And in theme code:

<?php foo_sliders(short_name) ?>

and its work great

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM