Taxonomies in URL

时间:2013-08-17 作者:urok93

我有一个自定义的帖子类型和两个与之相关的自定义分类法。一个是地点,另一个是活动。如何按以下格式形成永久链接?

http://mysite.com/usa/diving

因此,第一个是位置分类法,第二个是活动分类法?

4 个回复
SO网友:Matthew Boynes

我最近在几个文字夏令营中谈到了这一点。Here\'s my talk from WC Portland, 和the slides, source code, etc. 具体查看配方#2。

首先,您可能应该有一个静态前缀来启动URL。这不是百分之百必要的,但如果没有它,您的重写将与页面永久链接冲突。如果您决定使用静态前缀,您可能希望将其设置为post类型,因为这是您的分类法组织的内容。我不知道那是什么,所以我猜测这是关于“导游”的在这种情况下,您的URI将类似于/guides/usa/diving/。

这里有一些代码可以帮助您开始。如果有什么不明白的地方,你应该看演示文稿,并跟着幻灯片一起看。

<?php

/**
 * Setup custom permalinks with multiple taxonomies
 */

if ( !class_exists( \'WPSE_110508\' ) ) :

class WPSE_110508 {

    private static $instance;

    public static function instance() {
        if ( ! isset( self::$instance ) ) {
            self::$instance = new WPSE_110508;
            self::$instance->setup();
        }
        return self::$instance;
    }

    public function setup() {
        add_action( \'init\', array( $this, \'structures\' ) );
        add_filter( \'post_type_link\', array( $this, \'post_type_link\' ), 10, 2 );
        add_filter( \'term_link\', array( $this, \'term_link\' ), 10, 3 );
    }

    /**
     * Register our data structures
     *
     * @return void
     */
    function structures() {
        register_taxonomy( \'location\', \'guide\', array(
            \'label\' => \'Body Types\',
            \'rewrite\' => array( \'slug\' => \'guides\' )
        ) );

        add_permastruct( "all_activities", "guides/all/%activity%" );
        register_taxonomy( \'activity\', \'guide\', array(
            \'label\' => \'Activities\',
            \'rewrite\' => array( \'slug\' => \'guides/%location%\' )
        ) );

        register_post_type( \'guide\', array(
            \'public\' => true,
            \'label\' => \'Guides\',
            \'rewrite\' => array( \'slug\' => \'guides/%location%/%activity%\' )
        ) );
    }


    /**
     * Filter post type links for guides to replace %location% & %activity% if present
     *
     * @param string $link
     * @param object $post
     * @return string
     */
    function post_type_link( $link, $post ) {
        if ( \'guide\' == $post->post_type ) {
            if ( $locations = get_the_terms( $post->ID, \'location\' ) ) {
                $link = str_replace( \'%location%\', array_pop( $locations )->slug, $link );
            }
            if ( $activities = get_the_terms( $post->ID, \'activity\' ) ) {
                $link = str_replace( \'%activity%\', array_pop( $activities )->slug, $link );
            }
        }
        return $link;
    }


    /**
     * Filter term links for activities to replace %location% with "all"
     *
     * @param string $termlink
     * @param object $term
     * @param string $taxonomy
     * @return string
     */
    function term_link( $termlink, $term, $taxonomy ) {
        if ( \'activity\' == $taxonomy ) {
            return str_replace( \'%location%\', \'all\', $termlink );
        }
        return $termlink;
    }

}

function WPSE_110508() {
    return WPSE_110508::instance();
}
add_action( \'after_setup_theme\', \'WPSE_110508\' );

endif;

SO网友:henrywright

您似乎使用了两种非层次化的自定义分类法。尝试使用单一的分层自定义分类法(想象一下类别是如何工作的)。这样,您的URL就会看起来像“美国/潜水”。

当您通过主题函数注册自定义分类法时。php,执行以下操作:

\'hierarchical\' => true,

SO网友:Bianca Petroiu

这个php函数是这样的<?php get_term_link( $term, $taxonomy ); ?> 这里有一个如何将其用于代码的示例:$terms=get\\u terms(\'species\');

echo \'<ul>\';
foreach ($terms as $term) {
    //Always check if it\'s an error before continuing. get_term_link() can be finicky sometimes
    $term_link = get_term_link( $term, \'species\' );
    if( is_wp_error( $term_link ) )
        continue;
    //We successfully got a link. Print it out.
    echo \'<li><a href="\' . $term_link . \'">\' . $term->name . \'</a></li>\';
}
echo \'</ul>\';

结束

相关推荐