【问题标题】:Conflict with Widget function与小部件功能冲突
【发布时间】:2017-09-14 11:32:24
【问题描述】:

这是我的注册小部件侧边栏代码

// 侧边寄存器

add_action('widgets_init', 'comet_sidebar');

function comet_sidebar() {
    register_sidebar(array(
        'name'          => __('Right Sidebar', 'comet'),
        'description'   => __('Put Right Sidebar here', 'comet'),
        'id'            => 'right-sidebar',
        'before_widget' => '<div class="widget">',
        'after_widget'  => '</div>',
        'before_title'  => '<h6 class="upper">',
        'after_title'   => '</h6>',
    ));

    register_sidebar(array(
        'name'          => __('Footer Left', 'comet'),
        'description'   => __('Put Footer Sidebar here', 'comet'),
        'id'            => 'footer-left',
        'before_widget' => '<div class="col-sm-4"><div class="widget">',
        'after_widget'  => '</div></div>',
        'before_title'  => '<h6 class="upper">',
        'after_title'   => '</h6>',
    ));

    register_sidebar(array(
        'name'          => __('Footer Right', 'comet'),
        'description'   => __('Put Footer Sidebar here', 'comet'),
        'id'            => 'footer-right',
        'before_widget' => '<div class="col-sm-4"><div class="widget">',
        'after_widget'  => '</div></div>',
        'before_title'  => '<h6 class="upper">',
        'after_title'   => '</h6>',
    ));

}

这是我的另一个 php 文件。我有要求

if( file_exists( dirname(__FILE__). '/gallery.php' ) ) {
    require_once(dirname(__FILE__). '/gallery.php' );
}

gallery.php 的代码

<?php


add_shortcode('gallery', 'comet_gallery');

function comet_gallery($attr, $content) {
    $att = shortcode_atts( array(
        'ids' => '',
    ), $attr);          

    extract($att);

    $idd = explode(',', $ids);

    ob_start(); ?>
    <div data-options="{&quot;animation&quot;: &quot;slide&quot;, &quot;controlNav&quot;: true" class="flexslider nav-outside">
        <ul class="slides">
            <?php foreach($idd as $id) : ?>
            <?php $musa = wp_get_attachment_image_src($id, 'full'); ?>

            <li><img src="<?php echo $musa [0]; ?>"></li>
            <?php endforeach; ?>
        </ul>
    </div>

    <?php return ob_get_clean();

}

当我需要这个文件时。我的小部件设置无法正常工作。如果我删除这个需要文件。它运行良好。 谢谢

【问题讨论】:

  • 你在gallery.php中有什么?到底发生了什么?只有小部件设置不能正常工作,或者你有任何空白/损坏的页面?
  • 我已经添加了gallery.php的代码
  • 我刚刚在下面编辑了我的答案。

标签: php wordpress widget require conflicting-libraries


【解决方案1】:

我不认为return ob_get_clean(); 是正确的。

试试这个:

<?php
add_shortcode('gallery', 'comet_gallery');

function comet_gallery($attr, $content) {
    $att = shortcode_atts( array(
        'ids' => '',
    ), $attr);          

    extract($att);

    $idd = explode(',', $ids);
 ?>
    <div data-options="{&quot;animation&quot;: &quot;slide&quot;, &quot;controlNav&quot;: true" class="flexslider nav-outside">
        <ul class="slides">
            <?php foreach($idd as $id) : ?>
            <?php $musa = wp_get_attachment_image_src($id, 'full'); ?>

            <li><img src="<?php echo $musa [0]; ?>"></li>
            <?php endforeach; ?>
        </ul>
    </div>

    <?php return $content;

}
function comet_gallery_start() { ob_start("comet_gallery"); }
function comet_gallery_end() { ob_end_flush(); }
add_action('wp_head', 'comet_gallery_start');
add_action('wp_footer', 'comet_gallery_end');

require_once( TEMPLATEPATH."/gallery.php" );

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    相关资源
    最近更新 更多