繁体   English   中英

如何在 Wordpress 中自定义类别页面?

[英]How can I customize category pages in Wordpress?

我正在用 wordpress 创建一个博客,我正在使用 DIVI 主题,我需要更改博客类别页面的外观...

最简单的方法是什么?

我知道我应该在编辑器中查找 category.php 并创建一个新的 php 文件,但我找不到任何东西......

您将在主题文件夹中找到 category.php 并根据您的要求进行自定义。

如果您的主题中不存在 category.php 文件,那么您可以创建一个名为 category.php 的新文件并进行自定义,它会在您显示类别帖子时自动使用此文件。

您需要创建如下类别模板:

<?php
        /**
    * A Simple Category Template
    */

    get_header(); ?> 

    <section id="primary" class="site-content">
    <div id="content" role="main">
    <?php 
    // Check if there are any posts to display
    if ( have_posts() ) : ?>

    <header class="archive-header">
    <?php
    // Since this template will only be used for Design category
    // we can add category title and description manually.
    // or even add images or change the layout
    ?>

    <h1 class="archive-title">Design Articles</h1>
    <div class="archive-meta">
    Articles and tutorials about design and the web.
    </div>
    </header>

    <?php

    // The Loop
    while ( have_posts() ) : the_post();
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

    <div class="entry">
    <?php the_excerpt(); ?>

     <p class="postmetadata"><?php
      comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed');
    ?></p>
    </div>

    <?php endwhile; // End Loop

    else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    </div>
    </section>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

首先,如果您的主题文件夹中确实存在一个名为category.php文件,那么遵循@Lalji Nakum 关于直接编辑文件的建议是不明智的。 这意味着您可能会在未来的主题更新中丢失所有更改。 相反,您应该创建一个包含要更改的类别的 ID 或 slug 的模板文件。 如果您要更改所有类别的显示方式,您应该创建一个子主题 - 保存您自己的 category.php 版本。

如果您的主题文件夹中没有 category.php,则意味着该主题在 archive.php 或 index.php 中控制此视图。 WordPress 遵循严格的层次结构,寻找显示类别的方法。 然后,您将创建该文件并对它们的显示方式进行任何更改。 这里的一个问题可能是您必须从头开始制作。 另一种方法是退回到子主题解决方案,追踪您的主题实际上控制视图的位置(在前面提到的两个文件中的任何一个中),将文件复制到您的新子主题中并在此处进行更改。

现在已经确定您今天的主题中没有 category.php。 然后,您必须首先做出选择。 这是最好的一个:

  1. 在您的主题文件夹中创建 category.php。
  2. 从头开始创建文件的内容。 一个很好的起点是这里:

     <div id="container"> <div id="content" role="main"> <h1 class="page-title"><?php printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h1> <?php $category_description = category_description(); if ( ! empty( $category_description ) ) echo '<div class="archive-meta">' . $category_description . '</div>'; get_template_part( 'loop', 'category' ); ?> </div><!-- #content --> </div><!-- #container -->
  3. 对模板进行所有您想要的更改。

  4. 完成的!

创建子主题相当简单,解释如下: https : //codex.wordpress.org/Child_Themes

此外,听起来您正在寻找从编辑器创建文件。 这不是内置的功能,但可以通过像这样的创造性解决方案来管理: http : //ronangelo.com/create-new-theme-file-in-wp-admin-without-ftp/ 不过,更好的选择是使用 ftp/ssh。

在此处阅读有关类别模板(包括提到的层次结构)如何工作的更多信息: https : //codex.wordpress.org/Category_Templates

暂无
暂无

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

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