Home Forums Front-end Issues How to change ACF post fields being displayed based on category?


How to change ACF post fields being displayed based on category?

  • I have searched but could not find a solution I understand to this problem.

    I originally used only one post type that had Advanced Custom Fields for the content and put the following code in my single-post.php page in child theme to display on the frontend:

        <!-- START -->
        <div id="primary">
        	<div id="content" role="main">
        			<h1><?php get_post_field('post_title'); ?></h1>
        			<h2>Title: </h2><?php the_field('title'); ?><br><br>
        			<h3>Year: </h3><?php the_field('year'); ?><br><br>
        			<h4>Location: </h4><?php the_field('location'); ?><br><br>
        			<h4>Info: <br></h4><?php the_field('info'); ?><br><br>
        			<h4>Physical Description: <br></h4><?php the_field('description'); ?><br><br>
        			<h4>Social Media Links: <br></h4><?php the_field('social-media'); ?><br>
    $image = get_field('bio-pic2');
    if( !empty($image) ): ?>
    	<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif; ?>
    $image = get_field('bio-pic3');
    if( !empty($image) ): ?>
    	<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif; ?>
    	</div><!-- #content -->
    </div><!-- #primary -->
    <!-- END -->

    I would like to have 2 other post categories now that do not use the same ACF fields. I do not know how code a function to change field display based on the category of the post. The 2 new post categories can use the default formatting of WordPress posts but the empty main post fields still display if empty because I have put them in the theme template code.

    What code based on this format would be best use to show/hide displayed custom fields based on the category type OR at least be able to hide all custom fields if the category is not the same. I am not proficient with this, please explain simply or provide code worked into the existing code provided if possible.

    Feel free to ask anything to clarify or point out any issues. Thank you.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.