March 15, 2017
Similar to my Subnavigation Function, this will display widgets and adjust column widths if widgets are active on the page.
The “show_on_pages” custom field being used is utilizing the Relationship Field from Advanced Custom Fields.
Functions:
function has_widget(){
$widgets = get_posts(array(
'post_type' => 'widget',
'meta_query' => array(
array(
'key' => 'show_on_pages', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if($widgets) {}
return $widgets;
}
function display_widget($get_count = false) {
echo '<div id="subwidget_container"><div class="sub_widgets">';
echo '<ul>';
$widgets = get_posts(array(
'post_type' => 'widget',
'meta_query' => array(
array(
'key' => 'show_on_pages', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if( $widgets ):
foreach( $widgets as $widget ): ?>
<li>
<div class="widget_content">
<!-- <h3><?php echo $widget->post_title; ?></h3> -->
<?php echo apply_filters('the_content', get_post_field('post_content', $widget->ID)); ?>
</div><a href="<?php echo get_permalink( $widget->ID ); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/img/arrows_red.png" class="white_arrows" alt="White Arrows" />
</a>
</li>
<?php endforeach;
endif;
echo '</ul></div></div>';
}
Trigger Content Width Adjustment:
<div class="entry-content <?php if(has_widget()) { echo 'with_widget'; } ?>">
Trigger Function (if active):
<?php if(has_widget()) {
echo display_widget();
} ?>
CSS:
.entry-content.with_widget {
width: 67%;
float: left;
}
#subwidget_container {
float: right;
width: 30%;
}