• Resolved Sam – The Wild Initiative

    (@thewildinitiative)


    I’ve created a post type of “guest_appearance” which has a Relationship field named “listening_platforms” (in a field group of “podcast_info” if it matters). The Relationship field references the “platform_info” post type, which has fields of “platform_name” and “platform_url” (in a field group of “platform_info” if it matters).

    I’m trying to display an unordered list of each of the Platform Names, linking to the corresponding Platform URL using the following shortcode. While it’s displaying the correct number of list items, each list item is just displayed text of “[listening_platforms.field=’platform_name’]” and links to “[listening_platforms.field=’platform_url’]”.

    How can I fix this?

    [pods name="guest_appearance"]
    <ul>
      [each listening_platforms]
        <li><a href="[listening_platforms.field='platform_url']" target="_blank">[listening_platforms.field='platform_name']</a></li>
      [/each]
    </ul>
    [/pods]

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Paul Clark

    (@pdclark)

    [pods name="guest_appearance"]
    <ul>
      [each listening_platforms]
        <li><a href="{@platform_url}" target="_blank">{@platform_name}</a></li>
      [/each]
    </ul>
    [/pods]
    Thread Starter Sam – The Wild Initiative

    (@thewildinitiative)

    @pdclark Unfortunately, that shortcode doesn’t display anything on any of the pages. I ended up writing a PHP script to do the work and in concert use the shortcode [podcast_platforms].

    function podcast_platforms_shortcode($atts) {
        // Get the podcast platforms for the current post
        $post_id = get_the_ID();
        $platforms_pod = pods('guest_appearance', $post_id);
        $platforms = $platforms_pod->field('podcast_platforms');
    
        // Check if platforms exist
        if (!empty($platforms)) {
            ob_start(); // Start output buffering
    
            echo '<ul class="podcast-platforms-list">';
    
            foreach ($platforms as $platform) {
                $platform_info = pods('platform_info', $platform['ID']); // Assuming 'ID' is the ID of the related platform post
                $platform_name = $platform_info->field('platform_name');
                $platform_url = $platform_info->field('platform_url');
    
                if ($platform_name && $platform_url) {
                    echo '<li><a href="' . esc_url($platform_url) . '" target="_blank">' . esc_html($platform_name) . '</a></li>';
                }
            }
    
            echo '</ul>';
    
            return ob_get_clean(); // Return the buffered output
        }
    
        return ''; // Return empty string if no platforms found
    }
    add_shortcode('podcast_platforms', 'podcast_platforms_shortcode');
    
    Plugin Support Paul Clark

    (@pdclark)

    Okay that’s odd but glad you found the alternative approach.

    One reason may be that the OP and suggested template reference relationship field “listening_platforms”, but the revised shortcode function is referencing “podcast_platforms”.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using Shortcode to Display Series of Fields from Relationship Field’ is closed to new replies.