• I’m creating a single page. I want each section to have an id, starting with 1. Here’s what I have now:

    <?php
    $args = array(
        'sort_order' => 'ASC',
        'sort_column' => 'menu_order', //post_title
        'hierarchical' => 1,
        'exclude' => '',
        'child_of' => 0,
        'parent' => -1,
        'exclude_tree' => '',
        'number' => '',
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish'
    );
    $pages = get_pages($args);
    //start loop
    foreach ($pages as $page_data) {
        $content = apply_filters('the_content', $page_data->post_content);
        $title = $page_data->post_title;
        $slug = $page_data->post_name;
    ?>
    <div id="<?php echo $i++; ?>" class='<?php echo "$slug" ?>'><a name='<?php echo "$slug" ?>'></a>
            <h1><?php echo "$title" ?></h1>
                <?php echo "$content" ?>
    </div>
    
    <?php } ?>

    The only problem is, that it starts to count with the second section, not the first. The ID of the first section is empty. Here’s the output:

    <div id=""></div>
    <div id="1"></div>
    <div id="2"></div>
    ...

    What have I done wrong?

  • The topic ‘Single page count posts id’ is closed to new replies.