带自定义帖子类型的WordPress网站地图

时间:2011-09-13 作者:micah

这是一个非常好的主意,但我想进一步阐述一下。DavidKennedy在他的帖子中编写了一些代码,允许wp_list_pages 的类型功能Custom Post Types to use as a sitemap. 他的代码如下:

<h2 id="posts">My Post Type</h2>
    <ul>
    <?php 
        $terms = get_terms( \'my_taxonomy\', \'orderby=name\' );
        foreach ($terms as $term) {
            echo "<li><h3>".$term->name."</h3>";
            echo "<ul>";
            $args = array(
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $term->slug
                    )
                )
            );
            $new = new WP_Query($args);
                while ($new->have_posts()) {
                    $new->the_post();
                    echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                }
            echo "</ul>";
            echo "</li>";
        } ?>
        </ul>
我喜欢这个想法,但我想再进一步。最好增加层次结构的深度或级别,并显示父/子关系结果,而不是按字母顺序列出自定义帖子类型。换句话说,模拟更多wp_list_pages 提供。

有没有一种更简单的方法可以做到这一点,或者WordPress中有没有功能可以满足我的需求?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

此处有一条快速裂缝,应在两层深度内起作用:

<h2 id="posts">My Post Type</h2>
<ul>
<?php
    $not_in = array(); //to avoid naming the same post over and over
    //get top level terms
    $Parent_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => 0));
    foreach ($Parent_terms as $term) {
        echo "<li><h3>".$term->name."</h3>";
        echo "<ul>";
        //get all children of each term
        $child_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => $term->ID));
        foreach ((array)$child_terms as $t){
            echo "<li><h4>".$t->name."</h4>";
            echo "<ul>";
            $args = array(
                \'post__not_in\' => $not_in,
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $t->slug
                    )
                )
            );
            $new = new WP_Query($args);
            while ($new->have_posts()) {
                $new->the_post();
                $not_in[] = $post->ID;
                echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                //here you should check for children posts and output them 
            }
            echo "</ul>";
            echo "</li>";
        }
        //get all posts that are only listed in top level term
        $args = array(
            \'post__not_in\' => $not_in,
            \'post_type\' => \'my_posttype\',
            \'posts_per_page\' => -1,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'my_taxonomy\',
                    \'field\' => \'slug\',
                    \'terms\' => $term->slug
                    )
                )
        );
        $new = new WP_Query($args);
        while ($new->have_posts()) {
            $new->the_post();
            $not_in[] = $post->ID;
            echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
            //here you should check for children posts and output them 
        }
        echo "</ul>";
        echo "</li>";
    } ?>
 </ul>

结束

相关推荐

获取404.php而不是Single-<post-type>.php

试图找到一个帖子出现在http://beernews.org/supporter/rare-beer-club/ 但我得到的是404。页面外观的示例如下:http://beernews.org/brewery/5-rabbit-brewery/. 当我进行搜索时,它会在结果中显示为摘录。只是无法获得模板的单个支持者。php由于某种原因触发。我已经设置了与其他所有帖子类型和分类法完全相同的帖子类型(和相应的分类法)。我已刷新缓存和CDN。我没有用于此的实时服务器测试环境,但相同的代码可以在localhost

带自定义帖子类型的WordPress网站地图 - 小码农CODE - 行之有效找到问题解决它

带自定义帖子类型的WordPress网站地图

时间:2011-09-13 作者:micah

这是一个非常好的主意,但我想进一步阐述一下。DavidKennedy在他的帖子中编写了一些代码,允许wp_list_pages 的类型功能Custom Post Types to use as a sitemap. 他的代码如下:

<h2 id="posts">My Post Type</h2>
    <ul>
    <?php 
        $terms = get_terms( \'my_taxonomy\', \'orderby=name\' );
        foreach ($terms as $term) {
            echo "<li><h3>".$term->name."</h3>";
            echo "<ul>";
            $args = array(
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $term->slug
                    )
                )
            );
            $new = new WP_Query($args);
                while ($new->have_posts()) {
                    $new->the_post();
                    echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                }
            echo "</ul>";
            echo "</li>";
        } ?>
        </ul>
我喜欢这个想法,但我想再进一步。最好增加层次结构的深度或级别,并显示父/子关系结果,而不是按字母顺序列出自定义帖子类型。换句话说,模拟更多wp_list_pages 提供。

有没有一种更简单的方法可以做到这一点,或者WordPress中有没有功能可以满足我的需求?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

