我正在尝试自定义索引。我的主题的php,以便只返回/显示一种帖子格式(本例中为图像),并隐藏所有其他格式(旁白、视频、链接…)。我试图在索引页中实现此代码,但出现了错误。我希望有人能帮忙。
$args = array(
\'post_type\'=> \'post\',
\'post_status\' => \'publish\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-image\' ),
)
)
);
索引代码。php
<div class="content">
<?php if (have_posts()) : ?>
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title">
<h4><?php printf( __(\'Page %s of %s\', \'fukasawa\'), $paged, $wp_query->max_num_pages ); ?></h4>
</div> <!-- /page-title -->
<div class="clear"></div>
<?php endif; ?>
<div class="posts" id="posts">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( \'content\', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div> <!-- /posts -->
SO网友:zak ben
我解决了这个问题。我只需要添加以下代码行query_posts( $args );
结果如下:
<div class="content">
<?php if (have_posts()) : ?>
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title">
<h4><?php printf( __(\'Page %s of %s\', \'fukasawa\'), $paged, $wp_query->max_num_pages ); ?></h4>
</div> <!-- /page-title -->
<div class="clear"></div>
<?php endif; ?>
<?php
$args = array(
\'post_type\'=> \'post\',
\'post_status\' => \'publish\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-image\' ),
)
)
);
query_posts( $args );
?>
<div class="posts" id="posts">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( \'content\', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div> <!-- /posts -->