【发布时间】:2015-09-21 00:03:48
【问题描述】:
问题已更新。最新版本见下文
我在使用自定义帖子时无法查看所有帖子类型。这是基于同位素的,用户应该点击链接来查看该类别中的帖子。
出现由 Wordpress 标准帖子创建的所有帖子,但没有使用类型创建的帖子(自定义帖子)。
<ul id="filters" class="whitetext whitelink myeluft">
<li><a href="#" data-filter="*" class="selected">Alle</a></li>
<li><a href='#' data-filter='.foto'>Foto</a></li>
<li><a href='#' data-filter='.video'>Video</a></li>
<li><a href='#' data-filter='.web'>Web</a></li>
</ul>
<?php $the_query = new WP_Query( 'posts_per_page=50' ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
// Query posts - post_types
$anypost = get_posts( array(
'post_type' => 'any' // every post type, but not attachments
) );
$termsArray = get_the_terms( $post->ID, "category", $anypost); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> item col-md-3"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</div> <!-- end item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
<?php endif; ?>
如您所见,我尝试通过插入以下代码来修复它,但它仍然没有显示所有帖子类型。
// Query posts - post_types
$anypost = get_posts( array(
'post_type' => 'any' // every post type, but not attachments
) );
$termsArray = get_the_terms( $post->ID, "category", $anypost); //Get the terms for this particular item
我看过this article,但发现自己比开始更失落/
什么是可行的解决方案?
更新
通过使用以下代码,我可以查看所有帖子,但无法将它们过滤掉。在此处查看页面:http://goo.gl/e3cLuM(向下滚动直到看到所有帖子)
<?php $post_type = 'any';
$post_taxonomy = 'any';
// Get all
$terms = get_terms( $post_taxonomy );
$portfolio = new WP_Query('post_type='.$post_type.'&post_per_page=-1'); ?>
// First we loop our porfolio_category to show all categories as filter.
<ul id="filters" class="whitetext whitelink myeluft">
<a href="#" data-filter="*" class="selected"><li class="smoothtrans">Alle</li></a>
<a href='#' data-filter='.foto'><li class="smoothtrans">Foto</li></a>
<a href='#' data-filter='.video'><li class="smoothtrans">Video</li></a>
<a href='#' data-filter='.web'><li class="smoothtrans">Web</li></a>
</ul>
<?php if ( $portfolio->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $portfolio->have_posts() ) : $portfolio->the_post();
// Get current post terms.
$item_terms = wp_get_post_terms( get_the_ID(), $post_taxonomy, $args );
$classes = '';
// Append classes to use with each item.
foreach($item_terms as $item_term ){
$classes .= $item_term->slug.' ';
}
?>
<div class="<?php echo $termsString; ?> item col-md-4">
<ul class="grid cs-style-3">
<li>
<figure>
<?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption class="lefttext">
<h3><?php the_title(); ?></h3>
<span class="offgrey">Nettside</span>
<a href="#" class="smoothtrans">Se prosjekt</a>
</figcaption>
</figure>
</li>
</ul>
</div> <!-- end item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
<?php endif; ?>
【问题讨论】:
-
您是否对自定义帖子类型使用自定义分类法?
-
感谢您的回答。这是负面的,但如果有必要使其发挥作用,可以添加分类法。
-
您是否为 WP_Query 尝试过 array('posts_per_page'=>50,'post_type'=>any)
-
我相信该代码中的某处存在语法错误。至少它没有工作。
-
这到底是在哪个模板上
标签: php wordpress custom-post-type