Last active
July 20, 2017 15:15
-
-
Save verticalgrain/6e3c5acba16691da9791be967b3d1bb4 to your computer and use it in GitHub Desktop.
ACF Repeater in Wordpress Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Wordpress Widget that uses an ACF repeater | |
*/ | |
class HelpfulLinks extends WP_Widget{ | |
function __construct(){ | |
parent::__construct( | |
'helpful-links', | |
'Helpful Links', | |
'This widget lists helpful links' | |
); | |
} | |
function form ( $instance ){ | |
echo "<p>This will display a list of helpful links</pre>"; | |
echo "<br/>"; | |
} | |
function widget( $args, $instance ){ | |
echo $args[ 'before_widget' ] ?> | |
<?php if (get_field('helpful_links', 'widget_' . $args['widget_id'])) { ?> | |
<div class="headingblock headingblock--navy"> | |
<h2>Helpful Links</h2> | |
</div> | |
<div class="content-wrapper"> | |
<?php | |
// check if the repeater field has rows of data | |
if( have_rows('helpful_links', 'widget_' . $args['widget_id'])): | |
// loop through the rows of data | |
while ( have_rows('helpful_links', 'widget_' . $args['widget_id'])) : the_row(); | |
?> | |
<a class="content-wrapper__link" href="<?php the_sub_field('helpful_link_url'); ?>"><?php the_sub_field('helpful_link_title'); ?></a> | |
<?php | |
endwhile; | |
else : | |
endif; | |
?> | |
</div> | |
<?php } | |
echo $args[ 'after_widget' ]; | |
} | |
function update( $new_instance, $old_instance ){ | |
return $new_instance; | |
} | |
} | |
function register_custom_widgets(){ | |
register_widget( 'HelpfulLinks' ); | |
} | |
add_action( 'widgets_init', 'register_custom_widgets' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment