导致白屏的简单小工具

时间:2012-10-18 作者:Dom

此小部件代码导致我的WP站点出现白色屏幕。我已经对代码进行了大约5次重分解,试图解决它,但运气不好。

<?php

    class latest_mag_issue_widget extends WP_Widget {

        function latest_mag_issue_widget() {
            $widget_ops = array(\'classname\' => \'widget_latest_issues\', \'description\' => \'Latest Mag Issues\' );
            $this->WP_Widget(\'latest_issues\', \'Latest Issues\', $widget_ops);
        } //end constructor

        function widget( $args, $instance ) {
            extract( $args );

            // widget options
            $title = apply_filters( \'widget_title\', $instance[\'title\'] );
            $pastissues = $instance[\'pastissues\'];
            $thisissue = $instance[\'thisissue\'];

            echo $before_widget;

            echo \'<div class="widget widget-latest-issue">\';

            if ( $title ) {
                echo $before_title . $title . $after_title;
            }

            wp_reset_query();
            $wp_query = new WP_Query();
            $wp_query->query(array( \'post_type\' => \'mag-issue\', \'post_status\' => \'publish\', \'paged\' => 1, \'posts_per_page\' => 1));

            if ( have_posts() ) : while ( have_posts() ) : the_post();

            $image_url = get_post_meta($post->ID, \'press_smart\', true);
            $image_title = the_title();
            $image_thumbnail = the_post_thumbnail( array(125,166) );

            echo \'
                <div id="latest-issue">
                    <h3>Latest Issue</h3>
                    <a class="fl" href="\' . $image_url . \'" title="\' . $image_title .\'">\' . $image_thumbnail . \'</a> <br/>
                </div>
                \';

                if ( $thisissue == true) { 

                echo \'
                    <div id="this-issue">
                        <h3>In This Issue</h3>
                        <ul>\';

                            for ($i=1; $i < 10; $i++) { 
                                $headline_meta = get_post_meta($post->ID , \'headline_\' . $i  , true);
                                if ( $headline_meta ) { echo \'<li>\' . $headline_meta . \'</li><hr>\';}
                            } 

                            endwhile; endif; wp_reset_query();
                    echo \'
                        </ul>
                    </div>
                    \';

                }

                if ( $pastissues == true ) { 

                    $wp_query->query(array( \'post_type\' => \'mag-issue\', \'post_status\' => \'publish\', \'paged\' => 1, \'posts_per_page\' => 9, \'offset\' => 1 ));

                    if ( have_posts() ) : while ( have_posts() ) : the_post();

                    $post_url = get_post_meta( $post->ID, \'press_smart\', true );
                    $post_title = the_title();

                echo \'
                    <div id="past-issues">
                        <h3>Past Issues</h3>
                        <ul>        
                            <li>
                                <a href="\' . $post_url  . \'">\' . $post_title . \'</a>
                            </li>\';

                    endwhile; endif; wp_reset_query();

                    echo \'
                        </ul>
                    </div>\';

                } 
            echo \'</div>\';

            echo $after_widget; 


        } //end function widget()

        function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
            $instance[\'thisissue\'] = strip_tags( $new_instance[\'thisissue\'] );
            $instance[\'pastissues\'] = strip_tags( $new_instance[\'pastissues\'] );
            return $instance;
        } //end function update()


        function form($instance) {

            $title = esc_attr( $instance[\'title\'] );
            $thisissue = esc_attr( $instance[\'thisissue\'] );
            $pastissues = esc_attr( $instance[\'pastissues\'] );

            ?>

            <p>
                <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Widget Title\'); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" />
            </p>

            <p>
                <input id="<?php echo $this->get_field_id(\'thisissue\'); ?>" name="<?php echo $this->get_field_name(\'thisissue\'); ?>" type="checkbox" value="1" <?php checked( \'1\', $thisissue ); ?>/>
                <label for="<?php echo $this->get_field_id(\'thisissue\'); ?>"><?php _e(\'Display headlines?\'); ?></label>
            </p>

            <p>
                <input id="<?php echo $this->get_field_id(\'pastissues\'); ?>" name="<?php echo $this->get_field_name(\'pastissues\'); ?>" type="checkbox" value="1" <?php checked( \'1\', $pastissues ); ?>/>
                <label for="<?php echo $this->get_field_id(\'pastissues\'); ?>"><?php _e(\'Display past issues?\'); ?></label>
            </p>

            <?php
        } //end function form()

    }

    // Register the widget
    add_action( \'widgets_init\', \'register_mag_issues_widget\' );
    function register_mag_issues_widget() {
        register_widget( \'latest_mag_issues_widget\' );
    }

?>
谢谢你,

Dom

1 个回复
最合适的回答,由SO网友:stealthyninja 整理而成

register_widget( \'latest_mag_issues_widget\' );
应该是

register_widget( \'latest_mag_issue_widget\' );

Update<不过,在修复该错误后,还有几个其他错误。以下是我可以使用的代码的完整更新副本:

class latest_mag_issue_widget extends WP_Widget {

    function latest_mag_issue_widget() {
        $widget_ops = array( \'classname\' => \'widget_latest_issues\', \'description\' => \'Latest Mag Issues Widget for Media24 Business & Customs\' );
        $this->WP_Widget( \'latest_issues\', \'Latest Issues\', $widget_ops );
    } //end constructor

    function widget( $args, $instance ) {
        extract( $args );

        // widget options
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        $pastissues = $instance[\'pastissues\'];
        $thisissue = $instance[\'thisissue\'];

        echo $before_widget;

        echo \'<div class="widget widget-latest-issue">\';

        if ( $title ) {
            echo $before_title . $title . $after_title;
        }

        $wp_query = new WP_Query();
        $wp_query->query( array( \'post_type\' => \'mag-issue\', \'post_status\' => \'publish\', \'paged\' => 1, \'posts_per_page\' => 1 ) );

        if ( have_posts() ) : while ( have_posts() ) : the_post();
            $image_url = get_post_meta( $post->ID, \'press_smart\', true );
            $image_title = the_title();
            $image_thumbnail = the_post_thumbnail( array(125,166) );

            echo \'
                <div id="latest-issue">
                    <h3>Latest Issue</h3>
                    <a class="fl" href="\' . $image_url . \'" title="\' . $image_title .\'">\' . $image_thumbnail . \'</a> <br/>
                </div>
            \';

            if ( $thisissue == true ) {
                echo \'
                    <div id="this-issue">
                        <h3>In This Issue</h3>
                        <ul>\';

                        for ( $i = 1; $i < 10; $i++ ) {
                            $headline_meta = get_post_meta( $post->ID , \'headline_\' . $i  , true );
                            if ( $headline_meta ) { echo \'<li>\' . $headline_meta . \'</li><hr>\';}
                        }

                echo \'
                        </ul>
                    </div>
                \';
            }

            if ( $pastissues == true ) {

                $wp_query->query(array( \'post_type\' => \'mag-issue\', \'post_status\' => \'publish\', \'paged\' => 1, \'posts_per_page\' => 9, \'offset\' => 1 ));

                if ( have_posts() ) : while ( have_posts() ) : the_post();

                    $post_url = get_post_meta( $post->ID, \'press_smart\', true );
                    $post_title = the_title();

                    echo \'
                    <div id="past-issues">
                        <h3>Past Issues</h3>
                        <ul>
                            <li>
                                <a href="\' . $post_url  . \'">\' . $post_title . \'</a>
                            </li>\';
                    echo \'
                        </ul>
                    </div>
                    \';

                    endwhile; endif; wp_reset_postdata();
            }
        echo \'</div>\';

        echo $after_widget;

        endwhile; endif; wp_reset_query();
    } //end function widget()

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'thisissue\'] = strip_tags( $new_instance[\'thisissue\'] );
        $instance[\'pastissues\'] = strip_tags( $new_instance[\'pastissues\'] );
        return $instance;
    } //end function update()


    function form($instance) {

        $title = esc_attr( $instance[\'title\'] );
        $thisissue = esc_attr( $instance[\'thisissue\'] );
        $pastissues = esc_attr( $instance[\'pastissues\'] );

        ?>

        <p>
            <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Widget Title\'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>

        <p>
            <input id="<?php echo $this->get_field_id(\'thisissue\'); ?>" name="<?php echo $this->get_field_name(\'thisissue\'); ?>" type="checkbox" value="1" <?php checked( \'1\', $thisissue ); ?>/>
            <label for="<?php echo $this->get_field_id(\'thisissue\'); ?>"><?php _e(\'Display headlines?\'); ?></label>
        </p>

        <p>
            <input id="<?php echo $this->get_field_id(\'pastissues\'); ?>" name="<?php echo $this->get_field_name(\'pastissues\'); ?>" type="checkbox" value="1" <?php checked( \'1\', $pastissues ); ?>/>
            <label for="<?php echo $this->get_field_id(\'pastissues\'); ?>"><?php _e(\'Display past issues?\'); ?></label>
        </p>

        <?php
    } //end function form()

}

// Register the widget
add_action( \'widgets_init\', \'register_mag_issues_widget\' );
function register_mag_issues_widget() {
    register_widget( \'latest_mag_issue_widget\' );
}

结束

相关推荐

Widgets in PHP files?

有没有可能让Wordpress上的每个小部件都以不同的方式运行。php文件我有一个包含12个元素的页面,我想让每个元素都成为一个小部件,以便以后更容易管理/编辑它们,但如果它来自php页面,而不是来自管理面板上的代码块,则会更好。