0

I'm using a shortcode to display a custom post type. The shortcode contains an attribute to display only posts of a specific category within the custom post type e.g. [faq category="travel"]

For each shortcode wordpress outputs a specific set of html, including a div with id of "faq-accordion". For each shortcode I need to output the div with ID "faq-accordion+1" e.g "faq-accordion1", "faq-accordion2", "faq-accordion3", etc.

I have managed to append a number to each div but for some reason it only goes as far as 6 and then repeats the numbers.

Here is my code

    add_shortcode( 'faq', 'wpse105856_shortcode_callback' );

    function wpse105856_shortcode_callback( $atts ) {
    extract( shortcode_atts( array(
        'category' => ''
    ), $atts ) );

    $args = array(
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'faq'
    );

      if ( ! empty( $category ) ) {
        $args['category_name'] = $category;
    };

    $posts = get_posts( $args );

    //Here is where I am trying to dynamically add a new number to the faq-accordion div
    $num = 0;

        foreach ( $posts as $post ) {   
    $faq  = '<div id="faq-accordion'.++$num.'">'; //Open the container
        };

    foreach ( $posts as $post ) { // Generate the markup for each Question
        $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    };
    $faq .= '</div>'; //Close the container

    return $faq; //Return the HTML.
};
1
  • 1
    Hey check if you have specified the correct code here, in the above code you are looping "foreach ( $posts as $post )" and then again foreach ( $posts as $post ), that would generate totally wrong markup
    – Kumar
    Jul 10, 2013 at 10:42

1 Answer 1

0

the code should be instead

add_shortcode( 'faq', 'wpse105856_shortcode_callback' );

function wpse105856_shortcode_callback( $atts ) {
extract( shortcode_atts( array(
    'category' => ''
), $atts ) );

$args = array(
    'numberposts' => -1,
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post_type' => 'faq'
);

if ( ! empty( $category ) ) {
    $args['category_name'] = $category;
};

$posts = get_posts( $args );

//Here is where I am trying to dynamically add a new number to the faq-accordion div
$num = 0;
$faq = '';
$faq = '<div class="question-wrapper">';//wrapper div for all the questions
//Generate div for each question
foreach ( $posts as $post ) {   
    $faq  .= '<div id="faq-accordion'.++$num.'">'; //Open the container
    // Generate the markup for each Question
    $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
        $post->post_title,
        wpautop($post->post_content)
    );
    $faq .= '</div>'; //Close the container
}
$faq .= '</div>';//close the wrapper

return $faq; //Return the HTML.
};

As you commented, for your need in comment you can use $post->ID in div id like

echo '<div id="'.$post->ID.'">';
4
  • Ok, I see how having foreach twice would cause an error...however using the above code generates multiple <div id="question-wrapper"> even after changing the 3rd last line from "$faq .= '<div class=question-wrapper>';//close the wrapper" to "$faq .= '</div>';//close the wrapper" Jul 10, 2013 at 11:24
  • I've fixed the typo "</div>", well that isn't possible (multiple wrapper divs) untill you are doing the shortcode multiple times. can you post the whole code and output code here
    – Kumar
    Jul 10, 2013 at 11:29
  • ah yes of course...I'm a fool. I need to use the shortcode multiple times so of course the numbers are going to be repeated. I've been doing this all wrong. I need each faq-accordion div on the page to have a unique number...perhaps if I appended the faq-accordian with the category ID....it's different from my original question but some help would be greatly appreciated. Jul 10, 2013 at 11:38
  • in that case simply append post id to your div, that would create different ids, to give the proper answer I need your flow details like what you have and what you want to do with it
    – Kumar
    Jul 10, 2013 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.