如何获取自定义分类模板?

发布于 2024-12-17 10:59:43 字数 1175 浏览 3 评论 0原文

我在 WordPress 中有一个类别页面,它有自己的模板 categories.php,它实际上是一个静态页面,其中包含链接到特定类别的框。这是一个简短的代码:

<?php
/*
    Template Name: Categories
*/
?>

<?php get_header(); ?>

<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>

<div id="main-content-categories">

    <h5 class="inner_text_shadow">Categories & Tags</h5>

    <div id="clear-box">

    <a href="/category/locations"><div id="cc" class="c1"><h6>The Fall Locations</h6></div></a>
    <a href="/category/info"><div id="cc" class="c2"><h6>Info</h6></div></a>
    <a href="/category/budget"><div id="cc" class="c3"><h6>Budget</h6></div></a>


</div> <!-- END main-content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

CSS 并不重要。

我的问题是 - 哪个 PHP 文件/模板用于特定类别(例如 www.mywebsite.com/category/budget)中的帖子?当我单击任何一个框时,我确实会收到该类别中的帖子,但没有格式设置,只有页眉、侧边栏和页脚。 “来自类别的帖子”的实际代码是什么?我应该将其输入到哪个 PHP 文件中?

[同样的问题也适用于标签。 (www.mywebsite.com/tag/food)]

I have a categories page in Wordpress and it has its own template categories.php, which is actually a static page with boxes that link to specific categories. This is a shortened code:

<?php
/*
    Template Name: Categories
*/
?>

<?php get_header(); ?>

<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>

<div id="main-content-categories">

    <h5 class="inner_text_shadow">Categories & Tags</h5>

    <div id="clear-box">

    <a href="/category/locations"><div id="cc" class="c1"><h6>The Fall Locations</h6></div></a>
    <a href="/category/info"><div id="cc" class="c2"><h6>Info</h6></div></a>
    <a href="/category/budget"><div id="cc" class="c3"><h6>Budget</h6></div></a>


</div> <!-- END main-content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

CSS doesn't matter.

My question is - which PHP file/template is used for those posts that are in certain category like www.mywebsite.com/category/budget? When I click any of the boxes I do get the posts that are in that category but there's no formatting, just the header, sidebar and footer. What is the actual code for "posts from category" and what PHP file do I input it to?

[Same question goes for tags. (www.mywebsite.com/tag/food)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-12-24 10:59:43

category.php 可以处理所有类别。请参阅下面的示例:

<?php get_header(); ?>

<?php 

//Get Category
$category = get_category(get_query_var('cat'));

?>

    <!-- Start Loop -->
    <?php query_posts('category_name=' . $category->cat_name . '&paged='. get_query_var('paged')); ?>           

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

        <!-- Loop Code Here -->         

    <?php endwhile; else: ?>
            <p>An Error Occurred</p>
    <?php endif; ?> 


    <?php wp_reset_query(); ?>
    <!-- End Loop -->


<?php get_footer(); ?>

您还可以为每个类别创建单独的文件,格式为category-slug.php。在你的情况下,你会有category-locations.php,category-info.php & category-budget.php

希望这有帮助。

The category.php can handle all categories. See my example below:

<?php get_header(); ?>

<?php 

//Get Category
$category = get_category(get_query_var('cat'));

?>

    <!-- Start Loop -->
    <?php query_posts('category_name=' . $category->cat_name . '&paged='. get_query_var('paged')); ?>           

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

        <!-- Loop Code Here -->         

    <?php endwhile; else: ?>
            <p>An Error Occurred</p>
    <?php endif; ?> 


    <?php wp_reset_query(); ?>
    <!-- End Loop -->


<?php get_footer(); ?>

You can also have separate files for each category in the format category-slug.php. In your case you would have category-locations.php, category-info.php & category-budget.php

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文