This tutorial provides the code to add a custom condition using which we can output any element (like a Section) in Oxygen only if a custom field (created with Advanced Custom Fields) has a value.

This is based on the following screencast by Elijah (5:33 onwards):

Step 1

Install and activate Code Snippets plugin.

Step 2

Go to Snippets > Add New.

Title: [Condition] ACF Field Not Empty

Code:

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	oxygen_vsb_register_condition(
		// Condition Name
		'ACF Field Not Empty',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		array( '--' ),
		
		// Callback Function: Name of function that will be used to handle the condition
		'acf_field_not_empty_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'ACF'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, name of the ACF custom field.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function acf_field_not_empty_callback( $value, $operator ) {

	return ( get_field( $value ) ) ? true : false;

}

Set the snippet to run everywhere.

Save changes and activate.

Step 3

Now whichever Section you would like to be output on the front end when a specific custom field is populated, set a Condition to it like this:

replace “banner_text” with the name of your ACF custom field

and the Section will be printed on the front end only if that custom field associated to the current view (like a Page) has a value.

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE
magnifiercross
>
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: