如何在给定的可过滤库中显示我的自定义帖子类型?

时间:2017-05-09 13:57:48

标签: wordpress photo-gallery

  1. 以下是我想要显示自定义帖子类型的代码 过滤项目,任何帮助吗?
  2. 我创建了包含图片的自定义帖子类型, catagory,文件和标题我想制作一个过滤器库html 来自wp查询的动态。我想在下面的代码中显示自定义的帖子类型数据,以便我可以使用li相关类过滤项目。 这是我的HTML https://codepen.io/creotip/pen/dfjeF

    function custom_post_type(){

        $labels = array(
            'name'                => _x( 'Audio', 'Post Type General Name', 'twentythirteen' ),
            'singular_name'       => _x( 'Audio', 'Post Type Singular Name', 'twentythirteen' ),
            'menu_name'           => __( 'Audio', 'twentythirteen' ),
            'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
            'all_items'           => __( 'All Audio', 'twentythirteen' ),
            'view_item'           => __( 'View Audio', 'twentythirteen' ),
            'add_new_item'        => __( 'Add New Audio', 'twentythirteen' ),
            'add_new'             => __( 'Add New', 'twentythirteen' ),
            'edit_item'           => __( 'Edit Audio', 'twentythirteen' ),
            'update_item'         => __( 'Update Audio', 'twentythirteen' ),
            'search_items'        => __( 'Search Audio', 'twentythirteen' ),
            'not_found'           => __( 'Not Found', 'twentythirteen' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
        );
    
        // Set other options for Custom Post Type
    
        $args = array(
            'label'               => __( 'audio', 'twentythirteen' ),
            'description'         => __( 'Audio news and reviews', 'twentythirteen' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'thumbnail', 'comments',  ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'page',
    
            // This is where we add taxonomies to our CPT
            'taxonomies'          => array( 'category' ),
        );
    
        // Registering your Custom Post Type
        register_post_type( 'audio', $args );
    
        }
    
        /* Hook into the 'init' action so that the function
        * Containing our post type registration is not 
        * unnecessarily executed. 
        */
    
        add_action( 'init', 'custom_post_type', 0 );
    

    在此输入代码

2 个答案:

答案 0 :(得分:0)

//使用帖子过滤类别

<?php 
        $category_args = array(
        'taxonomy' => 'category',
        'hide_empty' => 0,
        'depth' => 0,
        'hierarchical' => true,
        'exclude' => '1',
        'orderby' => 'ID',
        'order'   => 'ASC'
        );

        $category_args_array = get_categories($category_args); 
        if (!empty($category_args_array)) 
        {
            $category_args_count=0;
            ?>
            <div class="toolbar mb2 mt2">
            <button class="btn fil-cat" href="" data-rel="all">All</button>
            <?php
            foreach ( $category_args_array as $category_args_category ) 
            {
                $category_args_count++;
                $category_args_id    = $category_args_category->term_id;
                $category_args_name  = $category_args_category->cat_name;
                $category_args_slug  = $category_args_category->slug;
                $category_args_slug = str_replace('-', '_', $category_args_slug);
                ?>
                <button class="btn fil-cat" data-rel="<?php echo $category_args_slug;?>"><?php echo $category_args_name;?></button>
                <?php
            }
            ?>
            </div> 
            <?php
        }
        ?>

        <div style="clear:both;"></div>   
        <?php
        $audio_type = 'audio';
        $audio_args=array(
            'type'                     => $audio_type,
            'post_status'              => 'publish',
            'posts_per_page'           => -1,
            'caller_get_posts'         => -1,
            'orderby'                  => 'id',
            'order'                    => 'ASC'
        );
        $audio_my_query = null;
        $audio_my_query = new WP_Query($audio_args);

        if( $audio_my_query->have_posts() ) 
        {
            ?>
            <div id="portfolio">
            <?php
            while ($audio_my_query->have_posts()) : $audio_my_query->the_post(); 

                $term_name_array = wp_get_post_terms($post->ID, 'category', array("fields" => "slug"));

                $term_name       =  $term_name_array[0];
                $class_term_name = str_replace('-', '_', $term_name);

                if ( has_post_thumbnail() ) {
                        //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                       ?>
                         <div class="tile scale-anm <?php echo $class_term_name?> all">
                                  <?php echo  get_the_post_thumbnail($post->ID,"thumbnail"); //thumbnail,medium,large,full,array(100,100)?>
                          </div>
                       <?php
                    }
               endwhile;
               ?>
               </div>
               <?php
        }
        wp_reset_query($audio_my_query);
      ?>
    <div style="clear:both;"></div>

答案 1 :(得分:0)

在taxonomy-category.php中打印所选类别

<?php
    $category_object=get_queried_object();
    $category_term_id = $category_object->term_id;
    $category_term_name = $category_object->cat_name;

?>
<h1><?php echo $category_term_name;?></h1>

在taxonomy-category.php中显示类别明智的帖子图片

<?php

    $audio_type = 'audio';
    $audio_args=array(
        'type'                     => $audio_type,
        'post_status'              => 'publish',
        'posts_per_page'           => -1,
        'caller_get_posts'         => -1,
        'tax_query'                => array(
            array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => $category_term_id
            )
        ),
        'orderby'                  => 'id',
        'order'                    => 'ASC'
    );
    $audio_my_query = null;
    $audio_my_query = new WP_Query($audio_args);

    if( $audio_my_query->have_posts() ) 
    {
        ?>
        <div id="portfolio">
        <?php
        while ($audio_my_query->have_posts()) : $audio_my_query->the_post(); 

            $term_name_array = wp_get_post_terms($post->ID, 'category', array("fields" => "slug"));

            $term_name       =  $term_name_array[0];
            $class_term_name = str_replace('-', '_', $term_name);

            if ( has_post_thumbnail() ) {
                    //get_the_post_thumbnail( $post->ID, array( 100, 100) ); 
                   ?>
                     <div class="tile scale-anm <?php echo $class_term_name?> all">
                              <?php echo  get_the_post_thumbnail($post->ID,"thumbnail"); //thumbnail,medium,large,full,array(100,100)?>
                      </div>
                   <?php
                }
           endwhile;
           ?>
           </div>
           <?php
    }
    wp_reset_query($audio_my_query);
?>