0

I can't find answer to my question, just hope you will find it relevant.

I'm working a magazine website and I need to display names of contributors in a list by family names. The contributors have been created by a custom taxonomy. Some of those names have more than one First names.

Example

S

Jane Gabriella Maria Sanchez
John Smith

So what I did is that I created a custom field for the family name. It works and put them in the right order. Here's my code the I created with the help of some resource here. The only thing I would like to be able to do now is to query only by the first letter of the get_field('family_name', $term). To been able to group them on my page. A, B, C, D.....

            <?php 

            $terms = get_terms('contributors');

            $args = array('contributors' => $term->slug);
            $query = new WP_Query( $args );


            $order_terms = array();
            foreach( $terms as $term ) {
            $position = get_field('family_name', $term);
            $order_terms[$position] ='<li><a href="'. get_bloginfo( 'url' ) . '/contributors/' . $term->slug . '">'.$term->name.'</a></li>';
            }

            ksort($order_terms);

            foreach( $order_terms as $order_term ) {
            echo $order_term;
            }

            ?>

Maybe it's not possible, let me know.

Thanks in advance.

2 Answers 2

1

Hope the below code block will help you. Please read the comments carefully. The code block-

// Your previous code.
// Say this is your $oder_terms variable
$order_terms = array(
    'Sanchez'   => 'Sanchez Link',
    'Smith'     => 'Smith Link',
    'Dramatist' => 'Dramatist Link',
    'Rashed'    => 'Rashed Link',
    'Munez'     => 'Munez Link',
    'James'     => 'James Link',
    'Jacky'     => 'Jacky Link',

);

ksort($order_terms);
// After ksort($order_terms); we get below array
/*
Array
(
    [Dramatist] => Dramatist Link
    [Jacky] => Jacky Link
    [James] => James Link
    [Munez] => Munez Link
    [Rashed] => Rashed Link
    [Sanchez] => Sanchez Link
    [Smith] => Smith Link
)
*/
// Now we need to group them on the basis of alphabet. Right ?

$new_order_terms = array();
foreach($order_terms as $key => $value) {
    $firstLetter = substr($value, 0, 1);
    $new_order_terms[$firstLetter][$key] = $value;
}

// Now if you do print_r($new_order_terms); your output will be
/*
Array
(
    [D] => Array
        (
            [Dramatist] => Dramatist Link
        )

    [J] => Array
        (
            [Jacky] => Jacky Link
            [James] => James Link
        )

    [M] => Array
        (
            [Munez] => Munez Link
        )

    [R] => Array
        (
            [Rashed] => Rashed Link
        )

    [S] => Array
        (
            [Sanchez] => Sanchez Link
            [Smith] => Smith Link
        )

)
*/
// All grouped by their first letter.
7
  • If this helps you please accept the answer. @francois
    – CodeMascot
    Commented Feb 15, 2017 at 18:28
  • No, it didn't. Sorry for this. My message went away before I could finish edit it. Here it is completed: Hi @the_dramatist, thanks for this solution. I don't understand exactly all the part of it. The $order_terms = array() that you created, is it done dynamically? In fact, what I try to achieve must be updated automatically and it must looks like that, a version I hardcoded till I could find a solution: [link] (thecairoreview.com/contributors)
    – François
    Commented Feb 15, 2017 at 18:35
  • Please read the comments on the code blocks. You'll have full understanding.
    – CodeMascot
    Commented Feb 15, 2017 at 18:37
  • By $order_terms I created a sample array with some names and links. And showed you the demo.
    – CodeMascot
    Commented Feb 15, 2017 at 18:39
  • What I don't understand is why we don't use 'get_field("family_name", $term)' to find the first letter? The 'terms' contain the first name and the family name so I can't use that value to sort my contributors. No?
    – François
    Commented Feb 15, 2017 at 18:55
0

So I finally been able to solve my problem by using two fields that I added to my custom taxonony called "Contributors". One for the admin of the site to write the family name of each contributor and a field to tell in which letter group it belong.

I'm sure there would be easier way to code this but, with my limited nowledge in coding, that's the best I could do.

Here's my code:

            <!-- Letter A -->

            <?php 

            $terms = get_terms(array(
            'taxonomy' => 'contributors',
            'meta_key' => 'letter_group',
            'meta_value' => 'a'
            ));

            if(!empty ($terms)) {

            $args = array(
            'contributors' => $term->slug,  
            );
            $query = new WP_Query( $args ); ?>

            <section class="names">

            <h3>A</h3>
            <p>
            <?php

            $order_terms = array();
            foreach( $terms as $term ) {
            $position = get_field('family_name', $term);
            $order_terms[$position][] ='<a href="'. get_bloginfo( 'url' ) . '/contributors/' . $term->slug . '">'.$term->name.'</a><br>'; }

            ksort($order_terms);

            foreach( $order_terms as $a ) {
            foreach($a as $order_term)
            echo $order_term;}

            wp_reset_postdata();

            ?>

            </p>
            </section>
            <?php
            }
            ?>

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.