Support

Account

Home Forums Add-ons Repeater Field How to get title for multiple links(PageLink)

Solved

How to get title for multiple links(PageLink)

  • Hello, this is my first post here(:

    From the code below, You can see that I have Repeater with subfields.
    Everything works as expected, only one part gives me headache.

    Subfield(link) is “Page Link” with “Select multiple values” option selected, so I could add multiple links and it works fine for both languages ( https://mysite.com/en/first-post-title.html https://mysite.com/en/second-post-title.html and so on.. )

    For two days now I’m trying to get post tilte for each link instead of url without any luck :/

    <?php
    if ( have_rows( 'repeater_field' ) ): ?>
    
    <div class="grid">
      <div class="grid-sizer"></div>
    
      <?php while( have_rows('repeater_field') ): the_row(); 
    
    // vars
    $image = get_sub_field('photo');
    $content = get_sub_field('description');
    $links = get_sub_field('link');
    
    ?>
      <article id="post-<?php the_ID(); ?>" class="grid-item <?php the_sub_field('photo_size_class'); ?>">
    
        <div class="blog-image">
    
          <img src="<?php echo $image['url']; ?>" class="img-responsive" alt="<?php echo $image['title'] ?>"/>
    
        </div>
        <div class="content">
    
          <p class="description">
            <?php 
    
    // vars
    
    // check 
    if( $links ): ?>
    
            <?php foreach( $links as $link ): ?>
    
            <a href="<?php echo $link ?>">
              <?php echo get_the_title($link); ?>
            </a>
    
            <?php endforeach; ?>
            <?php endif; ?>
               
                <!-- End for each -->
                
            <?php echo $content; ?>
          </p>
    
          <!-- .entry-content -->
        </div>
      </article>
      <!-- #post-## -->
    
      <?php endwhile; ?>
    </div>
    
    <?php
    endif;
    ?>
  • the “Page Link” field only returns URLs. get_the_title() requires a Post ID as input. You could possibly use this https://codex.wordpress.org/Function_Reference/url_to_postid to get the Post ID, however, the page link field also lets you select archive pages and there is no way in WP to convert these URLs or get the title of the page. The page link field was not really designed to do what you’re trying to do. The Post Object field, or relationship field might be a better choice.

  • Thank You, John. I’ve solved this by changing “Page Link” to “Relationship” and paste my code directly in to my page template(page-ID.php).

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

The topic ‘How to get title for multiple links(PageLink)’ is closed to new replies.