Putting the newly registered taxonomy to good use

Hire a WordPress Expert on Codeable
Updated On: May 10th, 2019 0 Comments

In the last lesson, we have already created and assigned the terms of “dosth_review_source” taxonomy to the “Reviews” post type. If you did not, it is time. Do it now.

First, let’s do a bit of a cleanup.

We no longer need the “Review” ACF field group we have created for our “Reviews” post type. So, let’s go ahead and just delete it.

If you now visit the Reviews archive page or Homepage, you will still see the Source of the review.

But if you go the admin dashboard and inside the individual review, you can no longer find the review “Source” field.

They are not really gone

The problem with custom fields is, Although we deleted them visually from the admin dashboard, they still exist in the database.

Be it default fields of WordPress or Fields of ACF, all the custom fields are still stored inside the wp_postmeta table of site’s database.

This is happening because we deleted the ACF field group but not the Source value from an individual review.

So, they still exist in the database and the following code inside our template files still outputs the Review “Source” value.

<span class="review-from">
    <?php printf( __( 'from %s', 'nd_dosth' ), get_field('source') ); ?>
</span>

You can remove the fields completely by using the PHPMyAdmin or the steps mentioned in the following article:

https://sumtips.com/blogging/add-delete-edit-custom-fields-wordpress/

Let’s replace the ACF code with get_the_terms()

Anyway, let’s modify the above line of code to output the Review Source using the custom taxonomy instead of the custom field.

Open up the reviews-slider.php file inside the “parts” directory of our theme.

nd-dosth/parts/reviews-slider.php

and replace the “review-from” span element with the following code:

<span class="review-from">
    <?php $terms = get_the_terms( get_the_ID() , 'dosth_review_source' ); ?>
    <?php printf( __( 'from %s', 'nd_dosth' ), $terms[0]->name ); ?>
</span>

Nothing much happening in the above code.

<?php $terms = get_the_terms( get_the_ID() , 'dosth_review_source' ); ?>

The get_the_terms() function returns a particular taxonomy’s terms assigned to a particular post. This is why we are specifying both Post ID and Taxonomy name.

In our case, we are getting the “dosth_review_source” taxonomy terms that are assigned to a particular review and then we are outputting the first term’s name.

<?php printf( __( 'from %s', 'nd_dosth' ), $terms[0]->name ); ?>

We are interested in outputting only one review source no matter how many review sources a particular review is assigned to.

If you want to display all the taxonomy terms assigned to a particular post, you should loop through the resulted terms from get_the_terms() function like this:


$terms = get_the_terms( $post->ID , 'taxonomyname' );
foreach ( $terms as $term ) {
    echo $term->name;
}

Get it?

Anyway, Here is the final reviews-slider.php file for this lesson:


<?php 
    $reviews_query = new WP_Query(
        array(
            'post_type' => 'dosth_reviews',
            "posts_per_page" => 4,
            'order_by' => 'menu_order'
        )   
    );
?>
<!-- reviews custom query loop -->
<?php if( $reviews_query->have_posts() ): ?>
    <section class="reviews-container make-it-slick">
        <h2><?php _e( 'What People Are Saying', 'nd_dosth' ); ?></h2>
        <div class="nd-dosth-reviews">
            <?php while( $reviews_query->have_posts() ): ?>
                <?php $reviews_query->the_post(); ?>
                <div class="review">
                    <blockquote>
                        <?php the_content(); ?>
                        <footer>
                            <cite><?php the_title(); ?></cite>
                            <span class="review-from">
                                <?php $terms = get_the_terms( get_the_ID() , 'dosth_review_source' ); ?>
                                <?php printf( __( 'From %s', 'nd_dosth' ), $terms[0]->name ); ?>
                            </span>
                        </footer>
                    </blockquote>
                </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </div>
    </section>
<?php endif;  ?>
<!-- end of reviews custom query loop -->

If you now open check out the reviews section on the Homepage, Everything looks same and working fine.

Common, let’s do the same for the archive-dosth_reviews.php file.

Here is final archive-dosth_reviews.php file for this lesson:


<?php
/**
 * The template for displaying archive of Reviews
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Dosth
 */
get_header();
?>
<div class="content-container">
    <h1 class="page-title"><?php post_type_archive_title(); ?></h1>    
    <div class="reviews-container">
        <div class="nd-dosth-reviews">
            <?php if ( have_posts() ): ?>
                <?php while( have_posts() ): ?>
                    <?php the_post(); ?>
                    <div class="review">
                        <blockquote>
                            <?php the_content(); ?>
                            <footer>
                                <cite><?php the_title(); ?></cite>
                                <span class="review-from">
                                    <?php $terms = get_the_terms( get_the_ID() , 'dosth_review_source' ); ?>
                                    <?php printf( __( 'From %s', 'nd_dosth' ), $terms[0]->name ); ?>
                                </span>
                            </footer>
                        </blockquote>
                    </div>
                <?php endwhile; ?>
                <?php the_posts_pagination(); ?>
            <?php else: ?>
                <p><?php _e( 'No Reviews found', 'nd_dosth' ); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>

In the next lesson, we will build an archive template file for the terms of our custom taxonomy.

Leave a Reply

Your email address will not be published. Required fields are marked *