Get_the_terms - 显示所有帖子类型

时间:2015-07-03 08:14:08

标签: php wordpress custom-post-type

问题已更新。请参阅以下最新版本

使用自定义帖子时,查看所有帖子类型时遇到麻烦。这基于同位素,用户应该点击链接查看该类别中的帖子。

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; ?>

3 个答案:

答案 0 :(得分:4)

假设我们有名为class People(models.Model): name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) creator = models.ForeignKey(settings.AUTH_USER_MODEL) class Observation(models.Model): name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) creator = models.ForeignKey(settings.AUTH_USER_MODEL) people = models.ForeignKey( 'people.People', related_name='observations' ) 的自定义帖子类型和名为portfolio的自定义分类

portfolio_category

答案 1 :(得分:3)

问题已经解决了。

我终于使用了以下代码:

<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
$terms = get_terms( $post_taxonomy );

$args = array(
    'post_type' => 'any',
    'posts_per_page' => 6,
    'post_taxonomy' => 'any',
);

$the_query = new WP_Query($args); 


// Loop post_type
?>

<?php if ( $the_query->have_posts() ) : ?>
    <div id="isotope-list">
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 

        $termsArray = get_the_terms( $post->ID, "category");  //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-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"><?php echo(types_render_field( "produkt", array( 'raw' => true) )); ?> / <?php echo(types_render_field( "produsert", array( 'raw' => true) )); ?></span>
                            <a href="<?php the_permalink() ?>" rel="bookmark" class="smoothtrans">Se prosjekt</a>
                        </figcaption>
                    </figure>
                </li>
            </ul>             
        </div> <!-- end item -->
        <?php endwhile;  ?>
    </div> <!-- end isotope-list -->
    <script src="<?php bloginfo('stylesheet_directory'); ?>/js/toucheffects.js"></script>
<?php endif; ?>

我不得不做很多改变,但也有一些改变。

感谢所有评论并带来工作思路的人。

请注意,我尚未解决的代码中存在一些错误。

答案 2 :(得分:0)

any参数适用于post_status,我认为post_type'post_status' => 'any')不接受此参数 (实际上,它在某些版本中没有正确实现)

您可以为 post_type 提供一些包含某些类型的数组:

'post_type' => array('post','page','event')