Template code; custom field suite

Hi. I am using custom field suit where I have set up a relationship field.

All I want to do is create a list of posts with links back to the original post.

<?php
$values = CFS()->get( 'products' );
foreach ( $values as $post_id ) {
    $the_post = get_post( $post_id );
    echo '<p> ' . $the_post->post_title . ' </p>';
}
?>

Above code successfully gets me the post title selected. How do I change this into a clickable link.

  • Dimitris
    • Support Star

    Hello there Cara,

    hope you're doing good today and thanks for joining us! :slight_smile:

    Can you give this a try?

    <?php
    $values = CFS()->get( 'products' );
    foreach ( $values as $post_id ) {
        $the_post = get_post( $post_id );
        echo '<p><a href="'. get_post_permalink( $post_id ) .'">' . $the_post->post_title .'</a></p>';
    }
    ?>

    Reference: https://developer.wordpress.org/reference/functions/get_post_permalink/

    Warm regards,
    Dimitris

  • Cara
    • WPMU DEV Initiate

    Thank you that did work! Now I want to take things a step further.

    Currently if nothing was entered, the following message is returned on the page.

    Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\LSC\wp-content\themes\powerclub\single-recipes.php on line 49

    I am now trying to create a conditional statement so that if nothing was entered, then nothing is returned, otherwise the links as selected.

    I have tried this, but it does not work ...

    <?php
    $values = CFS()->get( 'products' );
    if ($values, true) {
      echo '<h3 class="spacer20">The Following Products are Available from LSC:</h3>';
    foreach ( $values as $post_id ) {
        $the_post = get_post( $post_id );
        echo '<a href="'. get_post_permalink( $post_id ) .'">' . $the_post->post_title .'</a></p>';
      }
    }
    ?>
  • Cara
    • WPMU DEV Initiate

    You are fantastic. Can you help me now get the featured image related to the linked product.

    I am using the following code:

    <?php
                  $values = CFS()->get( 'products' );
                  if ($values) {
                    echo '<div class="product-box">';
                    echo '<h3 class="spacer20">The Following products are available from the lifestyleCHOICE practice:</h3>';
                  foreach ( $values as $post_id ) {
                      $the_post = get_post( $post_id );
                      echo ''. $the_post->the_post_thumbnail('thumb', array( 'class' => 'picture alignleft' ) ) .'';
                      echo '<p><a href="'. get_post_permalink( $post_id ) .'">' . $the_post->post_title .'</a></p>';
                    }
                    echo '</div>';
                  }
                ?>

    I am getting the following error:

    Fatal error: Uncaught Error: Call to undefined method WP_Post::the_post_thumbnail() in C:\xampp\htdocs\LSC\wp-content\themes\powerclub\single-recipes.php:48 Stack trace: #0 C:\xampp\htdocs\LSC\wp-includes\template-loader.php(74): include() #1 C:\xampp\htdocs\LSC\wp-blog-header.php(19): require_once('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\LSC\index.php(17): require('C:\\xampp\\htdocs...') #3 {main} thrown in C:\xampp\htdocs\LSC\wp-content\themes\powerclub\single-recipes.php on line 48

  • Dimitris
    • Support Star

    Please try to replace
    echo ''. $the_post->the_post_thumbnail('thumb', array( 'class' => 'picture alignleft' ) ) .'';
    with
    echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'picture alignleft' ) );
    Ref: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/#comment-314

    Please keep in mind that we're keen on having threads with a specific issue for a specific member at a time, so if you need further assistance on this, please do reach us in our live chat or create separate threads per issue. :slight_smile:
    https://premium.wpmudev.org/live-support/

    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.