wordpress - 如何通过 wordpress 中的自定义插件创建自定义艺术家页面
问题描述
我正在研究更多并使用代码..我已经完成了这段代码。
if (!defined('ABSPATH')) {
die;
}
if(!class_exists('ArtistPublic')):
class ArtistPublic
{
public static function createTemplate( ) {
global $user_ID;
$new_post = array(
'post_title' => 'Artist',
'post_content' => 'hello Artist',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
);
$post_id = wp_insert_post($new_post);
if( !$post_id )
wp_die('Error creating template page');
else
update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
// Filter page template
// add_filter('page_template', 'catch_plugin_template');
}
// Page template filter callback
public static function catch_plugin_template($template) {
if( is_page_template('artist.php') )
$template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
return $template;
}
}
endif;
但刷新后创建多个页面。有没有像 woocommerce 那样创建页面的好方法?
解决方案
所以代码就像什么
if (!defined('ABSPATH')) {
die;
}
if(!class_exists('ArtistPublic')):
class ArtistPublic {
public static function createTemplate( ) {
if( get_option('artist_page_id') ) :
global $user_ID;
$new_post = array(
'post_title' => 'Artist',
'post_content' => 'hello Artist',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
);
$post_id = wp_insert_post($new_post);
if( !$post_id ) :
wp_die('Error creating template page');
else :
update_option('artist_page_id', $post_id);
update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
endif;
endif;
// Filter page template
// add_filter('page_template', 'catch_plugin_template');
}
// Page template filter callback
public static function catch_plugin_template($template) {
if( is_page_template('artist.php') )
$template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
return $template;
}
}
endif;
推荐阅读
- asp.net-mvc - ASP.NET MVC / Core and Entity Framework 6 / Core - 配置和验证
- python - 字段值在列表中的 Django 查询集
- arrays - MongoDB具有唯一值的多个数组
- python - 在给定一条线的情况下找到周围的矩形区域
- ruby-on-rails - 将 has_many 更改为 has_and_belongs_to_many
- python - 蟒蛇和熊猫。检查列“name”和“last_name”中的值是否在“email”列中
- adal - 当您使用 AuthenticationContext.AcquireTokenAsync(...) 强制登录时,TokenCache 中实际更改或更新了什么?
- haskell - GHC 是否支持无 GC 编程?
- java - 在 Java 中使用递归泛型时的类型不兼容
- android - 使用毕加索加载时图像未保存到存储中