Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building a custom post type, where I have a little trouble generating the ID:s the way I want to. The idea of the post class is to generate a table with ID and post title. I wonder if it is possible to generate unique ID depending on the amount of posts in the class, starting from 1. My plan with the post class or type is that it will be used as a FAQ where the title launches a boostrap modal with the answer.

Here is the relevant code:

function cptModal_frontend($atts){
$id = rand(0, 999); // use a random ID so that the CSS IDs work with multiple on one page
$args = array( 'post_type' => 'cptModal', 'orderby' => 'menu_order', 'order' => 'ASC');
$loop = new WP_Query( $args );
$modals = array();
while ( $loop->have_posts() ) {
    $loop->the_post();
    if ( '' != get_the_title() ) {
        $title = get_the_title( get_the_ID());
        $content = get_the_excerpt();
        $modals[] = array('title' => $title, 'content' => $content);
    }
}
if(count($modals) > 0){
    ob_start();
    ?>
    <!--Table start-->

    <table class="table table-hover table-bordered">
    <thead>
          <tr>
            <th>#</th>
            <th>Fråga:</th>
          </tr>
    </thead>
    <tbody>
        <?php foreach ($modals as $key => $title) { ?>
        <tr>
          <td><?php echo $id;?></td>
          <td><a href="#cptmodal_<?php echo $id; ?>" data-toggle="modal"><?php echo $title['title'];?></a></td>
        </tr>
        <?php } ?>
    </tbody>
    </table>    
<!--Table end-->
<!--modal start-->
<?php foreach ($modals as $key => $title) { ?>
<div id="cptmodal_<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel"><?php echo $title['title'];?></h3>
</div>
<div class="modal-body">
<p><?php $title['content'] ?></p>
</div>
<div class="modal-footer">
<button class="btn btn-custom" data-dismiss="modal" aria-hidden="true">Stäng</button>
</div>
</div>
<?php } ?>
<!--Modal end-->
    <?php }
$output = ob_get_contents();
ob_end_clean();

// Restore original Post Data
wp_reset_postdata();    

return $output;

Complete code:

// Custom Post Type Setup
add_action( 'init', 'cptModal_post_type' );
function cptModal_post_type() {
$labels = array(
    'name' => 'Modals',
    'singular_name' => 'Modal',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New modal',
    'edit_item' => 'Edit modal',
    'new_item' => 'New modal',
    'view_item' => 'View modal',
    'search_items' => 'Search modals',
    'not_found' =>  'No modal',
    'not_found_in_trash' => 'No modal found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'FAQ Modals'
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => false,
    'show_ui' => true, 
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'page',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 21,
    'supports' => array('title','excerpt', 'page-attributes')
); 
register_post_type('cptModal', $args);
}

// FRONT END

// Shortcode
function cptModal_shortcode($atts, $content = null) {
// Set default shortcode attributes
$defaults = array(
    'backdrop' => 'true',
    'keyboard' => 'true',
    'show' => 'true'
);

// Parse incomming $atts into an array and merge it with $defaults
$atts = shortcode_atts($defaults, $atts);

return cptModal_frontend($atts);
}
add_shortcode('FAQ_modal', 'cptmodal_shortcode');
// Display latest WftC
function cptModal_frontend($atts){
$id = rand(0, 999); // use a random ID so that the CSS IDs work with multiple on one page
$args = array( 'post_type' => 'cptModal', 'orderby' => 'menu_order', 'order' => 'ASC');
$loop = new WP_Query( $args );
$modals = array();
while ( $loop->have_posts() ) {
    $loop->the_post();
    if ( '' != get_the_title() ) {
        $title = get_the_title( get_the_ID());
        $content = get_the_excerpt();
        $modals[] = array('title' => $title, 'content' => $content);
    }
}
if(count($modals) > 0){
    ob_start();
    ?>
    <!--Table start-->

    <table class="table table-hover table-bordered">
    <thead>
          <tr>
            <th>#</th>
            <th>Fråga:</th>
          </tr>
    </thead>
    <tbody>
        <?php foreach ($modals as $key => $title) { ?>
        <tr>
          <td><?php echo $id;?></td>
          <td><a href="#cptmodal_<?php echo $id; ?>" data-toggle="modal"><?php echo $title['title'];?></a></td>
        </tr>
        <?php } ?>
    </tbody>
    </table>    
<!--Table end-->
<!--modal start-->
<?php foreach ($modals as $key => $title) { ?>
<div id="cptmodal_<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel"><?php echo $title['title'];?></h3>
</div>
<div class="modal-body">
<p><?php $title['content'] ?></p>
</div>
<div class="modal-footer">
<button class="btn btn-custom" data-dismiss="modal" aria-hidden="true">Stäng</button>
</div>
</div>
<?php } ?>
<!--Modal end-->
    <?php }
$output = ob_get_contents();
ob_end_clean();

// Restore original Post Data
wp_reset_postdata();    

return $output;
}

?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Just take the post ID. It's already unique.

// In the loop
printf(
    "<tag>%s-{$post->post_title}</tag>",
    get_the_ID()
);

If you need it to go from 1-x, where it has to start with 1, then simply use:

// In the Loop:
// Assign "1" only to the first post
0 === $post->current_post AND $id = 1;

Then just go and build an array of integers before the Loop:

// An array of integers that have the range starting from 2 
// until the max amount of posts reduced by 1 for the first post
$id_array = range( 2, $GLOBALS['wp_query']->post_count -1 );

With array_rand() you'll be able to retrieve some sort of random key from it.

// Before the Loop
$rand_keys = array_rand( $id_array, count( $id_array ) );

// In the Loop
$id = 0 !== $post->current_post ? $id_array[ $rand_keys[ $post->current_post ] ] : 1;
printf(
    "<tag>%s-{$post->post_title}</tag>",
    $id
);

You could as well use shuffle() to mix the range() result.

share|improve this answer
    
Cool man! Il try it out! –  StenW Aug 3 '13 at 14:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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