0

I made a short code in my plugin that pulls out one of the custom post featured image from one taxonomy term each, the taxonomy term under which the post is in, and two term metas.

I want the term of that taxonomy to be limited to only a certain number of characters e.g. 12 but my code below does not truncate the long name of that term and add an ellipses as i am trying below.

ob_start();
    $post_type = 'esitykset';
    $taxonomy = 'tapahtumat';
    $post_ids = get_unique_term_recent_posts( $post_type, $taxonomy );
    if ( $post_ids ) {
        $args = [
            'post__in' => $post_ids,
            'post_type' => $post_type,
            'posts_per_page' => 8
        ];
        $q = new WP_Query( $args );


         if ( $q->have_posts() ) {
            while ( $q->have_posts() ) {
            $q->the_post();

                ?>

            <div class="home-poster-column">
                <?php
                    $thumb = '';

                    $width = (int) apply_filters( 'et_pb_index_blog_image_width', 724 );

                    $height = (int) apply_filters( 'et_pb_index_blog_image_height', 1024 );
                    $classtext = 'et_pb_post_main_image';
                    $titletext = get_the_title();
                    $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
                    $thumb = $thumbnail["thumb"];
                ?>
                <?php
                    global $term_link;
                        if ( $terms = get_the_terms( $term_link->ID, 'tapahtumat' ) ) {
                            // $term = $terms[0]; // WRONG! $terms is indexed by term ID!
                            $term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
                        }
                ?>
                <a href="<?php echo get_term_link( $term ); ?>">

                    <span class="esitys-poster">
                        <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>

                    </span><br/>
                    <strong>
                    <?php 

                        $event = the_terms( $post->ID, 'tapahtumat'); 

                        $len = 10; // <-- Adjust to your needs!

                        echo mb_strimwidth($event, 0, $len, 'UTF8' ) . '&hellip;'; 
                     ?>
                    </strong> <br/>

                     <?php 
                        /*  
                            Get the date range meta of tapahtumat taxonomy
                            http://wordpress.stackexchange.com/questions/11820/echo-custom-taxonomy-field-values-outside-the-loop 
                        */
                        global $date_range;
                        if ( $terms = get_the_terms( $date_range->ID, 'tapahtumat' ) ) {
                            // $term = $terms[0]; // WRONG! $terms is indexed by term ID!
                            $term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
                            echo get_term_meta( $term->term_id, 'date-range', true ) . '<br/>';
                        }

                        /*  Get the start price meta of tapahtumat taxonomy */
                        global $start_price;
                        if ( $terms = get_the_terms( $start_price->ID, 'tapahtumat' ) ) {
                            // $term = $terms[0]; // WRONG! $terms is indexed by term ID!
                            $term = array_shift( $terms ); // RIGHT! Will get first term, and remove it from $terms array
                            echo get_term_meta( $term->term_id, 'start-price', true );
                        }
                      ?>
                </a>
            </div>

            <?php 
            wp_reset_postdata();    } // endif have_posts()
    } // endif $post_ids 
    $myvariable = ob_get_clean();
    return $myvariable;

I know that the code above is a little twisted. Please feel free to suggest better ways if you feel is better approach.

Thanks

2 Answers 2

2

This is not working because the_terms() is echo-ing the output.

It would make more sense to trim down the lengthy term names, instead of doing it directly on the HTML output of get_the_term_list() that's used by get_the_terms(). That could give unvalid HTML, that could break the layout of your site.

Here's an example for the corresponding theme file:

// Add a filter
add_filter( 'get_the_terms', 'wpse248787_get_the_terms' );

// Display terms
the_terms( get_the_ID(), 'tapahtumat' );

// Remove the filter again
remove_filter( 'get_the_terms', 'wpse248787_get_the_terms' );

where you have defined:

function wpse248787_get_the_terms( $terms )
{
    $len = 10; // <-- Adjust to your needs!

    // Limit term names if needed
    foreach( $terms as $term )
    {
        if( $len > 0 && $len < mb_strlen( $term->name ) )
            $term->name = mb_substr( $term->name, 0, $len - 1, 'UTF8' ) . '&hellip;';
    }

    return $terms;
}

in the functions.php file in the current theme directory. You might also need to addjust the encoding argument or use e.g. get_option( 'blog_charset' ).

2
  • Good idea to use a filter!
    – Emil
    Dec 9, 2016 at 13:52
  • Sorry! I will add more details to my questions above
    – Mayank
    Dec 9, 2016 at 15:17
1

the_terms() will not return anything – it will display the terms and return false or nothing.

Use get_the_term_list() to get the full HTML for the terms to be displayed. Use get_the_terms() to get an array of the terms.
It's hard to understand what you mean by "limit the characters to 12" - do you want to limit each term to 12 characters, or the total string after the terms have been concatenated into a string?

Also, mb_strlen() is not the function you're looking for, it only gives you the length of the provided string, you should be using mb_strimwidth():

string mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker = "" [, string $encoding = mb_internal_encoding() ]] )
3
  • 1
    I wasn't aware of the existence of the mb_strimwidth() function, thanks for sharing that, +1
    – birgire
    Dec 9, 2016 at 13:31
  • I only just discovered it yesterday, and it's brilliant!
    – Emil
    Dec 9, 2016 at 13:51
  • Sorry! I will add more details to my questions above.
    – Mayank
    Dec 9, 2016 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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