Php 不同的职位类别有不同的产出
我这里有个小问题。到目前为止,我有两个帖子类别:category\u name=项目和category\u name=博客。所以我希望每个类别的帖子都有不同的风格和不同的输出。 因此,我创建了两个文件projects.php和content.php,然后将以下代码放入single.php中Php 不同的职位类别有不同的产出,php,wordpress,Php,Wordpress,我这里有个小问题。到目前为止,我有两个帖子类别:category\u name=项目和category\u name=博客。所以我希望每个类别的帖子都有不同的风格和不同的输出。 因此,我创建了两个文件projects.php和content.php,然后将以下代码放入single.php中 <?php get_header(); ?> <div id="primary" class="content-area"> <main id="main" class
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ( 'category_name=projects' ) : ?>
<?php get_template_part( 'template-parts/project', get_post_format() );
the_post_navigation();
?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', get_post_format() );
the_post_navigation();
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
<?php endif; ?>
<?php endwhile; else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
</main>
<!-- #main -->
</div>
<!-- #primary -->
<?php
'); 高度:80vh;
背景尺寸:封面;背景重复:无重复;背景位置:中心;">
出版:
所以问题是-如何为每个文件引用特定的类别?您的PHP
if
语句看起来有点奇怪。您没有使用任何比较运算符。您只是调用if('category\u name=projects')
,它只是对字符串调用if
(由于字符串的“value”大于0,因此应该返回true)
您是否有存储类别的变量?如果有,则需要执行
strcmp()
或==
以查看要显示的类别,然后显示相应的类别。您可以尝试使用wp查询-
像-
然后进行循环尝试-
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
我想我已经解决了。 所以…我将single.php重新编辑为默认值,并将当前代码添加到functions.php
function get_custom_cat_template($single_template) {
global $post;
if ( in_category( 'projects' )) {
$single_template = dirname( __FILE__ ) . '/projects.php';
}
return $single_template;
}
add_filter( "single_template", "get_custom_cat_template" ) ;
现在,当我浏览该类别时,WP调用projects.php,而所有rest类别默认保持不变。
现在将测试它。没有存储任何变量。因此我需要编写不同的single.php:(
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
function get_custom_cat_template($single_template) {
global $post;
if ( in_category( 'projects' )) {
$single_template = dirname( __FILE__ ) . '/projects.php';
}
return $single_template;
}
add_filter( "single_template", "get_custom_cat_template" ) ;