本文介绍了如何自动将自定义页面 slug 自动分配给 WordPress 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢 这篇文章 我有这个 php 代码可以在我的自定义主题激活时自动生成一些页面(效果很好)但是我也希望也能够自定义为每个页面创建的 slug,例如页面 Tool Brands
我希望 slug 只是 brands
这很重要有几个原因,1)为了便于客户使用 www.domain.com/brands 和 2) 我的页面标题是根据页面名称自动生成的,所以我想保留 Tool Brands
作为页面名称,但在这种情况下,它还会创建一个我不想要的 slug tool-brands
.
感谢您的帮助 - 因为我对 PHP 几乎一无所知,所以将它清楚地写出来对我来说将是一个很大的帮助.
代码在下面,也在 PasteBin
'页','post_status' =>'发布','post_title' =>ucwords($page_title),'post_name' =>strtolower(trim($page_title)),'post_content' =>$page_content,);$page_id = wp_insert_post($page_args);返回 $page_id;}//第二段代码add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');函数 my_custom_pages_on_theme_activation(){$pages_array = 数组('家' =>'','新闻' =>'','产品' =>'','政策' =>'','服务' =>'','工具品牌' =>'',);foreach ($pages_array as $page_title => $page_content) {$current_page = your_theme_create_page($page_title, $page_content);如果(假!= $current_page){add_action('admin_notices', function () use ($page_title) {?><div class="notice notice-success is-dismissible"><p><?php echo "完成!{$page_title} 已创建";?></p>
<?php});} 别的 {add_action('admin_notices', function () use ($page_title) {?><div class="notice notice-warning is-dismissible"><p><?php echo "{$page_title} 未创建!检查它是否已经存在";?></p>
<?php});}}$blog_page = get_page_by_title('新闻', 'OBJECT', 'page');如果($blog_page){update_option('page_for_posts', $blog_page->ID);}$front_home_page = get_page_by_title('home', 'OBJECT', 'page');如果($front_home_page){update_option('page_on_front', $front_home_page->ID);update_option('show_on_front', 'page');}}
解决方案
您需要在带有 slug 的 your_theme_create_page() 函数中再添加一个变量.并将数组格式更改为多维
'页','post_status' =>'发布','post_title' =>ucwords($page_title),'post_name' =>$new_post_name,'post_content' =>$page_content,);$page_id = wp_insert_post($page_args);返回 $page_id;}//第二段代码add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');函数 my_custom_pages_on_theme_activation(){$pages_array = 数组(1"=>大批(post_title"=>'这里的主页标题',post_content"=>'首页内容在这里',post_name"=>'home_slug_here',),2"=>大批(post_title"=>'新闻标题在这里',post_content"=>'新闻内容在这里',post_name"=>'news_slug_here',),3"=>大批(post_title"=>'这里的标题 3',post_content"=>'这里的内容 3',post_name"=>'slug_here3',),);foreach ($pages_array 作为 $page_array) {$page_title = $page_array['post_title'];$page_content = $page_array['post_content'];$new_post_name = $page_array['post_name'];$current_page = your_theme_create_page($page_title, $page_content, $new_post_name);如果(假!= $current_page){add_action('admin_notices', function () use ($page_title) {?><div class="notice notice-success is-dismissible"><p><?php echo "完成!{$page_title} 已创建";?></p>
<?php});} 别的 {add_action('admin_notices', function () use ($page_title) {?><div class="notice notice-warning is-dismissible"><p><?php echo "{$page_title} 未创建!检查它是否已经存在";?></p>
<?php});}}$blog_page = get_page_by_title('新闻', 'OBJECT', 'page');如果($blog_page){update_option('page_for_posts', $blog_page->ID);}$front_home_page = get_page_by_title('home', 'OBJECT', 'page');如果($front_home_page){update_option('page_on_front', $front_home_page->ID);update_option('show_on_front', 'page');}}
Thanks to This article I have this php code to auto generate some pages upon my custom theme's activation (Works great) however I would like to also be able to also customize the slugs that get created for each page for example for the page Tool Brands
I would like the slug to be just brands
This is important for a few reasons, 1) For ease of customer use www.domain.com/brands and 2) My page titles are auto generated based on the page name so I would like to keep the Tool Brands
as the page name but in this case it would also create a slug tool-brands
which I don't want.
Thanks for any help - also having it clearly wrote out for me would be a big help as I barely know PHP.
Code is below and also on PasteBin
<?php
declare(strict_types=1);
function your_theme_create_page($page_title, $page_content)
{
$page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
if ($page_obj) {
return false;
exit();
}
$page_args = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => ucwords($page_title),
'post_name' => strtolower(trim($page_title)),
'post_content' => $page_content,
);
$page_id = wp_insert_post($page_args);
return $page_id;
}
// Second Section of Code
add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
function my_custom_pages_on_theme_activation()
{
$pages_array = array(
'Home' => '',
'News' => '',
'Products' => '',
'Policy' => '',
'Services' => '',
'Tool Brands' => '',
);
foreach ($pages_array as $page_title => $page_content) {
$current_page = your_theme_create_page($page_title, $page_content);
if (false != $current_page) {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-success is-dismissible">
<p><?php echo "Done! {$page_title} has been created"; ?></p>
</div>
<?php
});
} else {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-warning is-dismissible">
<p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
</div>
<?php
});
}
}
$blog_page = get_page_by_title('news', 'OBJECT', 'page');
if ($blog_page) {
update_option('page_for_posts', $blog_page->ID);
}
$front_home_page = get_page_by_title('home', 'OBJECT', 'page');
if ($front_home_page) {
update_option('page_on_front', $front_home_page->ID);
update_option('show_on_front', 'page');
}
}
解决方案
You need to add one more variable to your_theme_create_page() function with slug.And change array format to multidimensional
<?php
declare(strict_types=1);
function your_theme_create_page($page_title, $page_content, $new_post_name)
{
$page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
if ($page_obj) {
return false;
exit();
}
$page_args = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => ucwords($page_title),
'post_name' => $new_post_name,
'post_content' => $page_content,
);
$page_id = wp_insert_post($page_args);
return $page_id;
}
// Second Section of Code
add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
function my_custom_pages_on_theme_activation()
{
$pages_array = array(
"1" => array(
"post_title" => 'Home title here',
"post_content" => 'Home content here',
"post_name" => 'home_slug_here',
),
"2" => array(
"post_title" => 'News title here',
"post_content" => 'News content here',
"post_name" => 'news_slug_here',
),
"3" => array(
"post_title" => 'title here 3',
"post_content" => 'content here 3',
"post_name" => 'slug_here3',
),
);
foreach ($pages_array as $page_array) {
$page_title = $page_array['post_title'];
$page_content = $page_array['post_content'];
$new_post_name = $page_array['post_name'];
$current_page = your_theme_create_page($page_title, $page_content, $new_post_name);
if (false != $current_page) {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-success is-dismissible">
<p><?php echo "Done! {$page_title} has been created"; ?></p>
</div>
<?php
});
} else {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-warning is-dismissible">
<p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
</div>
<?php
});
}
}
$blog_page = get_page_by_title('news', 'OBJECT', 'page');
if ($blog_page) {
update_option('page_for_posts', $blog_page->ID);
}
$front_home_page = get_page_by_title('home', 'OBJECT', 'page');
if ($front_home_page) {
update_option('page_on_front', $front_home_page->ID);
update_option('show_on_front', 'page');
}
}
这篇关于如何自动将自定义页面 slug 自动分配给 WordPress 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!