Skip to main content
4 of 6
Added one more image
Downloadtaky
  • 382
  • 1
  • 5
  • 19

Custom post type hierarchical loop in Homepage

This what i would like to achieve:

if_user_logged_in

show user's created cpost in hierarchical order

Is it possible to list custom posts in Homepage with this structure?

enter image description here

No problems in backend:

No problems in backend

Customer 1 (parent) the_title() - the_permalink() - edit_post_link()

  • Item 1 belongs to Customer 1 Children of Customer 1 the_title() - the_permalink() - edit_post_link()
  • Item 2 belongs to Customer 1 Children of Customer 1 the_title() - the_permalink() - edit_post_link()
  • Item 3 belongs to Customer 1 Children of Customer 1 the_title() - the_permalink() - edit_post_link()
  • Item 4 belongs to Customer 1 Children of Customer 1 the_title() - the_permalink() - edit_post_link()

Customer 2

  • Item 1 belongs to Customer 2 Children of Customer 2 the_title() - the_permalink() - edit_post_link()
  • Item 2 belongs to Customer 2 Children of Customer 2 the_title() - the_permalink() - edit_post_link()
  • Item 3 belongs to Customer 2 Children of Customer 2 the_title() - the_permalink() - edit_post_link()
  • Item 4 belongs to Customer 2 Children of Customer 2 the_title() - the_permalink() - edit_post_link()

And so on, I think you have the point :)

This is how I registered my cPost:

/*Custom post clienti */
add_action( 'init', 'register_cpt_cliente' );

function register_cpt_cliente() {

    $labels = array( 
        'name' => _x( 'Clienti', 'cliente' ),
        'singular_name' => _x( 'Cliente', 'cliente' ),
        'add_new' => _x( 'Aggiungi nuovo', 'cliente' ),
        'add_new_item' => _x( 'Aggiungi un nuovo cliente', 'cliente' ),
        'edit_item' => _x( 'Modifica cliente', 'cliente' ),
        'new_item' => _x( 'Nuovo client', 'cliente' ),
        'view_item' => _x( 'Guarda cliente', 'cliente' ),
        'search_items' => _x( 'Cerca cliente', 'cliente' ),
        'not_found' => _x( 'Nessun cliente trovato', 'cliente' ),
        'not_found_in_trash' => _x( 'Nessun cliente eliminato', 'cliente' ),
        'parent_item_colon' => _x( 'Cliente superiore:', 'cliente' ),
        'menu_name' => _x( 'Clienti', 'cliente' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Un archivio per gestirli tutti (Cit.)',
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'category', 'post_tag', 'page-category', 'abruzzo', 'basilicata', 'calabria', 'campania', 'emilia-romagna', 'friulli-venezia-giulia', 'lazio', 'liguria', 'lombardia', 'marche', 'molise', 'piemonte', 'puglia', 'san_marino', 'sardegna', 'sicilia', 'toscana', 'trentino-alto_adige', 'umbria', "valle_d'aosta", 'veneto' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => get_template_directory_uri().'/images/cli20.png',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'cliente', $args );
}

This is my loop that, obviusly works but has no hierarchical structure:

<?php if ( is_user_logged_in() ) {;?>
    <div id="listaclienti-container">
        <?php global $current_user;
            get_currentuserinfo();
            $idutente = $current_user->ID;
            $loop = new WP_Query( array( 
               'post_type' => 'cliente',
               'posts_per_page' => -1,
               'author' => $idutente) 
            );?>
        
        <ul id="listaclienti">
            
            <?php if (have_posts()) { ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post();?>
                    <li>
                        <h3>
                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                <?php the_title(); ?><?php edit_post_link('Edit', ' | ', ''); ?>
                            </a>
                        </h3>
                    </li>
                <?php endwhile;
            }else{echo 'No posts here';};?>
        </ul><!-- listaclienti -->
    
    </div><!-- listaclienti Container -->
<?php };?><!-- end of if user_logged_in -->
Downloadtaky
  • 382
  • 1
  • 5
  • 19