自定义后置类型继承(和URL结构)

时间:2015-10-08 作者:Frederik Spang

在查看了文档并四处搜索之后,我没有找到任何东西,所以下面是:

是否有任何自定义post类型继承的方法,以及类似以下url结构:

domain.com/%parenttype%/%parentname%/%posttype/%postname%/
假设艺术家有歌曲,看起来像:

domain.com/artist/john-lennon/songs/imagine/
我以前从未在开发中看到过这种情况。有人知道这件事吗?

1 个回复
SO网友:iantsch

我们开始吧:

注册您的两个CPT(=自定义帖子类型),例如艺术家、歌曲,将子CPT中的重写段标编辑为parent-type/%parent-slug%/child-slug (Attention: 不要使用相同的单词parent-typeparent-slug; parent-slug 应与您的家长CPT slug匹配)为您的孩子CPT添加一个元框,您可以在其中定义家长CPT,根据您的需要修改孩子CPT permalink

class myArtistSongs{

    function init_post_types() {
        $args = array(
            \'menu_position\' => 8,
            \'labels\' => array(
                \'name\' => __(\'Songs\', \'myArtistSongs\'),
                \'all_items\' => __(\'All Songs\', \'myArtistSongs\'),
                \'singular_name\' => __(\'Song\', \'myArtistSongs\')
            ),
            \'menu_icon\' => \'dashicons-format-audio\',
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'query_var\' => false,
            \'capability_type\' => \'post\',
            \'has_archive\' => false,
            \'rewrite\' => array(
                \'slug\' => \'artist/%artist%/songs\',
                \'feeds\' => true,
                \'pages\' => true,
                \'with_front\' => true,
            ),
            \'taxonomies\' => array(
                \'artist\'
            ),
            \'supports\' => array(
                \'title\',
                \'editor\',
                \'thumbnail\',
                \'excerpt\'
            )
        );
        register_post_type( \'song\', $args);
        $args = array(
            \'menu_position\' => 8,
            \'labels\' => array(
                \'name\' => __(\'Artists\', \'myArtistSongs\'),
                \'all_items\' => __(\'All Artists\', \'myArtistSongs\'),
                \'singular_name\' => __(\'Artist\', \'myArtistSongs\')
            ),
            \'menu_icon\' => \'dashicons-groups\',
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'query_var\' => false,
            \'capability_type\' => \'post\',
            \'has_archive\' => false,
            \'rewrite\' => array(
                \'slug\' => \'artists\',
                \'feeds\' => true,
                \'pages\' => true,
                \'with_front\' => true,
            ),
            \'taxonomies\' => array(
                \'artist\'
            ),
            \'supports\' => array(
                \'title\',
                \'editor\',
                \'thumbnail\',
                \'excerpt\'
            )
        );
        register_post_type( \'artist\', $args);
    }

    static function song_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, \'%artist%\') === FALSE) return $permalink;
        $post = get_post($post_id);
        if (!$post) return $permalink;
        $artist = get_post_meta($post->ID, \'_artist\', true);
        if ($artist > 0) {
            $artist = get_post($artist);
            if (!$artist) $artist = false;
            else $artist = $artist->post_name;
        }
        if (!$artist) $artist = \'no-artist\';
        return str_replace(\'%artist%\', $artist, $permalink);
    }

    function add_meta_box() {
        add_meta_box(
            \'article-parent\',
            __(\'Artist\',\'\'),
            array(\'myArtistSongs\', \'meta_box\'),
            \'song\',
            \'side\',
            \'low\'
        );
    }

    function meta_box($post) {
        $artist = get_post_meta($post->ID, \'_artist\', true);
        global $wp_query, $post;
        $old_wp_query = $wp_query;
        $old_post = $post;
        $loop = new WP_Query(array(\'post_type\' => \'artist\', \'posts_per_page=-1\'));
        echo "<select name=\'artist\' id=\'artist\'>";
        $no_value = __(\'(no artist)\',\'myArtistSongs\');
        echo "<option value=\'\'".(empty($artist)?" selected=\'selected\'":\'\').">".$no_value."</option>";
        if($loop->have_posts()) while ($loop->have_posts()) {
            $loop->the_post();
            $o_id = get_the_ID();
            $o = get_the_title();
            echo "<option value=\'{$o_id}\'".(!empty($artist) && $artist == $o_id?" selected=\'selected\'":\'\').">{$o}</option>";
        }
        echo "</select>";
        $post = $old_post;
        $wp_query = $old_wp_query;
    }

    function save($post_id) {
        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return;
        if ( !current_user_can( \'edit_post\', $post_id ) )
            return;
        if ($_POST[\'artist\']) {
            update_post_meta($post_id, \'_artist\', $_POST[\'artist\']);
        } else {
            delete_post_meta($post_id, \'_artist\');
        }
    }
}

add_action(\'init\', array(\'myArtistSongs\', \'init_post_types\'));
add_filter(\'post_link\',  array(\'myArtistSongs\', \'song_permalink\'), 10, 3);
add_filter(\'post_type_link\',  array(\'myArtistSongs\', \'song_permalink\'), 10, 3);
add_action(\'add_meta_boxes\', array(\'myArtistSongs\', \'add_meta_box\'));
add_action( \'save_post\', array(\'myArtistSongs\',\'save\') );

相关推荐

Let me choose permalinks

我需要选择一个叫做“mysite”的永久链接。com/1418”,但wordpress不断在永久链接中添加“-2”。通常这意味着我已经有了一个名为“相同”的页面,它位于垃圾箱或其他地方。但这里的情况似乎并非如此。我尝试在设置中重置永久链接,这也没有帮助。我如何使用数字作为页面名称permalink,而不用wordpress在permalink中添加“-2”。