我正在构建一个主题,在我的category.php页面上,我想要显示几个完整帖子(比方说3,但需要能够轻松地将其更改为2或1),然后是该类别中的其他帖子作为 Headers 链接 .
我的循环中有相当多的HTML用于样式化我的帖子和添加自定义字段,所以对所有代码都很抱歉,但这就是我的category.php页面现在的样子 . 我已经尝试过一些没有用过的东西,所以编辑了这个以显示我的原始代码,它只有一个正常的帖子列表 . 我对编辑The Loop有些新意,所以尽可能多地理解解释/清晰度 .
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><a href="<?php the_permalink() ?>#content" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span>
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span>
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
2 回答
你可以使用下面的代码来实现上面的事情:首先你需要循环所有的帖子,并且当它达到2以上的时候放置它的停止来打印内容 . 但是 Headers 将始终存在 .
有关详细信息,请参阅:https://codex.wordpress.org/The_Loop_in_Action
我自己想出了一个解决方案,虽然它依赖于使用插件/小部件而不是我更喜欢的插件/小部件 .
我只是将阅读设置设置为显示2个帖子,然后在循环下面我添加了一个小部件区域并使用Recent Posts Extended小部件来显示 Headers /链接列表 . 此小部件允许您跳过列表中的一定数量的帖子,因此我将其设置为从第3个帖子开始 . 没有选项只显示当前类别的帖子,因此我也必须使用Widget Context插件,并使用特定类别制作单独的小部件以显示在每个相应的类别页面上 . 正如我所说,有点复杂的解决方案,但最终的结果正是我想要实现的 .