Wordpress按类别和子类别划分帖子类型

时间:2017-11-27 10:33:01

标签: wordpress categories

我希望按类别和子类别(如果存在)获取菜单。 我在functions.php中声明我的自定义帖子类型并在主题上运行它。

我的function.php代码:

function register_lekarze(){
$labels = array(
    'name'               => _x( 'Lekarze', 'post type general name', '__freshview__' ),
    'singular_name'      => _x( 'Lekarze', 'post type singular name', '__freshview__' ),
    'menu_name'          => _x( 'Lekarze', 'admin menu', '__freshview__' ),
    'name_admin_bar'     => _x( 'Lekarze', 'add new on admin bar', '__freshview__' ),
    'add_new'            => _x( 'Dodaj nowego', '__freshview__' ),
    'add_new_item'       => __( 'Dodaj nowego', '__freshview__' ),
    'new_item'           => __( 'Nowy', '__freshview__' ),
    'edit_item'          => __( 'Edytuj', '__freshview__' ),
    'view_item'          => __( 'Zobacz', '__freshview__' ),
    'all_items'          => __( 'Wszyscy', '__freshview__' ),
    'search_items'       => __( 'Szukaj', '__freshview__' ),
    'parent_item_colon'  => __( 'Nadrzędny:', '__freshview__' ),
    'not_found'          => __( 'Nie znaleziono.', '__freshview__' ),
    'not_found_in_trash' => __( 'Nie znaleziono w koszu.', '__freshview__' )
);

$args = array(
    'labels'             => $labels,
    'description'        => __( 'Lekarze', '__freshview__' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => true,
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => 4,
    'supports'           => array( 'title', 'thumbnail', 'page-attributes' ),
    'menu_icon'          => 'dashicons-id'
);

register_post_type( 'lekarze', $args );



register_taxonomy("categories", array("lekarze"), 
    array(
        "hierarchical" => true,
        "label" => "Kategorie",
        "singular_label" => "Kategoria",
        "rewrite" => array( 'slug' => 'kategoria', 'with_front'=> true )));

} add_action(' init',' register_lekarze');

我的代码:

        <?php 
        $args = array (
            'post_type' => 'lekarze',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'tax_query' => array(
                array (
                    'taxonomy' => 'categories',
                    'field' => 'slug',
                    'terms' => 'lekarze',
                )
            ),
        );


        $query = new WP_Query( $args );
        if ( $query->have_posts() ) {
            $i=1;
            while ( $query->have_posts() ) {
                $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                $query->the_post();
                $fields = get_fields();
                if($actual_link == get_the_permalink()){
                    $class = 'current';
                }else{
                    $class= '';
                }
                ?>

                <li class="<?php echo $class;?>"><a href="<?php echo get_the_permalink();?>"><?php echo get_the_title(); ?></a></li>


                <?php 
                $i++;
            }
            wp_reset_postdata();
        }

        ?>  

我想要的是在菜单中得到这样的东西:

Lekarze(医生 - &gt;神经病学家 - &gt;医生姓名和永久链接名单)

2 个答案:

答案 0 :(得分:0)

尝试将自定义分类法添加到自定义帖子类型,以便进行分类和子分类。

https://codex.wordpress.org/Function_Reference/register_taxonomy

答案 1 :(得分:0)

我设法做这样的事情:

缺少的是我的子类别中的帖子列表,如何获取帖子(在li class =“doctors-post”之后)? 我的代码在这里:

            <?php
        $terms = get_terms( array(
            'taxonomy' => 'categories',
            'hide_empty' => false,
            'order'     => ASC,
            'parent' => 0
        ) );                
        foreach( $terms as $term ) {
            $myList = get_field("glowny_tytul", $term);
            $slug = $term->slug;
            $name = $term->name;
            $id = $term->term_id;
            ?> 
            <ul class="menu-list">
                <img class="menu-list-img" src="<?php the_field('tlo', $term); ?>">
                <li class="menu-list__categories"><a href="<?php echo get_term_link($id); ?>"><?php echo $name ?></a>

                    <ul class="submenu-list">
                        <?php
                        $subterms = get_terms( array(
                            'taxonomy' => 'categories',
                            'hide_empty' => false,
                            'order'     => ASC,
                            'parent' => $id
                        ) );                
                        foreach( $subterms as $subterm ) {
                            $submyList = get_field("glowny_tytul", $subterm);
                            $subslug = $subterm->slug;
                            $subname = $subterm->name;
                            $subid = $subterm->term_id;
                            ?> 
                            <li class="doctors-post"><a href="<?php echo get_term_link($subid); ?>"><?php echo $subname ?></a>


                                <!-- Maybe ajax when switching li -->

                            </li>

                            <?php
                        }
                        ?>  
                    </ul>   
                </li>
            </ul>
            <?php
        }
        ?>