繁体   English   中英

添加类别页面 - CPT - Wordpress

[英]Adding Category Pages - CPT - Wordpress

我有一个自定义的帖子类型,我正在尝试显示一个类别页面,所以当你点击一个类别的标准链接时,你会看到该类别中的所有帖子......看起来很简单,我看了一下Wordpress层次结构,但我无法弄清楚我需要创建哪些模板文件...

我目前有这个页面浏览我的自定义字段: http//ideedev.co.uk/newseed/brand/不循环自定义帖子类型。 代码基本上是这样的:

<?php
/**
 * Template name: Main Category Template
 */
?>

<?php get_header(); the_post(); ?>



<!-- Featured Image ===========================================  -->

<div class="image-test-container">

    <?php if ( has_post_thumbnail() ) {

    // Get the post thumbnail URL
    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
  } else {

    ?>
    <style type="text/css">.featured-image{
    display:none;
    }</style>
    <?php
  } ?>


    <div id="absolute1111" style=" background: url(<?php echo $feat_image; ?>);">
        <div class="centerd1111">
            <h1><?php the_title(); ?></h1>
        </div>
    </div>  

</div>





<!-- Page Content ===========================================  -->

<div class="container">

    <div class="row clearfix">
    <div class="">

    </div>
    </div>

    <div class="row clearfix">
    <div class="level-two-intro-text">
        <p><?php the_field('intro_text'); ?></p>
    </div>
    <div class="level-two-sub-title block__title">
        <?php the_field('sub_title'); ?>
    </div>
    </div>

</div>





<!-- Areas ===========================================  -->

<div class="container">
    <div class="row clearfix">

        <?php if(get_field('areas')): ?>


                <?php while(has_sub_field('areas')): ?>


                    <div class="single-area-item six columns">
                        <p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
                        <h2> <?php the_sub_field('area_title'); ?> </h2> 
                        <p> <?php the_sub_field('area_info'); ?> <p>
                        <div class="area-button"><a href="<?php the_sub_field('button_target'); ?>" class="btn btn--dark-blue" role="button"><?php the_sub_field('button_text'); ?></a></div>
                    </div>


                <?php endwhile; ?>


        <?php endif; ?>

    </div>
</div>

但是,当我点击每个类别下的“投资组合”按钮时,我希望它显示该类别的页面...

我可以通过为每个类别创建一个模板页面并循环显示特定类别中的所有帖子来实现这项工作,但这似乎是错误的方式 - 我认为应该有一个模板来显示我刚才的类别点击......

老实说,我觉得我有点困惑。 谢谢你看:)

+++ EDIT +++

这是我用于自定义帖子类型的代码...

register_post_type('portfolio', $args);

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

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'category'),
);

register_taxonomy('portfolio-category', array('portfolio'), $args);

您可以为自定义类别添加存档模板。 最简单的方法是将archive.php复制到主题文件夹中,并为其指定正确的名称。

根据模板heirarchy( https://developer.wordpress.org/themes/basics/template-hierarchy/ ),您需要使用文件名'taxonomy-portfolio-category.php'

如果要以不同方式设置每个类别的样式,可以创建名称为“taxonomy-portfolio-category- {categoryslug} .php”的文件。

创建一个文件'taxonomy-portfolio-category.php'

您的自定义分类代码放在这里。

您好先创建一个名为taxonomy-portfolio-category.php的页面并放在代码下面,它是一个示例代码,您可以根据您的主题进行编辑

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

<?php /* How to display posts of the Gallery format. The gallery category is the old way. */ ?>
<div class="test">
<?php 
echo "<h2>".get_the_title()."</h2>";
echo "<div>".get_the_excerpt()."</div>";
?>
</div>
<?php endwhile; // End the loop. Whew. ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM