Wordpress小部件作为div

时间:2018-09-05 17:18:04

标签: php html wordpress widget

我使用“ Hello World!”创建了简单的小部件。作为内容:

    public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );
    echo $args['before_widget'];
    //if title is present
    if ( ! empty( $title ) )
    echo $args['before_title'] . $title . $args['after_title'];
    //output
    echo __( 'Hello World!', 'hstngr_widget_domain' );
    echo $args['after_widget']; }

但是我需要将其实现为div。如果需要,有一个代码:

<div class='col-sm-12 col-md-8' style="padding-bottom: 15px;">
    <div class="more-equities-research-at-tipranks">
        <h3>More <span class="equities-name"><?php echo $company_ticker ?></span> Research at TipRanks</h3>
        <div class="equities-icons-links">
            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>" class="equities-link margin-b-15">
                <span class="equities-icons analyst-ratings"></span>analyst ratings
            </a>

            <a target="_blank" href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-predictions" class="equities-link margin-b-15">
                <span class="equities-icons blogger-opinions"></span>blogger opinions
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-news" class="equities-link margin-b-15">
                <span class="equities-icons news-sentiment"></span>news sentiment
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-charts" class="equities-link margin-b-15">
                <span class="equities-icons stats-charts"></span>stats charts
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/investor-sentiment" class="equities-link">
                <span class="equities-icons investor-sentiment"></span>investor sentiment
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/insider-trading" class="equities-link">
                <span class="equities-icons insider-activity"></span>insider activity
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/hedge-funds" class="equities-link">
                <span class="equities-icons hedge-funds"></span>hedge funds
            </a>
        </div>
    </div>
</div>

但是问题是我不知道如何通过“ echo”输出这个div。真的是真的吗?这该怎么做? 对不起,我的英语!

3 个答案:

答案 0 :(得分:0)

您可以像这样使用它

echo "<div class='someclass'>Hello World!</div>";

只需确保您是否具有使用不同引号的类或ID。

答案 1 :(得分:0)

尝试一下:

echo $args['before_title'] . $title . $args['after_title'];
?>
<div class='col-sm-12 col-md-8' style="padding-bottom: 15px;">
    <div class="more-equities-research-at-tipranks">
        <h3>More <span class="equities-name"><?php echo $company_ticker ?></span> Research at TipRanks</h3>
        <div class="equities-icons-links">
            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>" class="equities-link margin-b-15">
                <span class="equities-icons analyst-ratings"></span>analyst ratings
            </a>

            <a target="_blank" href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-predictions" class="equities-link margin-b-15">
                <span class="equities-icons blogger-opinions"></span>blogger opinions
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-news" class="equities-link margin-b-15">
                <span class="equities-icons news-sentiment"></span>news sentiment
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/stock-charts" class="equities-link margin-b-15">
                <span class="equities-icons stats-charts"></span>stats charts
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/investor-sentiment" class="equities-link">
                <span class="equities-icons investor-sentiment"></span>investor sentiment
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/insider-trading" class="equities-link">
                <span class="equities-icons insider-activity"></span>insider activity
            </a>

            <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker; ?>/hedge-funds" class="equities-link">
                <span class="equities-icons hedge-funds"></span>hedge funds
            </a>
        </div>
    </div>
</div>
<?php
echo $args['after_widget']; }

答案 2 :(得分:0)

有两种方法可以处理使用PHP输出大量HTML的问题。首先,您可以退出您的PHP:

class Foo_Widget extends WP_Widget {
    public function widget( $args, $instance ){
        echo $args['before_widget'];
            if( $title = apply_filters( 'widget_title', $instance['title'] ) )
                echo $args['before_title'] . $title . $args['after_title']; ?>

            <div class="col-sm-12 col-md-8" style="padding-bottom: 15px;">
                <!-- Rest of the code here -->
            </div>
        <?php echo $args['after_widget'];
    }
}

您还可以将Output Control与输出缓冲区一起使用:

class Foo_Widget extends WP_Widget {
    public function widget( $args, $instance ){
        echo $args['before_widget'];
            if( $title = apply_filters( 'widget_title', $instance['title'] ) )
                echo $args['before_title'] . $title . $args['after_title'];

            ob_start(); ?>
                <div class="col-sm-12 col-md-8" style="padding-bottom: 15px;">
                    <!-- Rest of the code here -->
                </div>
        <?php echo ob_get_clean();
        echo $args['after_widget'];
    }
}

您可以简单地回显整个糟糕的事情(尽管您可能不应该这样做)

class Foo_Widget extends WP_Widget {
    public function widget( $args, $instance ){
        echo $args['before_widget'];
            if( $title = apply_filters( 'widget_title', $instance['title'] ) )
                echo $args['before_title'] . $title . $args['after_title'];

            echo '<div class="col-sm-12 col-md-8" style="padding-bottom: 15px;">
                <!-- Rest of the code here -->
            </div>';
        echo $args['after_widget'];
    }
}

或者您可以在运行时将变量与HTML连接起来(同样,可能不要这样做)

class Foo_Widget extends WP_Widget {
    public function widget( $args, $instance ){
        echo $args['before_widget'];
            if( $title = apply_filters( 'widget_title', $instance['title'] ) )
                echo $args['before_title'] . $title . $args['after_title'];

            $html = '<div class="col-sm-12 col-md-8" style="padding-bottom: 15px;">';
                $html .= '<!-- Rest of the code here -->';
            $html .= '</div>';

            echo $html;
        echo $args['after_widget'];
    }
}

您最好的选择是第一个或第二个示例中的转义PHP本身,您甚至可以使用基本的foreach循环使较大的HTML块更易于管理:

class Foo_Widget extends WP_Widget {
    public function widget( $args, $instance ){
        echo $args['before_widget'];
            if( $title = apply_filters( 'widget_title', $instance['title'] ) )
                echo $args['before_title'] . $title . $args['after_title']; ?>

            <div class="col-sm-12 col-md-8" style="padding-bottom: 15px;">
                <div class="more-equities-research-at-tipranks">
                    <h3>More <span class="equities-name"><?php echo $company_ticker ?></span> Research at TipRanks</h3>
                    <div class="equities-icons-links">
                        <?php
                            $array = array(
                                'analyst ratings'    => array( 'link', 'margin-b-15' ),
                                'blogger opinions'   => array( '/stock-predictions', 'margin-b-15' ),
                                'news sentiment'     => array( '/stock-news', 'margin-b-15' ),
                                'stats charts'       => array( '/stock-charts', 'margin-b-15' ),
                                'investor sentiment' => array( '/investor-sentiment', '' ),
                                'insider activity'   => array( '/insider-trading', '' ),
                                'hedge funds'        => array( '/hedge-funds', '' ),
                            );

                            foreach( $array as $item => $atts ){ ?>
                                <a target="_blank"  href="https://www.tipranks.com/stocks/<?php echo $company_ticker.$atts[0]; ?>" class="equities-link <?php echo $atts[1]; ?>">
                                    <span class="equities-icons <?php echo sanitize_title_with_dashes( $item ); ?>"></span><?php echo $item; ?>
                                </a>
                            <?php }
                        ?>
                    </div>
                </div>
            </div>
        <?php echo $args['after_widget'];
    }
}