首页 > 解决方案 > 如何访问 WordPress 中的 category.php 页面?

问题描述

我已经category.php在我的主题中创建并添加了一些代码。下面的代码将显示我的类别列表。现在我想知道如何访问此category.php页面?

当我访问时,mywebsite.com/category我得到Page not found.

我在 category.php 页面中添加了以下代码

   <?php
   /**
   * A Simple Category Template
   */
   get_header(); 

?>  
<div id="primary" class="content-area">
   <main id="main" class="site-main">
       <div class="CategoryHero">
           <?php
// Get the current queried object
$term    = get_queried_object();
$term_id = ( isset( $term->term_id ) ) ? (int) $term->term_id : 0;

$categories = get_categories( array(
    'taxonomy'   => 'category',
    'orderby'    => 'name',
    'parent'     => 0,
    'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
?>
<div class="categoryWrapper_list"><div class="equalPadding">
<ul>
    <?php
    foreach ( $categories as $category ) 
    {
        //echo "<pre>";
        //var_dump($category);
        $cat_ID        = $category->term_id;
        $category_name = $category->name;
        $category_desc = $category->description;
        $category_images = get_option('category_images');  
        
        // When viewing a particular category, give it an [active] class
        $cat_class = ( $cat_ID == $term_id ) ? 'active' : 'not-active';
        // I don't like showing the [uncategoirzed] category
        if ( strtolower( $category_name ) != 'uncategorized' ){?>
            <li> <div class="subCategoryPostWrap" style="background-image:linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)),url(<?php echo $category_images[$cat_ID];?>)">
                <div class="blackBoxPackage">
                    <div class="cp-b-content">
                    <h4><?php echo $category_name;?></h4>
                    <p><?php echo $category_desc;?></p>
          <div class="canExplore">
              <a class="canbtn" href="<?php echo esc_url(get_category_link($cat_ID));?>">Explore now</a>
                </div>
          </div>

                </div>
                </div>
    </li>
                    <?php
                    
        }
    }
    ?>
</ul>
    </div>
           </div>
     
         </div>
   </main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

在此处输入图像描述

标签: wordpresscategories

解决方案


我不完全确定这会解决你的问题,但你需要让 WordPress 知道这是一个自定义模板,方法是在文件顶部写一个打开的 PHP 注释来说明模板的名称。

<?php 

/* Template Name: A Simple Category Template */ 

?>

推荐阅读