首页 > 解决方案 > Wordpress 嵌入帖子不适用于自定义帖子类型

问题描述

我试图让嵌入帖子在自定义帖子类型上工作。当我在编辑器中粘贴内部链接(自定义帖子类型)时,它会返回加载失败消息,因为它无法获取 url。它在默认的 wordpress 帖子上运行良好。

这是我的帖子类型的代码

<?php
/**
 * topics ====================================================
 */
add_action('init', 'my_custom_topics');
function my_custom_topics()
{
  $labels = array(
    'name' => _x('Topics', 'post type general name'),
    'singular_name' => _x('topics', 'post type singular name'),
    'add_new' => _x('新しくTopicsを書く', 'topics'),
    'add_new_item' => __('Topics記事を書く'),
    'edit_item' => __('Topics記事を編集'),
    'new_item' => __('新しいTopics記事'),
    'view_item' => __('Topics記事を見る'),
    'search_staff' => __('Topics記事を探す'),
    'not_found' =>  __('Topics記事はありません'),
    'not_found_in_trash' => __('ゴミ箱にTopics記事はありません'),
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array(
      "slug" => "topics",
      "with_front" => false
    ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title','editor','excerpt','thumbnail','revisions'),
    'has_archive' => true
  );
  register_post_type('topics',$args);
}

/** ---- Topicsタクソノミー追加 ---- **/
add_action ('init','create_topicscat_taxonomy','0');
function create_topicscat_taxonomy () {
    $taxonomylabels = array(
    'name' => _x('カテゴリー','post type general name'),
    'singular_name' => _x('カテゴリー','post type singular name'),
    'search_items' => __('カテゴリー'),
    'all_items' => __('Topicsカテゴリー'),
    'parent_item' => __( 'Parent Cat' ),
    'parent_item_colon' => __( 'Parent Cat:' ),
    'edit_item' => __('Topicsカテゴリーを編集'),
    'add_new_item' => __('Topicsカテゴリーを書く'),
    'menu_name' => __( 'Topicsカテゴリー' ),
    );
    $args = array(
    'labels' => $taxonomylabels,
    'hierarchical' => true,
    'has_archive' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topicscat' )
    );
    register_taxonomy('topicscat','topics',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_topics_category() {
$topics_category_args = get_taxonomy( 'topicscat' ); // returns an object

$topics_category_args->show_admin_topics = true;
$topics_category_args->rewrite['slug'] = 'topics';
$topics_category_args->rewrite['with_front'] = false;

register_taxonomy( 'topicscat', 'topics', (array) $topics_category_args );
}
add_action( 'init', 'rewrite_topics_category', 11 );

//tags taxonomy

add_action ('init','create_tagscat_taxonomy','0');
function create_tagscat_taxonomy () {
    $taxonomylabels = array(
    'name' => _x('カテゴリー','post type general name'),
    'singular_name' => _x('カテゴリー','post type singular name'),
    'search_items' => __('カテゴリー'),
    'all_items' => __('tagsカテゴリー'),
    'parent_item' => __( 'Parent Cat' ),
    'parent_item_colon' => __( 'Parent Cat:' ),
    'edit_item' => __('tagsカテゴリーを編集'),
    'add_new_item' => __('tagsカテゴリーを書く'),
    'menu_name' => __( 'tagsカテゴリー' ),
    );
    $args = array(
    'labels' => $taxonomylabels,
    'hierarchical' => true,
    'has_archive' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tagscat' )
    );
    register_taxonomy('tagscat','tags',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_tags_category() {
$tags_category_args = get_taxonomy( 'tagscat' ); // returns an object

$tags_category_args->show_admin_tags = true;
$tags_category_args->rewrite['slug'] = 'tags';
$tags_category_args->rewrite['with_front'] = false;

register_taxonomy( 'tagscat', 'topics', (array) $tags_category_args );
}
add_action( 'init', 'rewrite_tags_category', 11 );




// Get the Post ID from permalink structure base/CT/Post_ID
function topics_rewrites_init($post_link, $post = 0){
    add_rewrite_rule('topics\/([0-9]+)?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&post_type=topics&p=$matches[1]', 'top');
    add_rewrite_rule('topics\/([A-Za-z0-9-_]+)\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&topicscat=$matches[1]', 'top');
}
add_action('init', 'topics_rewrites_init');

function cms_links3($post_link, $post) {
    if($post->post_type === 'topics') {
        return home_url('topics/' . $post->ID . '/');
    } else {
        return $post_link;
    }
}
add_filter('post_type_link', 'cms_links3', 1, 3);

标签: wordpressembed

解决方案


推荐阅读