簡體   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