Home Forums General Issues How to display a list of links including post names


How to display a list of links including post names

  • Hi,

    I am working on a website build, and when a user creates a new article, they are to have the option of creating a set of manually curated links to other articles on the same site. To accomplish this I am trying to use the page-link field type, which gives the option of selecting multiple pages as links. However the page about this type of field does not explain how to output a list of links rather than just one (see link below) or how to output the page name as the link, rather than displaying the URL.

    I managed to get the links output as a list using the code below, but I still can’t work out how to use the post name instead of displaying the URL. Can anyone help?

        $articles = get_field('list_of_links');
        echo '<ul>';
    	foreach ($articles as $key => $val){
    	echo '<li><a href="'.$val.'">'.$val.'</a></li>';
        echo '</ul>';
  • Hi @jossoway

    I think you’d be much better of using the relationship field! That way your client can search and filter to find the appropriate posts they want to link to.

    Then in your code, to display a simple list of links with the post name you can do something like this:

    <?php $articles = get_field('list_of_links'); ?>
    <?php if( $articles ): ?>
    		<?php foreach( $articles as $article ): ?>
    				<a href="<?php echo get_permalink($article->ID); ?>" title="<?php echo __('Read more about') . ' ' . $article->post_title; ?>">
    					<?php echo $article->post_title; ?>
    		<?php endforeach; ?>
    <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to display a list of links including post names’ is closed to new replies.