Support

Account

Home Forums General Issues Hiding custom fields using conditional logic?

Solving

Hiding custom fields using conditional logic?

  • Hi,

    I’m using Advanced Custom Fields to create a job employee directory where you can see their names and their occupations.

    This is the code I’ve written in acf.php.

    <?php
    //Add the advanced custom field login
    $custom_field_directory = get_stylesheet_directory() . '/advanced-custom-fields/';
    
    add_filter('acf/settings/path', 'job_acf_settings_path');
    add_filter('acf/settings/dir', 'job_acf_settings_path');
    
    function job_acf_settings_path($path)
    {
    	return $custom_field_directory;
    }
    
    include_once($custom_field_directory . 'acf.php');
    
    if(function_exists("register_field_group"))
    {
    	register_field_group(array (
    		'id' => 'acf_employee-featured-image',
    		'title' => 'Employee Featured Image',
    		'fields' => array (
    			array (
    				'key' => 'field_5742905771cab',
    				'label' => 'Employee Featured Image',
    				'name' => 'employee_featured_image',
    				'type' => 'image',
    				'save_format' => 'url',
    				'preview_size' => 'thumbnail',
    				'library' => 'all',
    			),
    			array (
    				'key' => 'field_57429977f4d76',
    				'label' => 'Employee Featured Image Hover',
    				'name' => 'employee_featured_image_hover',
    				'type' => 'image',
    				'save_format' => 'url',
    				'preview_size' => 'thumbnail',
    				'library' => 'all',
    			),
    			array (
    				'key' => 'field_5742a00492112',
    				'label' => 'Name',
    				'name' => 'name',
    				'type' => 'text',
    				'default_value' => '',
    				'placeholder' => '',
    				'prepend' => '',
    				'append' => '',
    				'formatting' => 'html',
    				'maxlength' => '',
    			),
    			array (
    				'key' => 'field_5742998ef4d77',
    				'label' => 'Job Title',
    				'name' => 'job_title',
    				'type' => 'text',
    				'default_value' => '',
    				'placeholder' => '',
    				'prepend' => '',
    				'append' => '',
    				'formatting' => 'html',
    				'maxlength' => '',
    			),
    			array (
    				'key' => 'field_5743fbb622028',
    				'label' => 'Read Bio',
    				'name' => 'read_bio',
    				'type' => 'page_link',
    				'conditional_logic' => array (
    					'status' => 1,
    					'rules' => array (
    						array (
    							'field' => 'null',
    							'operator' => '==',
    						),
    					),
    					'allorany' => 'all',
    				),
    				'post_type' => array (
    					0 => 'news-work',
    				),
    				'allow_null' => 0,
    				'multiple' => 0,
    			)
    		),
    		'location' => array (
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'key-people',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    			),
    		),
    		'options' => array (
    			'position' => 'normal',
    			'layout' => 'no_box',
    			'hide_on_screen' => array (
    			),
    		),
    		'menu_order' => 0,
    	));
    }
    }

    And this is the code I’ve written in the archive-employee.php to loop the custom posts on screen.

    <?php
    get_template_part('_post-content');
    
    // retrieve and display all employees
    $args = array(
        'post_type' => 'employee',
        'posts_per_page' => -1,
    );
    $employee = new WP_Query($args);
    
    ?>
    <div class="entry-content fade03">
    
        <?php
    
        while($employee->have_posts())
        {
            $employee->the_post();
            ?>
    		<div class="team-roster">
    				<a href="#">
               <!-- <a href="<?php /* echo get_permalink($id);*/ ?>">-->
                    <div class="title"><?php echo get_field('name', $id); ?></div>
    				<div class="subtitle"><?php echo get_field('job_title', $id); ?></div>
    			</a>	
                    <img src="<?php echo get_field('employee_featured_image', $id); ?>" alt="<?php echo the_title(); ?>" />
    				<img src="<?php echo get_field('employee_featured_image_hover' ,$id); ?>" alt="<?php echo the_title(); ?>"  />
    			
            </div>
            <?php
        }
    
        ?>
    

    As an experiment, I want only some of these employees to have a biography. But I haven’t included a link in the archive-employee.php file because that would repeat the link to every employee post.

    If I want to create an option to tick on a post in the backend to show a link to its own biography on the index page, could I create a custom field to determine whether or not a post has a biography? Would it involve using conditional logic?

    Thank you.

  • Hi @davidr

    If you don’t mind the link input field visible on the backend, you can always use the True/False field. With that field, you can check if the checkbox is checked or not like this:

    if( get_field('true_false_field_name', $id) )
    {
        echo get_field('biography_field_name', $id);
    }

    I hope this helps 🙂

  • Thank you very much James. With your help I figured it out. 😀

    I created a True/False field and called it “view_profile”, and added this block of code into my array in the acf.php file:

    array (
         'key' => 'field_57467a65db91f',
          'label' => 'View Profile',
          'name' => 'view_profile',
          'type' => 'true_false',
          'message' => 'Show biography?',
          'default_value' => 0,
    ),

    This displays the checkbox option in the backend.

    Finally, I used conditional statements to determine if a user wants to link a profile or not in my archive-employee.php.

    <div class="entry-content">
    
        <?php
    
        while($employee->have_posts())
        {
            $employee->the_post();
            ?>
    		<div class="team-roster">
    		<?php if ( get_field( 'view_profile' ) ): ?>
    			  <a href="<?php echo get_permalink($id); ?>">
                    <div class="title"><?php echo get_field('name', $id); ?></div>
    				<div class="subtitle"><?php echo get_field('job_title', $id); ?></div>	
    				<div class="button">View Profile</div>
    			  </a>
    			
    			<?php else : ?>	
    			<a href="#">
                    <div class="title"><?php echo get_field('name', $id); ?></div>
    				<div class="subtitle"><?php echo get_field('job_title', $id); ?></div>	
    			</a>
    							
    				<?php endif; ?>
                    <img src="<?php echo get_field('employee_featured_image', $id); ?>" alt="<?php echo the_title(); ?>" />
    				<img src="<?php echo get_field('employee_featured_image_hover' ,$id); ?>" alt="<?php echo the_title(); ?>"  />
            </div>
            <?php
        }
    
        ?>
    </div>

    Awesome!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Hiding custom fields using conditional logic?’ is closed to new replies.