Community Forums › Forums › Archived Forums › General Discussion › PHP Wizrds…help please!
Tagged: php
- This topic has 7 replies, 2 voices, and was last updated 11 years, 7 months ago by
Posh John.
-
AuthorPosts
-
September 3, 2013 at 5:38 am #60390
Posh John
ParticipantI am creating a list (will be of my custom taxonomy) and trying to sort the data. My custom taxonomy is a list of names and i want to list by surname.
I have created a single page template to show my list but i am getting a php error - Missing argument 2 for lastNameSort() - how do i fix this?Here is my code so far:
<?php /* Template Name: All Artists */ ?> <?php add_action( 'genesis_entry_content', 'lastNameSort'); function lastNameSort($a, $b ) { $aLast = end(explode(' ', $a)); $bLast = end(explode(' ', $b)); return strcasecmp($aLast, $bLast); // The array of data (will remove this and use the custom taxonomy once i know it works). $array = array( 27 => 'Sarah Green', 29 => 'Adam Brown', 68 => 'Fred Able' ); uasort($array, 'lastNameSort'); print_r($array); } genesis();
Thanks for your help 🙂
Thus the heavens and the earth were completed in all their vast array. Genesis 2
September 3, 2013 at 6:26 am #60396Gary Jones
MemberChange:
add_action( 'genesis_entry_content', 'lastNameSort');
to:
add_action( 'genesis_entry_content', 'lastNameSort', 10, 2 );
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
September 3, 2013 at 6:37 am #60399Posh John
ParticipantHi Gary,
Thanks for stopping by. I am still getting the error though. Its says:
Warning: Missing argument 2 for lastNameSort() in /home/editions/public_html/wp-content/themes/mytheme-genesis/single-artists.php on line 8
Line 8 is:function lastNameSort($a, $b ) {
Any more ideas are welcome 🙂
Thanks
Thus the heavens and the earth were completed in all their vast array. Genesis 2
September 3, 2013 at 8:53 am #60424Gary Jones
MemberYou've got a callback for lastNameSort inside of the definition of lastNameSort. Try moving the } just near the end, to just before the // line.
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
September 3, 2013 at 1:56 pm #60487Posh John
ParticipantHi Gary,
Firstly, thanks for your help so far.
I've been at this all day now and i have finally reached a eureka moment...and i'm as happy as a pig in sh*t, but, there's one more piece to the puzzle.
i now have my taxonomy listing in surname order but i can't figure out how to make the names link to the correct pages. When i query WP i am only asking for the taxonomy name (otherwise my name-splitter function throws an error). do you have any ideas how i can query the taxonomy and have the results link to their pages?
Here is what i have so far...
<?php /* Template Name: All Artists */ ?> <?php function namesplitter($a, $b) { $aLast = end(explode(' ', $a)); $bLast = end(explode(' ', $b)); return strcasecmp($aLast, $bLast); } add_action( 'genesis_entry_content', 'display_sorted_tax', 10, 2 ); function display_sorted_tax() { $args = array( 'taxonomy' => 'artist', 'fields' => 'names' ); $terms = get_terms('artist', $args); uasort($terms, 'namesplitter'); $count = count($terms); $i=0; if ($count > 0) { $term_list = '<p class="my_term-archive">'; foreach ($terms as $term) { $i++; $term_list .= $term; if ($count != $i) $term_list .= ' · '; else $term_list .= '</p>'; } echo $term_list; } } genesis();
Thus the heavens and the earth were completed in all their vast array. Genesis 2
September 3, 2013 at 2:56 pm #60497Posh John
ParticipantHi again Gary,
Don't worry i've figured it out (although there may probably be a better way!!). Anyway, i added a get_term_by to fetch the details i needed. I know that makes a lot of queries, but it's the best i could figure out with my limited php ability.
See what you think:
<?php /* Template Name: All Artists */ ?> <?php function namesplitter($a, $b) { $aLast = end(explode(' ', $a)); $bLast = end(explode(' ', $b)); return strcasecmp($aLast, $bLast); } add_action( 'genesis_entry_content', 'display_sorted_tax', 10, 2 ); function display_sorted_tax() { $args = array( 'taxonomy' => 'artist', 'fields' => 'names' ); $terms = get_terms('artist', $args); uasort($terms, 'namesplitter'); $count = count($terms); $i=0; if ($count > 0) { $term_list = '<p class="my_term-archive">'; foreach ($terms as $termname) { $term = get_term_by('name', $termname, 'artist'); $i++; $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>'; if ($count != $i) $term_list .= ' · '; else $term_list .= '</p>'; } echo $term_list; } } genesis();
Thus the heavens and the earth were completed in all their vast array. Genesis 2
September 3, 2013 at 6:38 pm #60536Gary Jones
Member<?php /* * Template Name: All Artists */ add_action( 'genesis_entry_content', 'poshjohn_display_artists' ); /** * Display linked artists, ordered by surname. * * @return null Return early if there are no artists. */ function poshjohn_display_artists() { $terms = poshjohn_get_sorted_artists(); // If there's no artists, abort now. if ( ! $terms ) { return; } $count = count( $terms ); $term_list = '<p class="my_term-archive">'; foreach ( $terms as $index => $termname ) { $term = get_term_by( 'name', $termname, 'artist' ); $term_list = sprintf( '<a href=""%s"" title=""%s"">%s</a>', esc_url( get_term_link( $term ) ), esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ), $term->name ); if ( $index < $count ) $term_list .= ' · '; else $term_list .= '</p>'; } echo $term_list; } /** * Get the list of artists and sort them. * * @return array. List of artists, sorted by surname. May be empty array of no artists found or taxonomy does not exist. */ function poshjohn_get_sorted_artists() { $args = array( 'taxonomy' => 'artist', 'fields' => 'names', ); $terms = get_terms( 'artist', $args ); // If artist taxonomy does not exist, play nice with functions that expect an array. if ( is_wp_error( $terms ) ) return array(); // Sort names by surname uasort( $terms, 'poshjohn_sort_by_surname_callback' ); return $terms; } /** * Callback for array sorting. * * For $a and $b, we split the string by a space, then take the last portion of each, and do a case insensitive * binary-safe string comparison. * * @param string $a * @param string $b * * @return string Return < 0 if $a is less than $b; > 0 if $a is greater than $b, and 0 if they are equal. */ function poshjohn_sort_by_surname_callback( $a, $b ) { $aLast = end( explode( ' ', $a ) ); $bLast = end( explode( ' ', $b ) ); return strcasecmp( $aLast, $bLast ); } genesis();
That's how I would do it - minor refactoring to make the code easier to read, follow code standards, and have functions do only one task if possible. You might need to adjust that conditional that adds the middot, with a +1 added on to $count or something like that.
WordPress Engineer, and key contributor the Genesis Framework | @GaryJ
September 4, 2013 at 4:36 am #60585Posh John
ParticipantHi Gary,
Thanks for that Gary. Even though i got it to work, it's fantastic to see how it is done "properly", so i really appreciate the time you took to show me this.
I will now study your code and go off and research the terms you used to try and learn why you did it the way you did...this is invaluable to me learning to code better.
Thanks again for taking the time to provide this.
Kind regards
John.
Thus the heavens and the earth were completed in all their vast array. Genesis 2
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.