Support » Plugin: Custom Post Type UI » Show highest and lowest taxonomy value

  • Resolved Thomas Clausen

    (@tntc1978)


    I use this code to display the amount of players that can play a board game:
    <p><?php echo get_the_term_list( $post->ID, 'players', '<b>Players:</b> ', ', ', '' ); ?></p>

    But I would like to show the highest and lowest value of the Taxonomies called players. Can anyone help with that? The goal is to show this:

    Players: 2-8

    Where the post has all these values in the custom taxonomy: 2,3,4,5,6,7,8

    I have also used a custom field for it, but it doesn’t give the same flexibility in regards to archive listings.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’d try something like this:

    <?php
    $players_list = get_the_terms( get_the_ID(), 'players' );
    $player_names = wp_list_pluck( $players_list, 'name' );
    $range = min( $player_names ) . '-' . max( $player_names );
    echo "Players: $range";
    ?>
    

    The first line handles fetching the terms for the post, but doesn’t do anything for outut. The second line plucks just the term names out of the results from the term list, and the rest just handles grabbing the lowest and highest values out of that array.

    Wow thanks a bunch Michael. Didn’t expect any answer on a Saturday.

    It almost did the trick. The only thing is, that the numbers aren’t linking to the archive listing like other taxonomies. Would like to be able to show all the X player games for instance.

    See all the 3 player games…

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    So you need the text linked to an archive, and you want it to be an archive showing posts from all of the terms? Otherwise, i may need a better idea of what markup/output you’re hoping for.

    It’s just me not explaining it well I think. I just want to be able the click the numbers in the output. So if the output is:

    Players: 2-8

    Then I would want to be able to click the 2 or the 8 taking me to the archive listing of both.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Gotcha, but definitely adds to the complexity as well, especially with each number being its own term and having its own archive. Having them clickable and leading to an archive that lists both, would require amending queries somehow to make the archive show both. Another consideration would be how the URL should appear, which gets into rewrite rule modifications.

    That said, making the 2 clickable and leading to just the 2 archive, and then the same with the 8 being clickable into the 8 archive would be easy-peasy fine.

    It was actually just the easy solution I was looking for. The One taking me to the two different archives respectfully.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Try this version out:

    <?php
    $players_list = get_the_terms( get_the_ID(), 'players' );
    $player_names = wp_list_pluck( $players_list, 'name' );
    $range = sprintf(
    	'<a href="%s">%s</a>-<a href="%s">%s</a>';
    	get_term_link( min( $player_names ), 'players' ),
    	min( $player_names ),
    	get_term_link( max( $player_names ), 'players' )
    	max( $player_names )
    );
    echo "Players: $range";
    ?>
    

    I’ve been toying around with it, but it gives a parse error:

    Parse error: syntax error, unexpected ';', expecting ',' or ')' in /home/www/.../wp-content/plugins/php-code-widget/execphp.php(27) : eval()'d code on line 5

    Can you help me the last yard? Thanks for your patience Michael.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Oops, looks like I typo’d on this line:

    '<a href="%s">%s</a>-<a href="%s">%s</a>';
    

    Needs to be a comma at the end, not a semicolon.

    Fantastic. It was also missing a comma a bit further down. But for others that come this way to see how to do this. Here’s the solution:

    <?php
    $players_list = get_the_terms( get_the_ID(), 'players' );
    $player_names = wp_list_pluck( $players_list, 'name' );
    $range = sprintf(
    	'<a href="%s">%s</a>-<a href="%s">%s</a>',
    	get_term_link( min( $player_names ), 'players' ),
    	min( $player_names ),
    	get_term_link( max( $player_names ), 'players' ),
    	max( $player_names )
    );
    echo "Players: $range";
    ?>

    Michael you are the man! I want to support you back. Where’s the donate button or plugins I can buy to support you?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Gah, so many typos.

    A solid review of the plugin would be donation enough, to be honest πŸ™‚

    Thanks.

    Done πŸ™‚

    Two things to be conscious of:
    1. If I haven’t filled out the values in the custom taxonomy, it gives a bunch of errors
    2. If I only fill out one value (for instance for a two player game), it says:
    2-2 Players

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Show highest and lowest taxonomy value’ is closed to new replies.