Tagged: 

Viewing 22 posts - 1 through 22 (of 22 total)
  • Author
    Posts
  • #1319069

    Hey, this is in continuation of a ticket i opened ages ago. We ended up with the below code to display custom taxonomies on the postslider.php template.

    get_the_term_list( $the_id, $taxonomy, ”, ‘, ‘, ” ) . ‘ ‘;

    problem is that it dumps all the taxonomies into a list of href links, and i need to be able to distinguish between them by adding a class or id to each one based on the term name or slug. How can this e achieved?

    thanks

    #1319337

    Hey w_archer,

    Thank you for the inquiry.

    Try to use the get_the_terms function instead and run the returned results in a loop. You can go over the items one at a time, check for the name of the current item and do something with it. An example can be found in the documentation.

    // https://developer.wordpress.org/reference/functions/get_the_terms/

    Best regards,
    Ismael

    #1319812

    Thanks for the response Ismael,
    The example given says i need to add the taxonomy term_id, is that the same as the slug?

    And the postslider.php file adds everything to the $cats variable to make it show on the front end. So to add the ‘price’ taxonomy i would do this?
    $cats .= wp_cache_add( $post->ID, $term_ids, $taxonomy . ‘price’ );

    Here is the whole code i added, but it seems to break the whole website so i had to remove it.

    function get_the_terms( $post, $taxonomy ) {
    $post = get_post( $post );
    if ( ! $post ) {
    return false;
    }

    $terms = get_object_term_cache( $post->ID, $taxonomy );
    if ( false === $terms ) {
    $terms = wp_get_object_terms( $post->ID, $taxonomy );
    if ( ! is_wp_error( $terms ) ) {
    $term_ids = wp_list_pluck( $terms, ‘price’ );
    $cats .= wp_cache_add( $post->ID, $term_ids, $taxonomy . ‘price’ );
    }
    }

    #1319814

    also tried the below code which seems to add a number 1 after the other terms on the front end, but only on the first product:

    if( ! empty( $title ) || in_array( $show_meta_data, array( ‘always’, ‘on_empty_title’ ) ) )
    {
    if( true )
    {
    $taxonomies = get_object_taxonomies( get_post_type( $the_id ) );
    $cats = ”;
    $excluded_taxonomies = array_merge( get_taxonomies( array( ‘public’ => false ) ), array(‘post_tag’,’post_format’, ‘show’, ‘seller’, ‘selleremail’, ‘show_wanted’, ‘price’) );
    $excluded_taxonomies = apply_filters( ‘avf_exclude_taxonomies’, $excluded_taxonomies, get_post_type( $the_id ), $the_id );
    $terms = get_object_term_cache( $post->ID, $taxonomy );
    $term_ids = wp_list_pluck( $terms, ‘price’ );

    if( ! empty( $taxonomies ) )
    {
    foreach( $taxonomies as $taxonomy )
    {
    if( ! in_array( $taxonomy, $excluded_taxonomies ) )
    {
    $cats .= get_the_term_list( $the_id, $taxonomy, ”, ‘, ‘, ” ) . ‘ ‘;
    $cats .= wp_cache_add( $post->ID, $term_ids, $taxonomy . ‘price’ );

    }

    }
    }

    #1319834

    Hi,

    Thank you for the update.

    Why are you using the wp_cache_add function? Looks like you are using the actual definition of the get_the_terms function. Usage examples of the get_the_terms function are located at the bottom of the documentation.

    Example:

    $terms = get_the_terms( get_the_ID(), 'on-draught' );
    
    if ( $terms && ! is_wp_error( $terms ) ) : 
    
        $draught_links = array();
    
        foreach ( $terms as $term ) {
            $draught_links[] = $term->name;
        }
    
        $on_draught = join( ", ", $draught_links );
        ?>
    <p class="beers draught">
            <?php printf( esc_html__( 'On draught: <span>%s</span>', 'textdomain' ), esc_html( $on_draught ) ); ?>
    
    <?php endif; ?>
    

    Best regards,
    Ismael

    #1319839

    ok i see, sorry i dont know php at all and i am fumbling through without a clue. I have taken the example you sent and replaced the the terms with myown, however is still don’t know hoe to incorporate it into the $cats variable:

    $terms = get_the_terms( get_the_ID(), ‘price’ );
    if ( $terms && ! is_wp_error( $terms ) ) :
    $price_links = array();
    foreach ( $terms as $term ) {
    $price_links[] = $term->name;
    }
    $price = join( “, “, $price_links );
    ?>
    <p class=”price”>
    <?php printf( esc_html__( ‘price: <span>%s</span>’, ‘textdomain’ ), esc_html( $price ) ); ?>
    </p>
    <?php endif; ?>

    I tired pasting the above into several places of the postslider.php file but it just causes fatal errors everywhere and breaks the whole site. Unfortunately its not as straight forward as a page template.

    #1320074

    Hi,

    Try to look for this block of code around line 815 in the postslider.php file.

    if( $show_meta )
    					{
    						$taxonomies  = get_object_taxonomies( get_post_type( $the_id ) );
    						$cats = '';
    						$excluded_taxonomies = array_merge( get_taxonomies( array( 'public' => false ) ), array( 'post_tag', 'post_format' ) );
    						$excluded_taxonomies = apply_filters( 'avf_exclude_taxonomies', $excluded_taxonomies, get_post_type( $the_id ), $the_id );
    
    						if( ! empty( $taxonomies ) )
    						{
    							foreach( $taxonomies as $taxonomy )
    							{
    								if( ! in_array( $taxonomy, $excluded_taxonomies ) )
    								{
    									$cats .= get_the_term_list( $the_id, $taxonomy, '', ', ', '' ) . ' ';
    								}
    							}
    						}
    
    						if( ! empty( $cats ) )
    						{
    							$meta_out .= '<span class="blog-categories minor-meta">';
    							$meta_out .=	$cats;
    							$meta_out .= '</span>';
    						}
    					}
    

    Replace it with:

    if( $show_meta )
    					{
    						$taxonomies  = get_object_taxonomies( get_post_type( $the_id ) );
    						$cats = [];
    						$excluded_taxonomies = array_merge( get_taxonomies( array( 'public' => false ) ), array( 'post_tag', 'post_format' ) );
    						$excluded_taxonomies = apply_filters( 'avf_exclude_taxonomies', $excluded_taxonomies, get_post_type( $the_id ), $the_id );
    
    						if( ! empty( $taxonomies ) )
    						{
    							foreach( $taxonomies as $taxonomy )
    							{
    								if( ! in_array( $taxonomy, $excluded_taxonomies ) )
    								{
    									$cats[] = get_the_terms( $the_id, $taxonomy );
    								}
    							}
    						}			
    						if( ! empty( $cats ) )
    						{
    							$cat_walk = '';
    							$cat_class = 'stray';
    
    							foreach($cats[0] as $cat) {
    								if(in_array($cat->name, array("kitty", "pussy", "catty"))) {
    									$cat_class = "pets";
    								}
    								$cat_walk .= "<a class='". $cat_class . "' href='" . get_term_link($cat->term_id) . "'>" . $cat->name . "</a>";
    							} 
    
    							$meta_out .= '<span class="blog-categories minor-meta">';
    							$meta_out .=	$cat_walk;
    							$meta_out .= '</span>';
    						}
    					}
    

    This line checks if the term name is in the array using the in_array function and change the class accordingly.

    if(in_array($cat->name, array("kitty", "pussy", "catty"))) {
    									$cat_class = "pets";
    								}
    

    Best regards,
    Ismael

    #1320142

    Thanks Ismael,

    That makes sense, but the problem now is that its only showing one taxonomy – the brand. All the other are no longer visible.

    Another problem is that its using the brand name rather then the taxonomy name. For example it looking for a brand called brand1 and giving it the class i have assigned, but its ignoring brand2 etc. So if i apply this to the product price, I cant give a class to all prices, only one particular number?

    #1320610

    Hi,

    Thank you for the update.

    Try to replace this line..

    foreach($cats[0] as $cat) {
    								if(in_array($cat->name, array("kitty", "pussy", "catty"))) {
    									$cat_class = "pets";
    								}
    								$cat_walk .= "<a class='". $cat_class . "' href='" . get_term_link($cat->term_id) . "'>" . $cat->name . "</a>";
    							} 
    
    

    with..

    foreach($cats as $terms) {
    	foreach($terms as $term) {
    		if(in_array($term->name, array("kitty", "pussy", "catty"))) {
    			$cat_class = "pets";
    		}
    		$cat_walk .= "<a class='". $cat_class . "' href='" . get_term_link($term->term_id) . "'>" . $term->name . "</a>";
        }
    } 
    
    

    The changes above should work for all custom post types and taxonomies.

    Best regards,
    Ismael

    #1320630

    It seems that now all the taxomonies have a class of stray.

    I change this:
    if(in_array($term->name, array(“kitty”, “pussy”, “catty”))) {
    $cat_class = “pets”; }
    to be:

    if(in_array($term->name, array(“price”, “Price”))) {
    $cat_class = “price”; }

    so that it matched the taxonomy im looking at. However its still has ‘stray’ as a class instead of ‘price;

    #1320641

    Hi,

    You have to check for the term name, not the taxonomy name. What is the name of the custom taxonomy and what are the terms or categories inside that particular taxonomy?

    Best regards,
    Ismael

    #1320841

    The custom post is called ‘Advertisements’ and there are a bunch of taxonomies. The one that i am most interested in customizing is called ‘Price’ and i don’t really know what you mean about terms? each product has a different price?

    Essentially its a custom post with a price associated with it. I need that to show up on the postslider.php when i list all the posts.

    #1321335

    Hi,

    d i don’t really know what you mean about terms?

    Terms are items inside a specific taxonomy. The default post category is an example of a taxonomy where you can create different terms. Please check the documentation for more info.

    // https://wordpress.org/support/article/taxonomies/

    We are not really sure how your taxonomies or custom post types are set up but using taxonomy for prices is not really recommended. You should attach a price to a post using custom fields instead of taxonomy terms.

    Best regards,
    Ismael

    #1321564

    Hey i originally tried adding it using a custom field, but how can i then add the custom field to the postslider.php file?
    i have tried:

    $yourname = get_post_meta($post->ID, ‘your-name’, false);
    if( count( $your-name ) != 0 ) { ?>
    <?php foreach($yourname as $yourname) {
    echo ‘<div class=”test”>’.$yourname.'</div>’;
    } ?>
    <?php
    } else {
    // do nothing;
    }

    But again i don’t know how to add this to the postslider.php file

    #1321565

    sorry was logged in with wrong account

    #1321578

    Hi,

    If you want to place the price after the excerpt, this filter should work.

    add_filter("avf_post_slider_entry_excerpt", function(excerpt, $prepare_excerpt, $permalink, $entry) {
        $price = get_post_meta($entry->ID, "price", false);
        $excerpt .= "<span class='av-price'>". $price ."</span>";
        return $excerpt;
    }, 10, 4);
    

    If it has to be placed, under the post title, look for this code around line 892.

    $output .=  "<{$heading} class='slide-entry-title entry-title {$css}' $markup><a href='{$link}' title='" . esc_attr( strip_tags( $title ) ) . "'>{$title}</a></{$heading}>";
    

    Below, add the custom field.

     $price = get_post_meta($entry->ID, "price", false);
     $output .= "<span class='av-price'>". $price ."</span>";
    

    Best regards,
    Ismael

    #1321856

    It seems that now every post has ‘Array” just under the title. The placement is good but i don’t know why they all say ‘Array’.

    I added a custom field called price and a value. Also tried with a capital P but same result.

    Do i need to use ACF to do this? im just using the built in custom field options.

    Also am i still supposed to add the first chunk of code you added? where does that go? everywhere i put it i get the error “syntax error, unexpected ‘,’ expecting t variable’. If i save it anyway the website crashes.

    #1322107

    Hi,

    Thank you for following up.

    You do not need the previous modification because we are now retrieving the price using a custom field. And ACF is not required for this either, the default custom field option should be enough.

    We may need to access the file server in order to check the shortcode template properly. Please post the WP and FTP info in the private field. A link to the actual page where you need this modification to display will also help.

    Best regards,
    Ismael

    #1322155

    ok thanks. details below

    #1322192

    Hi,

    Thank you for the info.

    We revert the modifications in the postslider.php file and added the adjusted filter in the functions.php file. We are now retrieving the “bladeprice” custom field instead of “price”. This is the updated filter.

    
    function avf_post_slider_entry_excerpt_mod($excerpt, $prepare_excerpt, $permalink, $entry) {
    	$post_id = $entry->ID;
    	$blade_price = get_post_meta($post_id, "bladeprice", true);
    
    	if ($blade_price && get_post_type($post_id) == "advertisements") {
    		$excerpt = "<span class='av-price'>USD ". $blade_price . "</span>
    " . $excerpt;
    	}
    
        return $excerpt;
    }
    add_filter("avf_post_slider_entry_excerpt", "avf_post_slider_entry_excerpt_mod", 10, 4);
    

    Screenshot: https://imgur.com/AQkLp3k

    Best regards,
    Ismael

    #1322425

    Perfect! Thanks Ismael, i was a bit worried about this one.

    Thanks for the help

    #1322886

    Hi,

    You are welcome! Please let us know in another thread if you need anything else.

    Have a nice day.

    Best regards,
    Ismael

Viewing 22 posts - 1 through 22 (of 22 total)
  • The topic ‘add class to get_the_term_list’ is closed to new replies.