admin管理员组文章数量:1326502
I have built a handful of custom widgets on my site. In the main these are just fine how they are. But occassionally I also want to be able to use a field individually rather than the whole widget.
For example I have an address widget which has five fields: 'Street', 'Locality', 'Region', 'PostCode', and 'Country'.
The single address that is output will be used several locations on the site, but occasionally I just need the value from one field at a time eg: just the 'PostCode'.
Is there a way to extract individual field data from a widget?
As I assume is pretty standard the display for my widget is handled like so:
public function widget( $args, $instance ) {
extract( $args );
$street = $instance['street'];
$locality = $instance['locality'];
$region = $instance['region'];
$postCode = $instance['postCode'];
$country = $instance['country'];
echo $before_widget;
// Display the widget
if ( $street ) {
echo $street . '<br>';
}
if ( $locality ) {
echo $locality . ',<br>';
}
if ( $region ) {
echo $region . ',<br>';
}
if ( $postCode ) {
echo $postCode . '<br>';
}
if ( $country ) {
echo $country;
}
echo $after_widget;
}
It is used in a sidebar call 'address' and currently I output that to the page like so:
if ( is_active_sidebar( 'address' ) ) { dynamic_sidebar( 'address' ); }
My assumtion is that either an edit to the latter would do what I'm after, or I can somehow access the individual fields of the widget in the sidebar seperately. Eiter way (if any assumption is correct) I need some direction as to how to do this. I will need to usethis same data in several locations, just on occassion not all of it.
本文标签: How would you return a value from an individual field in a custom widget
版权声明:本文标题:How would you return a value from an individual field in a custom widget? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203522a2432392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论