Home › Forums › Add-ons › Flexible Content Field › Flexible Content in the Widget
I have added flexible content to the text widget but I cannot get it to display. Does flexible content work in the widget?
add_filter('dynamic_sidebar_params', 'my_dynamic_sidebar_params');
function my_dynamic_sidebar_params( $params ) {
// get widget vars
$widget_name = $params[0]['widget_name'];
$widget_id = $params[0]['widget_id'];
// bail early if this widget is not a Text widget
if( $widget_name != 'Text' ) {
return $params;
}
// add color style to before_widget
$content_type = get_field('content_type', 'widget_' . $widget_id);
if( $content_type == 'important_links' ) {
?>
<?php while(the_flexible_field("important_links")): ?>
<?php if(get_row_layout() == "file"): // layout: Content ?>
<?php $params[0]['after_title'] .= 'file'; ?>
<?php elseif(get_row_layout() == "link"): // layout: File ?>
<?php $params[0]['after_title'] .= 'link'; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php }
if( $content_type == 'contacts' ) { ?>
<?php if( have_rows('contacts') ): ?>
<?php $params[0]['after_title'] .= '<ul class="slides">' ?>
<ul class="slides">
<?php while( have_rows('contacts') ): the_row();
// vars
$contact_name = get_sub_field('contact_name');
$contact_title = get_sub_field('contact_title');
$contact_number = get_sub_field('contact_number');
$contact_email = get_sub_field('contact_email');
?>
<?php $params[0]['after_title'] .= '<li><header>' ?>
<?php $params[0]['after_title'] .= sprintf('<h4>%s</h4>', $contact_name) ?>
<?php $params[0]['after_title'] .= sprintf('<h5>%s</h5>', $contact_title) ?>
<?php $params[0]['after_title'] .= '</header><p>' ?>
<?php $params[0]['after_title'] .= sprintf('<a class="tel" href="tel:%s">%s</a><br />', $contact_number, $contact_number) ?>
<?php $params[0]['after_title'] .= sprintf('<a class="email" href="mailto:%s">%s</a><br />', $contact_email, $contact_email) ?>
<?php $params[0]['after_title'] .= '</p></li>' ?>
<?php endwhile; ?>
</ul>
<?php endif;
}
// return
return $params;
}
Figured it out on my own. Putting this here in case it helps someone. The following uses both a repeater field and flexible content in the widget.
function my_dynamic_sidebar_params( $params ) {
// get widget vars
$widget_name = $params[0]['widget_name'];
$widget_id = $params[0]['widget_id'];
// bail early if this widget is not a Text widget
if( $widget_name != 'Text' ) {
return $params;
}
//add content to widget
$content_type = get_field('content_type', 'widget_' . $widget_id);
if( $content_type == 'important_links' ) {
?>
<?php $params[0]['after_title'] .= '<ul>' ?>
<?php while(the_flexible_field("important_links", 'widget_' . $widget_id)): ?>
<?php if(get_row_layout() == "file"): // layout: file
//vars
$file_link_text = get_sub_field('file_link_text', 'widget_' . $widget_id);
$file = get_sub_field('file', 'widget_' . $widget_id);
$file_type = get_sub_field('file_type', 'widget_' . $widget_id);
?>
<?php $params[0]['after_title'] .= sprintf('<li><a href="%s" class="is-%s">%s</a></li>', $file['url'], $file_type, $file_link_text) ?>
<?php elseif(get_row_layout() == "link"): // layout: link
$link_text = get_sub_field('link_text', 'widget_' . $widget_id);
$url = get_sub_field('url', 'widget_' . $widget_id);
?>
<?php $params[0]['after_title'] .= sprintf('<li><a href="%s">%s</a></li>', $url, $link_text) ?>
<?php endif; ?>
<?php endwhile; ?>
<?php $params[0]['after_title'] .= '</ul>' ?>
<?php }
if( $content_type == 'contacts' ) { ?>
<?php if( have_rows('contacts', 'widget_' . $widget_id) ): ?>
<?php $params[0]['after_title'] .= '<ul>' ?>
<?php while( have_rows('contacts', 'widget_' . $widget_id) ): the_row();
// vars
$contact_name = get_sub_field('contact_name', 'widget_' . $widget_id);
$contact_title = get_sub_field('contact_title', 'widget_' . $widget_id);
$contact_phone = get_sub_field('contact_phone', 'widget_' . $widget_id);
$contact_email = get_sub_field('contact_email', 'widget_' . $widget_id);
$tel = preg_replace("/[^A-Za-z0-9]/", "", $contact_phone);
?>
<?php $params[0]['after_title'] .= sprintf('<li><header><h4>%s</h4>', $contact_name) ?>
<?php $params[0]['after_title'] .= sprintf('<h5>%s</h5></header><p>', $contact_title) ?>
<?php $params[0]['after_title'] .= sprintf('<a class="tel" href="tel:%s">%s</a><br />', $tel, $contact_phone) ?>
<?php $params[0]['after_title'] .= sprintf('<a class="email" href="mailto:%s">%s</a></p></li>', $contact_email, $contact_email) ?>
<?php endwhile; ?>
<?php $params[0]['after_title'] .= '</ul>' ?>
<?php endif;
}
// return
return $params;
}
add_filter('dynamic_sidebar_params', 'my_dynamic_sidebar_params');
The topic ‘Flexible Content in the Widget’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.