0

I am currently using this code to display the final child in a hierarchical taxonomy. For example, a post tagged 20th Century > 1990s > 1994 should ultimately only show 1994.

The code below works for most Parent/Child groups except for ones that end with 0s and have a child of 0. For example, 20th Century > 1990s > 1990 outputs 1990s (and not 1990).

I think the problem is that array() is using an alphanumerical method that outputs 20xxxx, 1990, 1990x. Thus thinking the final child is 1990s (not 1990).

Is there a way to ignore letters in an array? Or is there a better order to use than array()?

  <?php

    $terms = get_the_terms( $post->ID, 'From' );

        if ( !empty( $terms ) ) {
            $output = array();
            foreach ( $terms as $term ){
                if( 0 != $term->parent )
                    $output[] = '<a href="' . get_term_link( $term ) .'">' . $term->name . '</a>';
            }

                if( count( $output ) )
                echo '<p><b>' . __('','From') . '</b> ' . end($output) . '</p>';
            }
    ?>

If you need, you can also preview my site here: dev.jamesoclaire.com The first post shows "2010s" when instead it should show "2010"

ddxv
  • 23
  • 5
  • You can use REGEX to filter out any characters you don't want or only include characters you do want. – Quixrick Feb 13 '14 at 21:44

2 Answers2

0

You can use explode to explode each item separated by a > to put them into an array. Then you can use array_pop to get the last item of the array.

http://us3.php.net/array_pop

Finally, if necessary, you can filter out the characters that you do not wish to have in your string.

$string = '20th Century > 1990s > 1994';

// EXPLODE THE ITEMS AT THE GREATER THAN SIGN    
$timeline_items = explode('>', $string);

// POP THE LAST ITEM OFF OF THE ARRAY AND STORE IT IN A VARIABLE
$last_item = trim(array_pop($timeline_items));

To answer your question directly, you can strip out any non-numeric characters by using Regular Expressions.

$string = preg_replace('/[^0-9 >]/i', '', $string);

But that may not give you exactly what you are looking for. For example:

$string = '20th Century > 1990s > 1994';
$string = preg_replace('/[^0-9 >]/i', '', $string);
print $string;

Will give you:

20 > 1990 > 1994
Quixrick
  • 3,190
  • 1
  • 14
  • 17
  • I tried inserting `$terms = preg_replace('/[^0-9>]/i', '', $terms);` into the code but unfortunately $terms is an object and can't be converted to a string. I thought that if I could put in preg_replace somewhere near the end of my code, perhaps using $output that this might be a string, but this also did not work. I'll keep looking for an answer, thank you for this detailed and explanatory post. – ddxv Feb 13 '14 at 23:04
  • Your string should be `$terms->name` that you can operate on. But what may be easier for what you are trying to do is this. Since you are only wanting the last item, just make a variable and assign the `terms->name` to it. Each time it iterates over the loop, it will overwrite the previous entry stored in that variable. So when the loop is finished, your variable will only contain the item that you want. Under your `$output[]` line, add something like this: `$term_name = $term->name;` Then you can just print `$term_name` and have the value you want without having to loop over the array. – Quixrick Feb 13 '14 at 23:21
  • I tried both of your suggestions but due to my novice level I haven't had success. **1** I unsuccessfully inserted at each possible step `$terms = preg_replace('/[^0-9>]/i', '', $terms->name);` **2** `$term_name = $term->name`outputs the highest parent, which leads me to think running through the terms at all – ddxv Feb 14 '14 at 01:04
0

A friend helped me realize I was unfortunately asking the wrong question. What I needed to do was choose the correct term when I ran get_the_terms. Since I wanted to display the year only I excluded any $term->name which were not 4 characters long.

<?php
        $terms = get_the_terms( $post->ID, 'From' );
if ( !empty( $terms ) ) {
    foreach ( $terms as $term ){
        if( ( 0 != $term->parent ) and ( strlen($term->name) == 4 ) )
            $output = '<a href="' . get_term_link( $term ) .'">' . $term->name . '</a>';
    }

    if ( !empty( $output ) )
        echo '<p><b>' . __('','From') . '</b> ' . $output . '</p>';
}
?>
ddxv
  • 23
  • 5