通过插件创建自定义帖子模板和自定义帖子类型

时间:2018-10-18 07:54:31

标签: php wordpress custom-post-type

我想创建一个CPT(自定义帖子类型)及其模板以显示由我的帖子类型创建的单个帖子。

更多详细信息:

  1. 我的插件需要创建CPT,因此我不必依赖于更改主题文件夹并在主题文件夹中生成single-xxx.php文件。
  2. 简化内容的要求仍然存在。具有2个输入字段(例如密钥和机密),当用户在创建新帖子时输入时,应在查看帖子时显示。用户可以创建多个类似的帖子。
  3. 此外(虽然现在不是必需的),从更大的角度来看,我试图实现的是生成这些帖子类型的简码并将其用于内容中。.

PS:我是wordpress环境的新手,当我尝试查找信息时,信息不胜枚举,超出了我的理解范围。

如果有人可以帮助我,请保持代码相当简单并解释步骤。如果需要,可以将其保存在一个文件中。

如果我错过任何可能有助于更好地理解我的问题的细节,也大喊大叫...

到目前为止我尝试过的代码。

add_action( 'init', 'myplugin_widgets_plugin_init' );
function myplugin_widgets_plugin_init() {
register_post_type( 'myplugin_widgets',
    array(
        'labels' => array(
            'name' => 'myplugin Widgets',
            'singular_name' => 'myplugin View',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New View',
            'edit' => 'Edit',
            'edit_item' => 'Edit View',
            'new_item' => 'New View',
            'view' => 'View',
            'view_item' => 'View',
            'search_items' => 'Search an myplugin view',
            'not_found' => 'No views found',
            'not_found_in_trash' => 'No views found in Trash',
            'parent' => 'Parent Movie Review'
        ),

        'public' => true,
        'menu_position' => 15,
        'supports' => array( 'title'),
        'taxonomies' => array( '' ),
        'has_archive' => false
    )
);
}

function getmypluginPostTypeTemplate($single_template) {
   global $wp_query, $post;
   $single_template = plugins_url( 'templates/mypluginwidgetdisplay.php', __FILE__ );
  if ($post->post_type == 'myplugin_widgets'){
      $single_template = plugins_url( 'templates/mypluginwidgetdisplay.php', __FILE__ );
  }
  return $single_template;
 }
 add_filter( 'single_template', 'getmypluginPostTypeTemplate' );

预先感谢

0 个答案:

没有答案