0

I would like to create a template to display custom post's terms.

First menu level page :

(Drink - About / LOGO / Questions - Contact )


Section 1 (Drink Page) :

(col-6) -> Image / (col-6) -> Page Presentation Text

Section 2 :

(col-4) -> First Term / (col-4) -> Second Term / (col-4) -> Third Term


On click, i would like to display the first post assigned to it's term.

Exemple :

  • For the first menu level -> www.mysite.com/drinks/ On this page, on 2nd section -> First term (Wine), Second Term (Hard), Third Term (Soft)

On click, by example on Hard, i would like somethink like this -> www.mysite.com/drinks/hard/post-name

--

Currently : - My first menu item, is a PAGE.

When i click on the first term, my url is -> www.mysite.com/custom-post/post-name and not www.mysite.com/first-menu-page/post-name

I would likd to use the same template for multiple page, with multiple custom post..

I a junior developper and it's a little bit hard to explain..

Below, my current code :

<!-- SECTION 1 -->
<section class="main-page ptb-60" id="main-page">
    <div class="container">

        <div class="row">

            <div class="col-xl-7 page-content">
                <!-- Image -->
                <?php 

                $image = get_field('page_image');

                if( !empty($image) ): ?>

                    <img src="<?php echo $image['url']; ?>" class="img-fluid" alt="<?php echo $image['alt']; ?>" />

                <?php endif; ?>
                <!-- Image -->
            </div>

            <div class="col-xl-5">
                <!-- Presentation Text -->
                <h2><?php the_title(); ?></h2>
                <p><?php the_field('page_description'); ?></p>
                <hr>
                <p class="intro-texte pb-20"><?php the_field('page_introduction'); ?></p>
                <p><?php the_field('page_content'); ?></p>
                <!-- Presentation Text -->
            </div>
        </div>

    </div>
</section>
<!-- SECTION 1 -->

<!-- SECTION 2 -->
<section class="sous-pages ptb-60 bg-g" id="sous-pages">
    <div class="container">

        <div class="row ptb-30">
            <div class="col-xl-12 d-flex justify-content-center">
                <h3><?php the_field('section_title'); ?></h3>
            </div>
        </div>

        <div class="row">  


            <?php

                $taxonomy = get_field('taxonomy');

                $terms = get_terms(
                    array(
                        'taxonomy'   => $taxonomy,
                        'hide_empty' => false,
                ));



                if ( ! empty( $terms ) && is_array( $terms ) ) {
                // Run a loop and print them all
                foreach ( $terms as $term ) {

                        $the_query = new WP_Query( array( 
                        "posts_per_page" => 1,
                        "orderby" => 'date', // this is the default
                        "order" => 'DESC', // this is the default
                        "tax_query" => array(
                            array (
                            'taxonomy' => $taxonomy, // use the $tax you define at the top of your script
                            'field' => 'term_id',
                            'terms' => $term->term_id, // use the current term in your foreach loop
                            ),
                        ),
                        ) ); ?>

                        <!--LOOP CONTENT -->
                        <div class="col-xl-4 col-lg-4 col-md-6 col-sm-12 pb-20 cpt-categories">

                                <div class="cat-images">
                                <!-- Image -->
                                <?php 

                                $image = get_field('image_category', $term);

                                if( !empty($image) ): ?>

                                    <img src="<?php echo $image['url']; ?>" class="img-fluid pb-20" alt="<?php echo $image['alt']; ?>" />

                                <?php endif; ?>
                            <!-- Image -->
                            </div>


                            <?php $pageone = get_the_permalink($the_query->posts[0]); ?>

                            <h4><a href="<?php echo $pageone; ?>"><?= $term->name ?></a></h4>
                                <?php echo $cat; ?>
                        </div>
                        <!--LOOP CONTENT -->

            <?php
            } // End foreach
            } // End if 
            ?>

        </div>

    </div>
</section>
<!-- SECTION 2 -->
0

As you can see in The WordPress Template Hierarchy, you must create a taxonomy template.

Basically, you have two options:

1) taxonomy.php: if all terms share the same design.

2) taxonomy-{slug}.php: if each term has its own design (different colors, layout etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.