admin管理员组

文章数量:1122826

So I have two ACF fields. A text field author_name and an url field author_link. The url field is intended to wrap around the text field to make the links clickable. The staff member would enter this information in the backend of each Wordpress post in order to show the name of the author of the piece of literature and link to a page with other works by this author. Both the text and url fields are in the same ACF field group.

I managed to figure out a few components to this code including how to sort it alphabetically based on the last name and how to prevent duplicate entries from appearing on the generated list. Those parts are working.

However, I'm now stumped on how to associate the URL link with the names of the authors. It seems that using the implode method removes the URL's attachment from the text. I also must put endwhile; before implode or else it outputs strangely.

Here is my code currently:

   <ul>
   <?php 
   if ( have_posts() ) {        

    $query = new WP_Query($args);
    

    while ($query->have_posts()): $query->the_post();

    $author_array[] = get_field('author_name');
    $author_name = get_field ('author_name') ;
    $author_link = get_field( 'author_link' );
    $args = array (
   'post_type' => 'post', // post type
   'posts_per_page' => 20, // grab all the posts
   'meta_key' => 'author_name', 
   'meta_compare' => 'EXISTS', // make sure the post have this acf value
   'orderby'           => 'meta_value',
   'order'   => 'ASC',
);
    
$do_not_duplicate = $post->ID;
$author_array[] = get_field('author_name');
$author_link = get_field( 'author_link' );

// Creates an array 

$author_arrays = array_unique($author_array);


// Remove duplicate entries;

usort ($author_arrays,
function ($a,$b) {
$a = substr(strrchr($a, ' '), 1);
$b = substr(strrchr($b, ' '), 1);
return strcmp($a, $b);
});
    
    echo '<li><a href="' . $author_link . '">';
    
    endwhile;
    
    echo implode( '<li></li>', $author_arrays) ;
    
    
    echo '</a></li>';


wp_reset_postdata();
}
    
?>

    </ul>
    

Here is an image of what I am trying to get:

As you can see, it displays and sorts properly. However, you will find that the links are not properly appended when you click on it.

Any help is greatly appreciated!

本文标签: functionsAdvanced Custom Fields in WPQuery Clickable Text in List Format