我使用以下方法为特定类别设置了自定义单一模板:
// custom single template for specific category
function wpse_custom_category_single_template( $single_template ) {
// global $post;
// get all categories of current post
$categories = get_the_category( $post->ID );
$top_categories = array();
// get top level categories
foreach( $categories as $cat ) {
if ( $cat->parent != 0 ) {
$top_categories[] = $cat->parent;
} else {
$top_categories[] = $cat->term_id;
}
}
// check if specific category exists in array
if ( in_array( '7', $top_categories ) ) {
if ( file_exists( get_template_directory() . '/single-custom.php' ) ) return get_template_directory() . '/single-custom.php';
}
return $single_template;
}
add_filter( 'single_template', 'wpse_custom_category_single_template' );
在single-custom.php中,我正在使用:
<?php echo get_the_date('F jS, Y', $post->ID); ?>
但它正在从所有这一类别的第一篇文章中提取日期(即在每个帖子上显示相同的日期)。在不使用自定义单个模板的所有其他帖子中,日期显示正常。任何人都可以建议我在哪里犯错误。
编辑以显示single-custom.php
<?php
/**
* The template for displaying all single posts and attachments
*
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
get_header(); ?>
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('
<p id="breadcrumbs">','</p>
');
}
?>
<header>
<h1 class="hero-title">Customers <span>&</span> Resources</h1>
</header>
<div class="main-container">
<div class="main-grid">
<main class="main-content-full-width single-news">
<div class="news-bar">
<h2>Case Studies</h2>
<?php
$args = array(
'prev_text' => '<span class="fa fa-chevron-left"> </span>Previous Study',
'next_text' => 'Next Study <span class="fa fa-chevron-right"> </span>',
'screen_reader_text' => 'News navigation',
'excluded_terms' => '6',
'taxonomy' => 'category'
);
the_post_navigation($args); ?>
</div>
<div class="single-news-articles">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<h3 class="entry-title"><?php wp_title(); ?></h3>
<span class="news-date"><?php get_the_date('F jS, Y', $post->ID); ?></span>
</header>
<div class="entry-content">
<?php
the_content();
?>
<?php edit_post_link( __( '(Edit)', 'foundationpress' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<footer>
<?php
wp_link_pages(
array(
'before' => '<nav id="page-nav"><p>' . __( 'Pages:', 'foundationpress' ),
'after' => '</p></nav>',
)
);
?>
<?php $tag = get_the_tags(); if ( $tag ) { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</article>
</div>
<div class="footer-news">
<?php
$args = array(
'prev_text' => '<span class="fa fa-chevron-left"> </span>Previous Study',
'next_text' => 'Next Study <span class="fa fa-chevron-right"> </span>',
'screen_reader_text' => 'Case Study navigation'
);
the_post_navigation($args); ?>
</div>
</div>
<?php endwhile;?>
</main>
</div>
</div>
<?php echo do_shortcode("[consultant]"); ?>
<?php get_footer();
答案 0 :(得分:1)
我相信你可以从$post->ID
删除get_the_date()
:
echo get_the_date( 'F jS, Y' );
如果这不起作用,您可以尝试使用get_the_ID()
功能代替$post->ID
:
echo get_the_date( 'F jS, Y', get_the_ID() );
旁注:
您的代码中还有一个额外的</div>
,这是故意的吗?如果没有,我已经清理了一点,以便更容易解析:
<?php
/**
* The template for displaying all single posts and attachments
*
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
get_header();
if( function_exists( 'yoast_breadcrumb' ) )
yoast_breadcrumb( '<p id="breadcrumbs">', '</p>' );
?>
<header>
<h1 class="hero-title">Customers <span>&</span> Resources</h1>
</header>
<div class="main-container">
<div class="main-grid">
<main class="main-content-full-width single-news">
<div class="news-bar">
<h2>Case Studies</h2>
<?php
$args = array(
'prev_text' => '<span class="fa fa-chevron-left"> </span>Previous Study',
'next_text' => 'Next Study <span class="fa fa-chevron-right"> </span>',
'screen_reader_text' => 'News navigation',
'excluded_terms' => '6',
'taxonomy' => 'category'
);
the_post_navigation( $args );
?>
</div>
<div class="single-news-articles">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<h3 class="entry-title"><?php wp_title(); ?></h3>
<span class="news-date"><?php get_the_date( 'F jS, Y' ); ?></span>
</header>
<div class="entry-content">
<?php the_content(); ?>
<?php edit_post_link( __( '(Edit)', 'foundationpress' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<footer>
<?php
wp_link_pages(
array(
'before' => '<nav id="page-nav"><p>' . __( 'Pages:', 'foundationpress' ),
'after' => '</p></nav>',
)
);
if( get_the_tags() )
the_tags( '<p>', ', ', '</p>' );
?>
</footer>
</article>
<div class="footer-news">
<?php
$args = array(
'prev_text' => '<span class="fa fa-chevron-left"> </span>Previous Study',
'next_text' => 'Next Study <span class="fa fa-chevron-right"> </span>',
'screen_reader_text' => 'Case Study navigation'
);
the_post_navigation( $args );
?>
</div>
</div>
<?php endwhile;?>
</main>
</div>
</div>
<?php
echo do_shortcode("[consultant]");
get_footer();
?>
答案 1 :(得分:0)
如果没有看到来自single-custom.php
的代码,则很难确定,但听起来您的$post
变量不包含您想要日期的项目。确保在每次循环迭代时重新分配全局$post
变量。像这样的东西会把它放在你的循环中。
$your_query_object->the_post();
echo get_the_date('F jS, Y', $post->ID)