PHP Wizrds…help please!

Community Forums Forums General Discussion PHP Wizrds…help please!

This topic is: not resolved

Tagged: 

This topic contains 7 replies, has 2 voices, and was last updated by  Posh John 1 year, 3 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #60390

    Posh John
    Participant
    Post count: 42

    I 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

    #60396

    Gary Jones
    Moderator
    Post count: 693

    Change:

    add_action( 'genesis_entry_content', 'lastNameSort');

    to:

    add_action( 'genesis_entry_content', 'lastNameSort', 10, 2 );


    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ

    #60399

    Posh John
    Participant
    Post count: 42

    Hi 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

    #60424

    Gary Jones
    Moderator
    Post count: 693

    You’ve got a callback for lastNameSort inside of the definition of lastNameSort. Try moving the } just near the end, to just before the // line.


    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ

    #60487

    Posh John
    Participant
    Post count: 42

    Hi 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 .= ' &middot; '; else $term_list .= '</p>';
        }
        echo $term_list;
    }
    
    }
    
    genesis();

    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    #60497

    Posh John
    Participant
    Post count: 42

    Hi 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 .= ' &middot; '; else $term_list .= '</p>';
    	}
        echo $term_list;
    }
    
    }
    
    genesis();

    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    #60536

    Gary Jones
    Moderator
    Post count: 693

    [php]
    <?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(
    %s‘,
    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();
    [/php]

    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.


    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ

    #60585

    Posh John
    Participant
    Post count: 42

    Hi 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

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.