此处有一条快速裂缝,应在两层深度内起作用:

<h2 id="posts">My Post Type</h2>
<ul>
<?php
    $not_in = array(); //to avoid naming the same post over and over
    //get top level terms
    $Parent_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => 0));
    foreach ($Parent_terms as $term) {
        echo "<li><h3>".$term->name."</h3>";
        echo "<ul>";
        //get all children of each term
        $child_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => $term->ID));
        foreach ((array)$child_terms as $t){
            echo "<li><h4>".$t->name."</h4>";
            echo "<ul>";
            $args = array(
                \'post__not_in\' => $not_in,
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $t->slug
                    )
                )
            );
            $new = new WP_Query($args);
            while ($new->have_posts()) {
                $new->the_post();
                $not_in[] = $post->ID;
                echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                //here you should check for children posts and output them 
            }
            echo "</ul>";
            echo "</li>";
        }
        //get all posts that are only listed in top level term
        $args = array(
            \'post__not_in\' => $not_in,
            \'post_type\' => \'my_posttype\',
            \'posts_per_page\' => -1,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'my_taxonomy\',
                    \'field\' => \'slug\',
                    \'terms\' => $term->slug
                    )
                )
        );
        $new = new WP_Query($args);
        while ($new->have_posts()) {
            $new->the_post();
            $not_in[] = $post->ID;
            echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
            //here you should check for children posts and output them 
        }
        echo "</ul>";
        echo "</li>";
    } ?>
 </ul>

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请

带自定义帖子类型的WordPress网站地图 - 小码农CODE - 行之有效找到问题解决它

带自定义帖子类型的WordPress网站地图

时间:2011-09-13 作者:micah

这是一个非常好的主意,但我想进一步阐述一下。DavidKennedy在他的帖子中编写了一些代码,允许wp_list_pages 的类型功能Custom Post Types to use as a sitemap. 他的代码如下:

<h2 id="posts">My Post Type</h2>
    <ul>
    <?php 
        $terms = get_terms( \'my_taxonomy\', \'orderby=name\' );
        foreach ($terms as $term) {
            echo "<li><h3>".$term->name."</h3>";
            echo "<ul>";
            $args = array(
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $term->slug
                    )
                )
            );
            $new = new WP_Query($args);
                while ($new->have_posts()) {
                    $new->the_post();
                    echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                }
            echo "</ul>";
            echo "</li>";
        } ?>
        </ul>
我喜欢这个想法,但我想再进一步。最好增加层次结构的深度或级别,并显示父/子关系结果,而不是按字母顺序列出自定义帖子类型。换句话说,模拟更多wp_list_pages 提供。

有没有一种更简单的方法可以做到这一点,或者WordPress中有没有功能可以满足我的需求?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

此处有一条快速裂缝,应在两层深度内起作用:

<h2 id="posts">My Post Type</h2>
<ul>
<?php
    $not_in = array(); //to avoid naming the same post over and over
    //get top level terms
    $Parent_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => 0));
    foreach ($Parent_terms as $term) {
        echo "<li><h3>".$term->name."</h3>";
        echo "<ul>";
        //get all children of each term
        $child_terms = get_terms( \'my_taxonomy\', array(\'orderby\' => \'name\',\'parent\' => $term->ID));
        foreach ((array)$child_terms as $t){
            echo "<li><h4>".$t->name."</h4>";
            echo "<ul>";
            $args = array(
                \'post__not_in\' => $not_in,
                \'post_type\' => \'my_posttype\',
                \'posts_per_page\' => -1,
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'my_taxonomy\',
                        \'field\' => \'slug\',
                        \'terms\' => $t->slug
                    )
                )
            );
            $new = new WP_Query($args);
            while ($new->have_posts()) {
                $new->the_post();
                $not_in[] = $post->ID;
                echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
                //here you should check for children posts and output them 
            }
            echo "</ul>";
            echo "</li>";
        }
        //get all posts that are only listed in top level term
        $args = array(
            \'post__not_in\' => $not_in,
            \'post_type\' => \'my_posttype\',
            \'posts_per_page\' => -1,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'my_taxonomy\',
                    \'field\' => \'slug\',
                    \'terms\' => $term->slug
                    )
                )
        );
        $new = new WP_Query($args);
        while ($new->have_posts()) {
            $new->the_post();
            $not_in[] = $post->ID;
            echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
            //here you should check for children posts and output them 
        }
        echo "</ul>";
        echo "</li>";
    } ?>
 </ul>