【问题标题】:Get_the_terms - display all post typesGet_the_terms - 显示所有帖子类型
【发布时间】: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


【解决方案1】:

假设我们有名为 portfolio 的自定义帖子类型和名为 portfolio_category 的自定义分类法

<?php $post_type = 'portfolio';
$post_taxonomy = 'portfolio_category';
//First get all terms of portfolio_category.
$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">
    <li><a href="#" data-filter="*" class="selected">All</a></li>
    <?php foreach($terms as $term) : ?>
         <li><a href='#' data-filter='.<?php echo $term->slug ?>'><?php echo $term->name ?></a></li>
    <?php endforeach; ?>
</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 $classes; ?> item col-md-3">
                            <h3><?php the_title(); ?></h3>
                            <?php if ( has_post_thumbnail() ) {
                                the_post_thumbnail();
                            } ?>
                        </div> <!-- end item -->
                    <?php endwhile;  ?>
                </div> <!-- end isotope-list -->
            <?php endif; ?>

【讨论】:

  • 谢谢。我相信我们在这里有所收获。我在自定义帖子中的帖子中添加了 post_taxonomy:Foto,它出现了。我使用 foto_category 作为分类法。但是,foreach 中将显示名称的列表无法正常工作。该页面可在此处找到:goo.gl/jytG7o(向下滚动直到看到 Siste prosjekt)。
  • 对不起,我错了。我的意思是我使用 foto-category 作为 slug,使用 foto 作为分类。
  • 我已经更新了这个问题。如果您愿意,请查看原始问题。
【解决方案2】:

问题已经解决了。

我终于用了下面的代码:

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

我需要做的改变并不多,但也有一些。

感谢所有发表评论并提出工作想法的人。

请注意,我还没有修复代码中的一些错误。

【讨论】:

    【解决方案3】:

    any 参数适用于 post_status ,我不认为它被 post_type ('post_status' =&gt; 'any') 接受 (其实有些版本没有正确实现)

    您可以给 post_type 一个包含某些类型的数组:

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

    【讨论】:

    • 感谢您的评论。我已经对其进行了测试,但它似乎不起作用。我也尝试用自定义的 post slug 替换数组,但这也不起作用......
    • 然后检查数据库,看看是什么让这些帖子不同(类别等)。
    • 我已经更新了这个问题。如果您愿意,请查看原始问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多