• Resolved Fangstrom

    (@fangstrom)


    This question is also posted on the uproot forum.

    I have been away from the development of pages for a while but now I’m back and more rusty than ever ??

    Literally I love the relation function but wonder how in the world to do to create a link (href) to the item that it is related to.
    The whole thing works just great, it′s just the link that is the problem, i have used the sample code (“Template Usage”) that can be found on the cfs site.

    As always, absolutely awesome plugin!

    Best regards/fangstrom

    https://www.ads-software.com/plugins/custom-field-suite/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    A relationship field returns an array of post IDs.

    You just need to loop through the array, passing each post ID through WP’s get_permalink() function.

    E.g.

    $posts = (array) CFS()->get( 'your_field' );
    foreach ( $posts as $post_id ) {
        echo '<div>' . get_permalink( $post_id ) . '</div>'; // outputs each URL
    }
    
    Thread Starter Fangstrom

    (@fangstrom)

    Brilliant!

    Did some mods to the code and now it works exactly as i wanted it to.
    This is what i ended up with ^^

    <?php if ('' != $cfs->get('my_relationship')) : ?>
    			<h4>Header</h4>
    			<?php
    				$posts = (array) CFS()->get( 'my_relationship' );
    				foreach ( $posts as $post_id ) {
    				$the_post = get_post($post_id);
    					echo '<a href="' . get_permalink( $post_id ) . '">' . $the_post->post_title . '</a><br/>';
    				}
    			?>
    		<?php endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Relationship linkage’ is closed to new replies.