How to display custom fields above other widgets of sidebar in Genesis

Posted on

In this article I show how we can use ACF Pro to attach two custom fields, Event Cost and Event Sign up URL to event Custom Post Type and display their values (if present) in the Primary sidebar at the top of other widgets.

Step 1

Install and activate Pro version of Advanced Custom Fields. Pro version is needed for this example because one of the two custom fields being used is of URL type which is not available in the free version.

Step 2

Install and activate your favorite Events plugin. In this example I am using Event Organiser. This creates a event CPT.

Step 3

Add a new custom fields group named say, Event Meta and add the two custom fields like so (export file below the screenshot):

If you would like to simply import this custom field group, here’s the export file.

Step 4

Add/Edit your event entries and populate the custom fields.

Step 5

Add the following in child theme’s functions.php:

// Display values of custom fields at the top of Primary Sidebar on event CPT singular pages
add_action( 'genesis_before_sidebar_widget_area', 'sk_single_event_custom_fields' );
function sk_single_event_custom_fields() {

// if we are not on a single event page, abort.
if ( !is_singular( 'event' ) ) {
return;
}

$event_cost = get_post_meta( get_the_ID(), 'event_cost', true );
$event_sign_up_url = get_post_meta( get_the_ID(), 'event_sign_up_url', true );

if ( $event_cost || $event_sign_up_url ) {
echo '<section class="widget event-custom-fields"><div class="widget-wrap">';
if ( $event_cost ) {
echo '<p>Event Cost: $', $event_cost, '</p>';
}

if ( $event_sign_up_url ) {
echo '<p><a href="', $event_sign_up_url , '" class="button">Sign Up</a></p>';
}
echo '</div></section>';
}

}