Wordpress:在两个不同的页面中使用相同的小部件侧边栏,条件在一个页面中

时间:2018-01-09 12:50:45

标签: php wordpress widget sidebar

我有多个小部件,我在页面的侧边栏中显示,我想在另一个页面中显示相同的侧边栏,但只显示第一个和第二个小部件。我知道我可以创建另一个侧边栏,但我想知道是否可以在不同页面的相同侧边栏中使用条件。

我的小部件:

<?php

class Temoignages_Widget extends WP_Widget {

    public function __construct() {

        parent::__construct('temoignageswidget', 'Bloc Temoignage', array('description' => 'Ajout d\'un témoignage a la liste'));
    }

    public function widget($args, $instance) {

        $max = $this->maximum($instance['content']);

        if(isset($_COOKIE['width'])) {

            echo   '
                <div class="col-lg-offset-1 col-lg-5 col-md-offset-1 col-md-5 col-sm-offset-1 col-sm-5 col-xs-offset-1 col-xs-5">
                    <div class="testimony_bloc_sport_home_hs' . $max[2] . '">
                        <p class="pos_absolute tag_testimony_bloc_sport_home_hs fsize30">' . $instance['tags'] . '</p>
                    <img src="/images/photos_temoignages/' . $instance['photoPath'] . '" class="pos_absolute img_testimony_bloc_sport_home_hs" />
                    <p class="pos_absolute content_testimony_bloc_sport_home_hs fsize30">
                        << ' . $max[0] . ' >>
                    </p>
                    ' . $max[1] . '
                    <p class="pos_absolute name_testimony_bloc_sport_home_hs fsize30">
                        <b>' . $instance['author'] . '</b>
                    </p>
                    <p class="pos_absolute function_testimony_bloc_sport_home_hs fsize30">
                        ' . $instance['function'] . '
                    </p>
                </div>
            </div>
            <div class="pos_absolute previous_bloc_testimony_sport_hidden">
                <div class="pos_absolute testimony_bloc_sport_home_hs_hidden">
                    <p class="pos_absolute tag_testimony_bloc_sport_home_hs">' . $instance['tags'] . '</p>
                    <img src="/images/quit_testimony.png" class="pos_absolute img_quit_testimony_bloc_sport_home_hs_hidden" />
                    <img src="/images/photos_temoignages/' . $instance['photoPath'] . '" class="pos_absolute img_testimony_bloc_sport_home_hs_hidden" />
                    <p class="pos_absolute content_testimony_bloc_sport_home_hs fsize30">
                        << ' . ($instance['content']) . ' >>
                    </p>
                    <p class="pos_absolute name_testimony_bloc_sport_home_hs fsize30">
                        <b>' . $instance['author'] . '</b>
                    </p>
                    <p class="pos_absolute function_testimony_bloc_sport_home_hs fsize30">
                        ' . $instance['function'] . '
                    </p>
                </div>
            </div>
        ';
    }
    else {

        echo   '
            <div class="col-lg-offset-1 col-lg-3 col-md-offset-1 col-md-3 col-sm-offset-1 col-sm-3 col-xs-offset-1 col-xs-3">
                <div class="testimony_bloc_sport_home_hs' . $max[2] . '">
                    <p class="pos_absolute tag_testimony_bloc_sport_home_hs fsize30">' . $instance['tags'] . '</p>
                    <img src="/images/photos_temoignages/' . $instance['photoPath'] . '" class="pos_absolute img_testimony_bloc_sport_home_hs" />
                    <p class="pos_absolute content_testimony_bloc_sport_home_hs fsize30">
                        << ' . $max[0] . ' >>
                    </p>
                    ' . $max[1] . '
                    <p class="pos_absolute name_testimony_bloc_sport_home_hs fsize30">
                        <b>' . $instance['author'] . '</b>
                    </p>
                    <p class="pos_absolute function_testimony_bloc_sport_home_hs fsize30">
                        ' . $instance['function'] . '
                    </p>
                </div>
            </div>
            <div class="pos_absolute previous_bloc_testimony_sport_hidden">
                <div class="pos_absolute testimony_bloc_sport_home_hs_hidden">
                    <p class="pos_absolute tag_testimony_bloc_sport_home_hs">' . $instance['tags'] . '</p>
                    <img src="/images/quit_testimony.png" class="pos_absolute img_quit_testimony_bloc_sport_home_hs_hidden" />
                    <img src="/images/photos_temoignages/' . $instance['photoPath'] . '" class="pos_absolute img_testimony_bloc_sport_home_hs_hidden" />
                    <p class="pos_absolute content_testimony_bloc_sport_home_hs fsize30">
                        << ' . ($instance['content']) . ' >>
                    </p>
                    <p class="pos_absolute name_testimony_bloc_sport_home_hs fsize30">
                        <b>' . $instance['author'] . '</b>
                    </p>
                    <p class="pos_absolute function_testimony_bloc_sport_home_hs fsize30">
                        ' . $instance['function'] . '
                    </p>
                </div>
            </div>
        ';
    }
}

public function form($instance) {

    $photoPath = isset($instance['photoPath']) ? $instance['photoPath'] : '';
    $content = isset($instance['content']) ? $instance['content'] : '';
    $author = isset($instance['author']) ? $instance['author'] : '';
    $function = isset($instance['function']) ? $instance['function'] : '';
    $tags = isset($instance['tags']) ? $instance['tags'] : '';

    ?>
    <p>
        <label for="<?php echo $this->get_field_name('photoPath');?>"><?php _e('Nom de la photo avec extension (doit être carrée et dans le dossier photos_temoignages) : ');?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('photoPath');?>" name="<?php echo $this->get_field_name('photoPath');?>" type="text" value="<?php echo $photoPath;?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_name('content');?>"><?php _e('Contenu : ');?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('content');?>" name="<?php echo $this->get_field_name('content');?>" type="text" value="<?php echo $content;?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_name('author');?>"><?php _e('Nom et prénom de l\'auteur :');?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('author');?>" name="<?php echo $this->get_field_name('author');?>" type="text" value="<?php echo $author;?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_name('function');?>"><?php _e('Fonction ou sport exercé de l\'auteur :');?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('function');?>" name="<?php echo $this->get_field_name('function');?>" type="text" value="<?php echo $function;?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_name('tags');?>"><?php _e('Tags :');?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('tags');?>" name="<?php echo $this->get_field_name('tags');?>" type="text" value="<?php echo $tags;?>" />
    </p>

    <?php
}

public function maximum($text) {

    $toReturnText = $text;
    $toReturnImg = "";
    $toReturnClass = "";

    if(strlen($text) > 390) {

        $lastSpacePos = 0;

        for($i = 0; $i < 389; $i++) {

            if($text[$i] == ' ') {

                $lastSpacePos = $i;
            }
        }

        $textSplited = substr($text, 0, $lastSpacePos);

        $toReturnText = $textSplited . ' ...';
        $toReturnImg = '<img src="' . get_stylesheet_directory_uri() . '/images/bouton_gris_hs.png" class="pos_absolute need_more_testimony_button"  />';
        $toReturnClass = " need_more_testimony";
    }

    return [$toReturnText, $toReturnImg, $toReturnClass];
} 
}

我的侧边栏:

function temoignages_sport() {

    register_sidebar(array(
        'id' => 'temoignages_sport',
        'name'=> 'Temoignages Sport',
        'description' => 'Emplacement des témoignages concernant le sport version mobile'
     ));
 }

0 个答案:

没有答案