某一类别页面上的所有帖子

问题描述 投票:0回答:3

我实在想不通这个问题。我试图将一个类别的所有帖子发布到一个页面上,但我仍然得到所有类别。这是我现在使用的代码。我以为我可以用 WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) ); 进行管理,但这完全让我发疯。

<?php /* Template Name: Blog */ ?>
<?php get_header(); ?>
<div id="content-wrap">
<div id="content">
<div class="post_content">
<h1 class="archive_title"><?php the_title(); ?></h1>
<?php
$query['post_type'] = 'post';
// WP 3.0 PAGED BUG FIX
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query['paged'] = $paged;

$recipe_posts = new WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) );
while ( $recipe_posts->have_posts() ) { $recipe_posts->the_post(); }

query_posts($query);
if (have_posts()) : ?>
<?php $more = 0; ?>
<div class="posts">
<?php while (have_posts()) : the_post();
$is_recipe = in_category('Resep'); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
<?php if (option::get('index_thumb') == 'on') {
get_the_image( array( 'size' => 'loop', 'width' => option::get('thumb_width'), 'height' => option::get('thumb_height'), 'before' => '<div class="post-thumb">', 'after' => '</div>' ) );
} ?>
<div class="details">
<h2 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php if ( option::get('display_meta') == 'on' ) { ?>
<div class="meta">
<?php
if ( $is_recipe ) {
$fields = get_fields();
if ( !empty( $fields ) ) echo $fields;
} else { ?>
<p><strong><img src="<?php echo get_template_directory_uri() . '/images/person.png'; ?>" /><?php _e('Author', 'wpzoom'); ?>:</strong><?php the_author_posts_link(); ?></p>
<p><strong><img src="<?php echo get_template_directory_uri() . '/images/clock.png'; ?>" />
<?php _e('Posted', 'wpzoom'); ?>
:</strong> <?php echo get_the_date(); ?></p>
<?php } ?>
</div>
<?php } ?>
<div class="entry">
<?php the_content('<span>'.__('Read More', 'wpzoom').' &#8250;</span>'); ?>
</div>
<p>
<?php if ( option::get('display_readmore') == 'on' && (option::get('display_content') == 'Excerpt') ) { ?>
<a href="<?php the_permalink(); ?>" class=" clean more-link">
<?php _e( ( $is_recipe ? 'Lihat Resep' : 'Read More' ), 'wpzoom' ); ?>
</a>
<?php } ?>
<?php edit_post_link( __('Edit', 'wpzoom'), ' <small>', '</small>' ); ?>
</p>
</div>
<div class="cleaner">&nbsp;</div>
</div>
<!-- /.post -->
<?php endwhile; ?>
</div>
<div class="cleaner">&nbsp;</div>
<?php get_template_part( 'pagination' ); ?>
<?php wp_reset_query(); ?>
<div class="cleaner">&nbsp;</div>
<?php endif; ?>
</div><!-- / .post_content -->
</div><!-- / #content -->
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
php wordpress
3个回答
2
投票

你让它变得太复杂了,只需在 while 循环中指定你需要的东西: $recipe_posts = new WP_Query( array( 'posts_per_page' => -1, 'category_name' => 'resep' ) ); while ( $recipe_posts->have_posts() ) { $recipe_posts->the_post(); echo '<li>' . get_the_title() . '</li>'; } wp_reset_query();



1
投票


$args = array('posts_per_page' => -1 , 'category_name' => 'resep'); $recipe_posts = new WP_Query($args); if($recipe_posts->have_posts()) : while($recipe_posts->have_posts()) : $recipe_posts->the_post(); ?> <h1><?php the_title() ?></h1> <div class='post-content'><?php the_content() ?></div> <?php endwhile; else: ?> Oops, there are no posts. <?php endif; ?>



0
投票

global $wp_query; $args = array_merge( $wp_query->query, array( 'category_name' => 'resep' ) ); query_posts( $args ); $recipe_posts = new WP_Query($args); $more = 0; if($recipe_posts->have_posts()) : while ($recipe_posts->have_posts()) : $recipe_posts->the_post();?>

© www.soinside.com 2019 - 2024. All rights reserved.