好的,试试这个(未经测试,请原谅任何小的打字错误/语法错误)。
我已经修改,以便循环首先检查是否有要显示的帖子。我也不再需要$count
以及添加\' \'
通过向数组中添加任何匹配项,然后使用join()
.
希望我没有走错路,它会适合你的需要。
<?php
/**
* The Template for displaying all single posts.
*
* @package WordPress
* @subpackage WDM
* @since WDM 1.0
*/
get_header();
?>
<div id="primary" class="site-content">
<div id="content" class="clear" role="main">
<?php
if(have_posts()) : while(have_posts()) : the_post();
$excludedcats = array(3);
$categories = get_the_category();
$title_cats = array(); // Initialise the arry to avoid errors when checking it
foreach($categories as $category) :
/** Check to see if the category needs to be added to the class="" of the title */
if(!in_array($category->cat_ID, $excludedcats)) :
$title_cats[] = \'heading-\'.$category->slug;
endif;
endforeach;
/** Output the title. We are checking that there were actually category headings here as well, so that class="" is properly formatted */
echo \'<h1 class="category-heading\'.
sprintf(__(\'%1$s\'),
/** %1$s */ (!empty($title_cats)) ? \' \'.join(\' \', $title_cats) : \'\'
). // end sprintf()
\'">\'; // end <h1>
single_post_title();
echo \'</h1>\';
/** Output the content */
the_content();
?>
<div class="related-posts-div">
<?php //wp_related_posts()?>
</div>
<nav class="nav-single">
<h3 class="assistive-text"><?php _e( \'Post navigation\', \'wdm\' ); ?></h3>
<span class="nav-previous"><?php previous_post_link( \'%link\', \'<span class="meta-nav">\' . _x( \'←\', \'Previous post link\', \'wdm\' ) . \'</span> %title\' ); ?></span>
<span class="nav-next"><?php next_post_link( \'%link\', \'%title <span class="meta-nav">\' . _x( \'→\', \'Next post link\', \'wdm\' ) . \'</span>\' ); ?></span>
</nav><!-- .nav-single -->
<?php comments_template( \'\', true ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>