• Resolved one3rdnerd

    (@one3rdnerd)


    Are there any code examples using a shortcode?

    I am using Beaverbuilder (like Elementor) and want to pull the data into my template with a shortcode.

    I would attempt it myself but I can’t really tell what code to use after looking at the plugin accordion as parts of it are in a < pre > box and parts aren’t. It’s sort of scrambled.

    Is there a web page with code examples?

    This is how far I have got

    /* Repeater Fields */
    function press_shortcode() {
    global $post;
    $presspub = pods('press_pub', $post->ID);
    echo $presspub->display('publication_name');
    print_r( pods_field('press_pub', $post->ID, 'publication_name') );
    print_r( pods_field('press_pub', $post->ID, 'publication_logo') );
    }
    add_shortcode('pressbox', 'press_shortcode');`
    
    • This topic was modified 3 years, 4 months ago by one3rdnerd.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter one3rdnerd

    (@one3rdnerd)

    @codingpanda Any thoughts? ??

    Plugin Author Coding Panda

    (@codingpanda)

    Hi @one3rdnerd I tried to build an Element accordion widget but it did not work well so it is not completed.

    Anyway, there is no problem building a shortcode to output the repeat field data. Please use pods_field to fetch the data. I tested your shortcode and pods_field worked fine. ??

    Thread Starter one3rdnerd

    (@one3rdnerd)

    @codingpanda

    I couldn’t get it to work that way but the below shortcode works…

    
    /* Repeater Fields */
    function press_shortcode($atts) {
    	global $post;
    	ob_start();
    $pressarr = pods_field('team_member', $post->id, 'press');
    if(!empty($pressarr)) {
      echo "<h2>Press Mentions</h2>";
    }
    foreach($pressarr as $val) {
      echo "<div class='advisor-press-item'>";
      $img_atts = wp_get_attachment_image_src($val['publication_logo'], 'full');
      echo "<a class='img-link' target='_blank' href='" . $val['pub_link'] . "'><img src='" . $img_atts[0] . "' alt='' /></a>";
      echo "<div class='pub-link'>" . $val['name'] . "</div>";
      echo "<a class='press-link' target='_blank' href='" . $val['pub_link'] . "'>" . $val['pub_article_title'] . "</a><br>";
      echo "</div>";
    }
    	return ob_get_clean();
    }
    add_shortcode('pressbox', 'press_shortcode');

    Then just add the shortcode [pressbox] in the Beaver Builder or Elementor template.

    Pat K

    (@blackcapdesign)

    one3rdnerd: thanks for sharing this! Most helpful.

    For anyone who’s trying to add a repeater field using the templates that are built into PODS, this extends the shortcode function; tested & worked for me!

    1. Edit/customize and paste the function below into the functions.php file in your custom theme to output an array including ALL fields. Why? It outputs a list of all the fields needed to edit the Repeater Fields function (the second function listed below). Note: change “name-of-the-pods-table-as-repeater-field” to the name shown in the Fields tab in your Pods Admin screen.

    // Use this to output an array including ALL fields 
    function repeater_shortcode() {
    global $post;
    echo '<pre>';
    print_r( pods_field('name-of-the-pods-table-as-repeater-field'));
    echo '</pre>';
    }
    add_shortcode('repeater', 'repeater_shortcode');
    // end function

    2. Paste the shortcode [repeater] into the template window in PODS (and save).
    3. Open the page on the front end, and copy and paste the output into a text file, so you have a list of all the available fields – shown in square brackets.
    4. Comment out or remove the function above. Then paste in and edit/customize this new function:

    /* Repeater Fields */
    function repeater_shortcode($atts) {
    	global $post;
    	ob_start();
    $repeater = pods_field('name-of-the-pods-table-as-repeater-field');
    if(!empty($repeater)) {
      echo "<h2 class='repeater-heading'>This Optional Subheading Appears Above the Repeater Fields</h2>";
    }
    foreach($repeater as $val) {
    	echo "<div class='repeater-block-container'>";
      echo "<h3 class='available-field-1'>" . $val['available-field-1'] . "</h3>";
    	echo "<p class='available-field-2'>" . $val['available-field-2'] . "</p>";
    	echo "<p class='available-field-3'>" . $val['available-field-3'] . "</p>";	
    	echo "</div>";
    }
    	return ob_get_clean();
    }
    add_shortcode('repeater', 'repeater_shortcode');
    // end function

    5. Make sure to customize ‘name-of-the-pods-table-as-repeater-field’ (same as you did for the first function) and replace the ‘available-fields’ with the ones shown in square brackets output from the first function. The HTML and class names can be modified as needed.
    6. The shortcode is the same, so you don’t need to do anything other than save and upload functions.php

    Hope this helps if you’re trying to get repeater fields working with the built-in PODS templates…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Output with Shortcode’ is closed to new replies.