最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Template for displaying CPTTaxonomy URL issues

matteradmin16PV0评论

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/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/drinks/hard/post-name

--

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

When i click on the first term, my url is -> www.mysite/custom-post/post-name and not www.mysite/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 -->

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/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/drinks/hard/post-name

--

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

When i click on the first term, my url is -> www.mysite/custom-post/post-name and not www.mysite/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 -->
Share Improve this question asked Mar 19, 2019 at 14:36 WDCreativWDCreativ 32 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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).

Post a comment

comment list (0)

  1. No comments so far