首页中自定义贴子式分层循环

时间:2013-06-23 作者:Downloadtaky

我读到:Display posts of custom type in hierarchical order 但不知道是否以及如何使用答案。

这就是我想要实现的目标:

if\\u user\\u logged\\u登录

按层次顺序显示用户创建的cpost

是否可以使用此结构在主页中列出自定义帖子?

enter image description here

后端没有问题:

No problems in backend

客户1(父级)the\\u title()-the\\u permalink()-edit\\u post\\u link()

第1项属于客户1的客户1子项\\u title()-第3项属于客户1的客户1子项\\u title()-第3项属于客户1的客户1子项\\u permalink()-第4项属于客户1的客户1子项-\\u permalink()-编辑\\u post\\u链接()客户2

第1项属于客户2的客户2子女第3项属于客户2的客户2子女第4项属于客户2的客户2子女-\\u permalink()-edit\\u post\\u link()等等,我想你有道理:)

以下是我注册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 );
}
这是我的循环,显然有效,但没有层次结构:

<?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 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

从代码的外观来看,您所做的只是创建一个嵌套列表。如果是这样,最简单的事情就是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 似乎只适用于\'hierarchical\' => true,, 但你已经用那个论点注册了你的CPT。

您应该能够根据需要使用CSS对其进行样式化。

如果您需要更复杂的东西,您可能需要一个定制助行器,如下所示:

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

结束

相关推荐

Child Pages Loop

如果有人能帮忙那就太好了。我找到了一段我使用过的代码,它基本上完成了我希望它完成的任务,列出了父级的子页面,如果有缩略图,则添加缩略图,并添加了自定义摘录。然而,问题是我不能添加个人<?php post_class(); ?> 对于循环中的div,它使用来自父级的类并对子页重复。<?php $child_pages = $wpdb->get_results(\"SELECT * FROM $wpdb->posts WHERE post_parent = \".$pos