Я создал простой виджет с надписью "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>
Но проблема в том, что я не знаю, как вывести этот div через "echo". Это правда? Как это сделать? Извините за мой английский!






Вы можете использовать это так
echo "<div class='someclass'>Hello World!</div>";
Просто убедитесь, что у вас есть классы или идентификаторы, чтобы использовать разные кавычки.
Попробуй это:
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']; }
Есть несколько способов справиться с выводом большого количества HTML-кода с помощью PHP. Прежде всего, вы можете выйти из 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'];
}
}
Вы также можете использовать Выходной контроль с выходным буфером:
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'];
}
}
Лучше всего использовать сам escape-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'];
}
}