0

I read this: Display posts of custom type in hierarchical order But didn't understand if and how to use the answer.

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 -->

1 Answer 1

1

From the look of your code, all you are doing is creating a nested list. If so, the easiest thing would be wp_list_pages.

$args = array(
    'post_type'    => 'cliente',
    'post_status'  => 'publish',
    'author' => $idutente, // must be comma separated list of IDs
);
wp_list_pages($args);

wp_list_pages only seems to work with 'hierarchical' => true,, but you have registered your CPT with that argument.

You should be able to style it with CSS however you want.

If you need something more complicated, you may need a custom Walker, something like this:

class My_Page_Walker extends Walker_Page {
  function end_el( &$output, $page, $depth = 0, $args = array() ) {
    $link = get_edit_post_link($page->ID);
    $link = '<a class="post-edit-link" href="' . $link . '">Edit</a>';
    $output .= ' ('.$link.') </li>';
  }
}

$args = array(
  'post_type'=>'cliente',
  'post_status'=> 'publish',
  'walker' => new My_Page_Walker
);
wp_list_pages( $args ); 
4
  • Do you know if it possible to add an EDIT link to each page permalink? Something like: Page (Edit) - Sub (Edit) - Sub2 (Edit) This is what I've got but it echoes an EDIT link before the UL and it is related to my custom homepage. <?php wp_reset_query(); $args = array( 'post_type' => 'cliente', 'post_status' => 'publish', 'title_li' => __(''), 'link_after' => "</a>".edit_post_link('Edit', ' | ', '')."", 'sort_order ' => 'DESC', 'echo' => 1, 'author' => $idutente, // must be comma separated list of IDs ); wp_list_pages($args);?> Commented Jun 24, 2013 at 10:08
  • This is what I actually got: Imgur Commented Jun 24, 2013 at 10:13
  • @Downloadtaky : see the edit
    – s_ha_dum
    Commented Jun 24, 2013 at 13:39
  • Thank you once again. I'm using your walker class and I'm trying to understand why the parent EDIT is after the childs. Imgur Commented Jun 24, 2013 at 14:52

Your Answer

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

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