Ask your WordPress questions! Pay money and get answers fast! (more info)

Create Shortcode from Custom Field Loop WordPress

  • REFUNDED

This is my code to loop custom fields (using Advanced Custom Fields/Repeater Add On) and create a shortcode for it called [accordion]. The syntax seems okay in that it doesn't break my functions file, but it's not working. Does anyone know what I did wrong?
http://www.advancedcustomfields.com/docs/functions/has_sub_field/#usage

What I'm trying to do is run the suggested loop from the ACF documentation above with a shortcode. If you go to the link it will take you to the loop I'm supposed to use, I just need the user to be able to run that within the post content with [accordion]

function accordiontabs_content_shortcode($atts, $content = null){
if( get_field('accordion_tabs')):
while(has_sub_field('tab_title')):
echo '<h3>';
the_sub_field('tab_content');
echo '</h3>';
echo '<div>';
the_sub_field('tab_content');
echo '</div>';
endwhile;
endif;
}
add_shortcode('accordion', 'accordiontabs_content_shortcode');

Answers (3)

2012-12-12

Arnav Joy answers:

try this to check if message gets printed or not
function accordiontabs_content_shortcode($atts, $content = null){

echo 'test msg for accordian<br />';

if( get_field('accordion_tabs')):

while(has_sub_field('tab_title')):

echo '<h3>';

the_sub_field('tab_content');

echo '</h3>';

echo '<div>';

the_sub_field('tab_content');

echo '</div>';

endwhile;
else :
echo 'ACF problem';
endif;


}

add_shortcode('accordion', 'accordiontabs_content_shortcode');


amycarolyn comments:

This outputs 'test msg for accordion'.


Arnav Joy comments:

it means that shortcode is working but the if else part is not , try to echo something outside endif then check it any thing outputs then it is ok other wise you are having some problem in if else part


amycarolyn comments:

Okay, thanks for helping me troubleshoot, you are probably on the right track, but I have no idea how to fix the problem. I raised the prize money hoping someone can give me the correct code.


Arnav Joy comments:

try this

<?php

function accordiontabs_content_shortcode($atts, $content = null){

if(function_exists('get_field')) :
if( get_field('accordion_tabs')):

while(has_sub_field('tab_title')):

echo '<h3>';

the_sub_field('tab_content');

echo '</h3>';

echo '<div>';

the_sub_field('tab_content');

echo '</div>';

endwhile;

endif;
else:
echo 'function for ACF not exists';
endif;

}

add_shortcode('accordion', 'accordiontabs_content_shortcode');
?>

2012-12-12

Navjot Singh answers:

Try this

function accordiontabs_content_shortcode($atts, $content = null){
global $post;
$post_id = $post->ID;
if( get_field('accordion_tabs')):
while(has_sub_field('tab_title',$post_id)):
echo '<h3>';
the_sub_field('tab_title');
echo '</h3>';
echo '<div>';
the_sub_field('tab_content');
echo '</div>';
endwhile;
endif;
}
add_shortcode('accordion', 'accordiontabs_content_shortcode');


amycarolyn comments:

Thank you for your response. When I use this there is no output.


Navjot Singh comments:

Try this

function accordiontabs_content_shortcode($atts, $content = null){
global $post;
$post_id = $post->ID;
if( get_field('accordion_tabs',$post_id)):
while(has_sub_field('tab_title',$post_id)):
echo '<h3>';
the_sub_field('tab_title');
echo '</h3>';
echo '<div>';
the_sub_field('tab_content');
echo '</div>';
endwhile;
endif;
}
add_shortcode('accordion', 'accordiontabs_content_shortcode');


amycarolyn comments:

That doesn't seem to work either, I was hoping I just made a dumb mistake.

2012-12-12

phppoet answers:

first put global variable post and settings before this code so that page can get settings from your settings group

<?php
global $your_settings; //replace with your settings options for custom filed
gloabl $post;

$post_id = $post->ID;

function accordiontabs_content_shortcode($atts, $content = null){

if( get_field('accordion_tabs')):

while(has_sub_field('tab_title')):

echo '<h3>';

the_sub_field('tab_content');

echo '</h3>';

echo '<div>';

the_sub_field('tab_content');

echo '</div>';

endwhile;

endif;

}

add_shortcode('accordion', 'accordiontabs_content_shortcode');
?>


then test it by echoing text like 'hello world'; and check . please update post if you get any error


amycarolyn comments:

I don't understand what I would add for your_settings.


phppoet comments:

you need to add group settings name on which script saves your data . go to phpmyadmin , select your database , and open wp_options table and find the table in which your data stored . you just need to put that table name .


if this data is being stored as meta then you can try this code
<?php
function accordiontabs_content_shortcode($atts, $content = null){

global $post;

$tabs1 = get_post_meta( $post->ID, 'accordion_tabs', true );

$tabs2 = get_post_meta( $post->ID, 'tab_title', true );

if( get_field($tabs1):

while(has_sub_field($tabs2)):

echo '<h3>';

the_sub_field($tabs2);

echo '</h3>';

echo '<div>';

the_sub_field($tabs1);

echo '</div>';

endwhile;

endif;

}

add_shortcode('accordion', 'accordiontabs_content_shortcode'); ?>


amycarolyn comments:

Sorry, this is over my head and both sets of code broke the site when I added it to the functions file.