Support

Account

Home Forums Front-end Issues Displaying stuff depending on categories

Solving

Displaying stuff depending on categories

  • Hi there, finally I managed to get the code right. For example I have several fields like this in my template part acffields/acf-books.php (yeah, to keep it tidy, i use a subfolder for acf related files)

    <?php
    echo '<p><strong>Informationen zum Buch</strong></p>
    <table>
    	<tr>
    		<td><em><strong>Author:</strong></em></td>
    		<td><i><?php the_field('bookauthor'); ?></td>
    	</tr>
    	<tr>
    		<td><em><strong>Title:</strong></em></td>
    		<td><i><?php the_field('booktitle'); ?></td>
    	</tr>
    		<tr>
    		<td><em><strong>Original title:</strong></em></td>
    		<td><i><?php the_field('booktitleorig'); ?></td>
    	</tr>
    		<tr>
    		<td><em><strong>Translatedby:</strong></em></td>
    		<td><i><?php the_field('booktranslator'); ?></td>
    	</tr>   
        // and blah blah blah --- even more table rows of all the fields of the field group and then the end of the table
    </table>';
    ?>
    

    so I also created template parts for films, for music etc.

    The hard thing now is to make it category dependent what template part to display right below the post content.

    For example:
    If the post is in the category “books” or in one of its subcategories -> display the template part acf-books.php
    else if the post is in the category “films” or in one of its subcategories -> display the template part acf-films.php
    (you can repeat this for audiobooks, games and music cds)
    And if none of those categories (or subcategories) match (because no custom fields are needed for posts in those categories) it just should display, well, none of the template parts.

    How can I manage this?

    Sorry, I’m really bad at coding. 🙁

  • This isn’t strictly speaking an ACF question, and more of a WP core question. Depending on how you’ve set up your site will depend on the particular code that you’ll need.

    Getting WP categories is relatively easy, and if you have set each of these up as a normal Post within WordPress, then you can use a combination of the function in_category() to check if the current post is in the specified category, and cat_is_ancestor_of() to check if that category is a sub-category of the parent.

    In practice this would look something like:

    <?php
    
    global $post;
    
    // Get categories applied to post
    $categories = get_the_category($post->ID)
    
    // Retrieve the first category ID (just in case multiple selected)
    $current_category = $categories[0]->term_id;
    
    // If this post is in the category Books, or a child-category of Books
    if( in_category('books') || cat_is_ancestor_of('books', $current_category) ) {
    	// Get the template part from acffields/acf-books.php
    	get_template_part('acffields/acf','books');
    }
    elseif( in_category('films') || cat_is_ancestor_of('films', $current_category) ){
    	// Get the template part from acffields/acf-films.php
    	get_template_part('acffields/acf','films');
    }
    else{
    	// If you want to put something else in if no category selected, add it in here.
    }
    
    ?>

    If you have set your posts up as a Custom Post Type with a custom taxonomy – then this won’t work, and you’ll need to use get_post_terms() instead, and then do an alternate check to confirm if that post is within that post category.

    You can alter this to the actual names of the categories, and add / remove any as necessary. Hopefully this will help give you a good starting point.

  • Hi Edd,

    thanks, I will try this.

    I don’t use custom post types, every post is a regular post. The category decides the way which template part has to be loaded, so this should work.

    Gonna play around with it now. Thank you so much also for helping me out with beyond-ACF stuff.

    Best Regards
    Chris

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

You must be logged in to reply to this topic.