在 Wordpress 中显示分类法
Showing taxonomies in Wordpress
我有一个包含三个自定义分类法的 Wordpress 站点。它们已配置好并正在工作,每个都标有一些 post,并希望 运行 代码与它们相关。根据文档,如果自定义分类法是猫、狗和树,并且我想 运行 php 关于猫,在对文档进行大量尝试和上下调整之后,我想我应该创建一个模板并将其命名为“taxonomy-cats.php”:
taxonomy-{taxonomy}.php: For example, if the taxonomy is named “sometax,” WordPress would look for a file named taxonomy-sometax.php
但我还是没明白:文档说“wordpres 会寻找”。为什么 WP 会寻找它而不是其他的?我想我至少必须告诉它搜索什么或何时搜索它。我如何或在哪里告诉 WP 寻找它?
<?php /* Template Name: Taxonomy cats */
get_header(); ?>
<h1>Hello world</h1>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
作为模板,我发现我可以在管理中将其作为 post 的属性添加,但是当我进入该页面时,如果我尝试获取分类法的 ID,我获取 post.
的 ID
WordPress 使用循环显示帖子。无论您身在何处,大部分时间都在使用循环。
The Loop is PHP code used by WordPress to display posts. Using The
Loop, WordPress processes each post to be displayed on the current
page, and formats it according to how it matches specified criteria
within The Loop tags. Any HTML or PHP code in the Loop will be
processed on each post.
来源@https://codex.wordpress.org/The_Loop
Wordpress 也基于模板层次结构系统。除了基本的 index.php 模板文件外,您可以选择是否要实现特定的模板文件。
WordPress uses the query string to decide which template or set of
templates should be used to display the page. The query string is
information that is contained in the link to each part of your
website.
来源@https://developer.wordpress.org/themes/basics/template-hierarchy/
在显示 dogs
分类法的情况下,Wordpress 按此顺序查找以下文件:taxonomy-pets-dogs.php
如果此模板不存在,则继续查找宠物 taxonomy-pets.php
,如果它不存在,它会移动到 taxonomy.php
... 等等
(其中 pets
是您的分类法名称,dogs
是该分类法中的术语)
指定 taxonomy-pets-dogs.php
使您能够为该特定术语创建自定义模板。 cats
不会有与 dogs
相同的模板,除非两者都使用 taxonomy.php
或 taxonomy-pets.php
并且不存在特定模板。
但无论如何 taxonomy-{taxonomy}.php
或 taxonomy-{taxonomy}-{term}.php
将始终使用循环来显示帖子和术语。与任何其他模板一样。
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo the_title().'<br/>';
endwhile; else :
echo 'No posts found!';
endif; ?>
我有一个包含三个自定义分类法的 Wordpress 站点。它们已配置好并正在工作,每个都标有一些 post,并希望 运行 代码与它们相关。根据文档,如果自定义分类法是猫、狗和树,并且我想 运行 php 关于猫,在对文档进行大量尝试和上下调整之后,我想我应该创建一个模板并将其命名为“taxonomy-cats.php”:
taxonomy-{taxonomy}.php: For example, if the taxonomy is named “sometax,” WordPress would look for a file named taxonomy-sometax.php
但我还是没明白:文档说“wordpres 会寻找”。为什么 WP 会寻找它而不是其他的?我想我至少必须告诉它搜索什么或何时搜索它。我如何或在哪里告诉 WP 寻找它?
<?php /* Template Name: Taxonomy cats */
get_header(); ?>
<h1>Hello world</h1>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
作为模板,我发现我可以在管理中将其作为 post 的属性添加,但是当我进入该页面时,如果我尝试获取分类法的 ID,我获取 post.
的 IDWordPress 使用循环显示帖子。无论您身在何处,大部分时间都在使用循环。
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
来源@https://codex.wordpress.org/The_Loop
Wordpress 也基于模板层次结构系统。除了基本的 index.php 模板文件外,您可以选择是否要实现特定的模板文件。
WordPress uses the query string to decide which template or set of templates should be used to display the page. The query string is information that is contained in the link to each part of your website.
来源@https://developer.wordpress.org/themes/basics/template-hierarchy/
在显示 dogs
分类法的情况下,Wordpress 按此顺序查找以下文件:taxonomy-pets-dogs.php
如果此模板不存在,则继续查找宠物 taxonomy-pets.php
,如果它不存在,它会移动到 taxonomy.php
... 等等
(其中 pets
是您的分类法名称,dogs
是该分类法中的术语)
指定 taxonomy-pets-dogs.php
使您能够为该特定术语创建自定义模板。 cats
不会有与 dogs
相同的模板,除非两者都使用 taxonomy.php
或 taxonomy-pets.php
并且不存在特定模板。
但无论如何 taxonomy-{taxonomy}.php
或 taxonomy-{taxonomy}-{term}.php
将始终使用循环来显示帖子和术语。与任何其他模板一样。
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo the_title().'<br/>';
endwhile; else :
echo 'No posts found!';
endif; ?>