自定义帖子类,根据帖子数量生成从1到x的唯一ID?

时间:2013-08-03 作者:StenW

我正在构建一个自定义的post类型,在生成ID:s时遇到了一些困难。post类的思想是生成一个具有ID和post标题的表。我想知道是否有可能根据类中的帖子数量生成唯一的ID,从1开始。我对post类或类型的计划是,它将被用作常见问题解答,其中标题将启动一个带有答案的boostrap模式。

以下是相关代码:

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;
完整代码:

// 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;
}

?>

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

就拿帖子ID吧。它已经是唯一的了。

// In the loop
printf(
    "<tag>%s-{$post->post_title}</tag>",
    get_the_ID()
);
如果需要从1-x开始,那么必须从哪里开始1, 然后简单地使用:

// In the Loop:
// Assign "1" only to the first post
0 === $post->current_post AND $id = 1;
然后在循环之前构建一个整数数组:

// 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 );
使用array_rand() 您将能够从中检索某种随机键。

// 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
);
你也可以使用shuffle() 混合range() 后果

结束

相关推荐

Pagination and multiple loops

对,所以我现在有一个使用css网格将页面分成三部分的页面。第三列是侧栏,前两列各有一个要显示的帖子查询,并创建一个漂亮的帖子网格(虚拟内容):http://puu.sh/2Xh9o.jpg每个循环如下所示: <?php query_posts(\'showposts=5\'); ?> <?php $posts = get_posts(\'numberposts=5&offset=0\'); foreach ($posts as $post) : start_wp()