>我正在写一个wordpress博客(_的主题),并希望在所有页面上可用的菜单中显示每个类别的所有帖子
我正在使用以下代码
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<h4 class="category" id="' . $category->name.'" >' . $category->name.'</h4>';
$post_args = array(
'numberposts' => 5,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<p class="category-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
}
}
?>
此代码将完全按照我想要的方式显示和格式化列表。
问题...
1.)在索引页面上,如果这段代码出现在循环之前,它将使我的粘性帖子消失。
2.) 在单个帖子页面上,如果代码在循环之后出现,则循环后不会显示任何内容。
这很好,因为我可以使用条件来显示代码或取决于页面,但是......
3.)这段代码还使得单个帖子循环只会显示最近帖子的内容,但仅限于它在循环之前出现,否则我又回到问题2。
我尝试使用wp_reset_postdata()和wp_reset_query()遵循代码,但无济于事,并且我无法弄清楚如何使用codex中的多循环教程正确显示所有内容。
这也是我的单.php循环
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop. ?>
这是我的索引.php循环
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php wdh_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
请帮助我
<?php
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => 8, // specify category id here
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
foreach( $myposts as $post )
{
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
wp_reset_postdata();
?>