Làm cách nào để hiển thị các danh mục Loại bài tùy chỉnh của tôi?


7

Tôi có một loại bài tùy chỉnh. Những gì tôi muốn làm là hiển thị các danh mục dự án ngay trên đầu các dự án để khách truy cập có thể lọc các dự án tương ứng.

Trong tôi functions.phpcó:

<?php

require_once('portfolio-type.php');
add_filter('excerpt_length', 'my_excerpt_length');

function my_excerpt_length($length) {
return 25; 
}

add_filter('excerpt_more', 'new_excerpt_more');  
function new_excerpt_more($text){  

return ' ';  
}  

function portfolio_thumbnail_url($pid){
$image_id = get_post_thumbnail_id($pid);  
$image_url = wp_get_attachment_image_src($image_id,'screen-shot');  
return  $image_url[0];  
}
?>

Trong portfolio-type.php:

 <?php

 if ( function_exists( 'add_theme_support' ) ) { 
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 270, 170, true ); // Normal post thumbnails
add_image_size( 'screen-shot', 720, 540 ); // Full size screen
 }

 add_action('init', 'portfolio_register');  

 function portfolio_register() {  
 $args = array(  
    'label' => __('Portfolio'),  
    'singular_label' => __('Project'),  
    'public' => true,  
    'show_ui' => true,  
    'capability_type' => 'post',  
    'hierarchical' => false,  
    'rewrite' => true,  
    'supports' => array('title', 'editor', 'thumbnail')  
   );  

register_post_type( 'portfolio' , $args );  
 }  

register_taxonomy("project-type", array("portfolio"), array("hierarchical"  =>  true, "label" => "Project Types", "singular_label" => "Project Type",   "rewrite" => true));
 ?>

Và cuối cùng trong index.php của tôi, tôi có:

<!-- Start Projects -->

                    <div id="posts" class="row isotope">

                      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>  

                          <?php  
                              $title= str_ireplace('"', '', trim(get_the_title()));  
                              $desc= str_ireplace('"', '', trim(get_the_content()));  
                          ?>     

                          <div class="item post item span4 isotope-item">

                            <a class="project-wrp fancybox" title="<?=$title?>" rel="lightbox[work]" href="<?php print portfolio_thumbnail_url($post->ID) ?>"><div class="profile-photo"><div class="profile-icon">&#0102;</div><?php the_post_thumbnail(array('230','170'),array('alt' => '')); ?> </div>  
                            <div class="project-name"><?php echo $title; ?></div>
                            <div class="project-client"><?php echo $desc; ?></div>
                            </a>
                          </div>  
                      <?php endwhile; endif; ?>  


                    </div>

Câu trả lời:


15

Xóa mã của bạn khỏi portfolio-type.php đăng ký loại bài đăng và phân loại (dòng 9 trở đi).

Sử dụng mã sau đây (trong portfolio-type.php) để đăng ký loại bài "portfolio"

function portfolio_register() {
    $labels = array(
        'name' => _x('Portfolio', 'post type general name'),
        'singular_name' => _x('Portfolio Item', 'post type singular name'),
        'add_new' => _x('Add New', 'portfolio item'),
        'add_new_item' => __('Add New Portfolio Item'),
        'edit_item' => __('Edit Portfolio Item'),
        'new_item' => __('New Portfolio Item'),
        'view_item' => __('View Portfolio Item'),
        'search_items' => __('Search Portfolio Items'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 8,
        'supports' => array('title','editor','thumbnail')
    ); 
    register_post_type( 'portfolio' , $args );
}
add_action('init', 'portfolio_register');

Sử dụng mã sau đây (trong portfolio-type.php) để đăng ký phân loại "portfolio_clists" cho loại bài đăng "portfolio", làm cho nó được phân cấp (như thể loại)

function create_portfolio_taxonomies() {
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'categories' ),
    );

    register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args );
}
add_action( 'init', 'create_portfolio_taxonomies', 0 );

Sau đó, sử dụng mã sau đây để truy xuất các thuật ngữ phân loại trong tệp mẫu (như index.php)

<?php

$taxonomy = 'portfolio_categories';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>

?>

Hãy cho tôi biết nếu bạn cần bất kỳ làm rõ.


Tôi đã thay thế mã trong portfolio-type.php của mình bằng mã bạn cung cấp và tôi có các danh mục để hiển thị nhưng vì lý do nào đó chúng không lọc các dự án.
Laniakea

Hai đoạn mã đầu tiên tôi cung cấp là cho chức năng back-end. Đoạn mã thứ ba tôi cung cấp cho phép bạn truy xuất tên danh mục. Hãy để tôi đi qua trang web của bạn một lần nữa và xem những gì bạn đang thiếu bây giờ.
Rahul Verma

Bạn có thể đăng mã php (cho toàn bộ phần danh mục đầu tư) bây giờ không?
Rahul Verma

Tôi đã cam kết mã ở đây .
Laniakea

Tôi sẽ bắt đầu một chủ đề mới liên quan đến việc lọc danh mục.
Laniakea

0

Đối với danh mục bài viết danh mục này danh mục sử dụng:

<?php echo get_the_term_list(get_the_ID(), 'portfolio_category', '', ', ', ''); ?>
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.