PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 12, 2022

[FIXED] wrap each custom post taxonomy term within an li - using a foreach loop

 April 12, 2022     custom-post-type, taxonomy, taxonomy-terms, wordpress     No comments   

Issue

I'm trying to display a number of custom post taxonomy terms - but each within their own specific list item.

I have a site which is about garages. (developing on localhost so don't have a link yet).

I have a custom post type called Cars. Within this I have a custom post taxonomy with the make of the Car 'Ford' in this case.

Within 'Ford' is a list of custom post taxonomy terms of all the ford cars at that garage. 'GT', 'Sierra', 'Orion'.

Instead of listing the terms: 'GT', 'Sierra', 'Orion'. I want to show a picture of the car within a ul list.

I've created a sprite with all the images and want to loop through these setting the background position for each li item.

The first lot of code below displays a list of of the terms which is all good, but not what I want. Right at the bottom is the code which I've tried to add the list items to but just getting a blank screen...

Any help, much appreciated. Thanks

<?php if ( get_post_type() == cars ) { ?>
        <div class="entry-meta-custom">
            <?php
            $terms = get_the_terms( $post->ID, 'ford' );

            if ( $terms && ! is_wp_error( $terms ) ) : 

                 $draught_links = array();

             foreach ( $terms as $term ) {
                 $draught_links[] = $term->name;
             }

              $on_draught = join( ", ", $draught_links );
              ?>

             <?php echo $on_draught; ?>

         <?php endif; ?>
     </div><!-- .entry-meta-custom -->
 <?php } ?>

And here's where I've tried to add the list items.

<?php if ( get_post_type() == cars ) { ?>
        <div class="entry-meta-custom">     
            <?php
            $terms = get_the_terms( $post->ID, 'ford' );

            if ( $terms && ! is_wp_error( $terms ) ) : 

                $draught_links = array();

            foreach ( $terms as $term ) {

                if ($term->name == 'gt') { 
                    $term->name = '<li class="gt">' . $term->name . '</li>';
                 }
                 if ($term->name == 'sierra') { 
                    $term->name = '<li class="sierra">' . $term->name . '</li>';
                 }
                 if ($term->name == 'orion') { 
                    $term->name = '<li class="orion">' . $term->name . '</li>';
                 }
                $draught_links[] = $term->name;
            }

            $on_draught = join( ", ", $draught_links );
            ?>

            <?php echo '<ul>' . $on_draught . '</ul>; ?>

        <?php endif; ?>
    </div><!-- .entry-meta-custom -->
<?php } ?>

Solution

I worked it out. Below is the code for anyone that is looking to replace custom post taxonomy list terms with individual images which are link to the term archive.

Source http://codex.wordpress.org/Function_Reference/get_term_link

<?php if ( get_post_type() == cars ) { ?>
    <div class="entry-meta-custom">     
        <?php

        $terms = get_terms('ford');

        echo '<ul>';

        foreach ($terms as $term) {
            echo '<li class="'.$term->name.'"><a href="'.get_term_link($term->slug, 'ford').'"></a></li>';
        }

        echo '</ul>'; ?>

    </div><!-- .entry-meta-custom -->
<?php } ?>


Answered By - fidev
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,260,416

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing