【问题标题】:How to auto assign custom page slugs to WordPress pages automatically?如何自动将自定义页面 slug 自动分配给 WordPress 页面?
【发布时间】:2021-09-22 02:32:51
【问题描述】:

感谢This article 我有这个 php 代码可以在我的自定义主题激活时自动生成一些页面(效果很好)但是我还希望能够自定义为每个页面创建的 slug,例如页面Tool Brands我希望slug只是brands这很重要,原因有几个,1)为了方便客户使用www.domain.com/brands和2)我的页面标题是根据页面名称自动生成的,所以我会喜欢将 Tool Brands 保留为页面名称,但在这种情况下,它也会创建一个我不想要的 slug tool-brands

感谢您的帮助 - 因为我几乎不了解 PHP,所以清楚地写出来对我来说也是一个很大的帮助。

代码在下面和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');
 
    }
 
}

【问题讨论】:

  • post_name 参数是映射到 slug 的内容
  • 我必须改变什么才能设置我自己的自定义 slug?

标签: php wordpress wordpress-theming


【解决方案1】:

您需要使用 slug 向 your_theme_create_page() 函数再添加一个变量。 并将数组格式更改为多维

<?php
declare(strict_types=1);
function your_theme_create_page($page_title, $page_content, $new_post_name, $new_post_status)
{
    $page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
 
    if ($page_obj) {
 
        return false;
 
        exit();
 
    }
 
    $page_args = array(
 
        'post_type'      => 'page',
 
        'post_status'    => $new_post_status,
 
        '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',
        "post_status" => 'private',
    ),          
    "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'];
 if(isset($page_array['post_status'])) {
 $new_post_status= $page_array['post_status'];
 } else  {
 $new_post_status = 'publish';
 }
        $current_page = your_theme_create_page($page_title, $page_content, $new_post_name, $new_post_status );
 
        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');
 
    }
 
}

【讨论】:

  • 非常感谢您在这方面的帮助!该代码开箱即用!
  • 所以我有一个帖子名称Develop,我想将其发布为“私人”,但这在数组中对我不起作用:"post_visibility" =&gt; 'private', 另见link here
  • 你需要 "post_status" => 'private',
  • 感谢您的建议,我确实尝试过但无济于事,似乎它可能被之前的声明覆盖了?见link
  • 是的,我更新了代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2017-02-10
  • 1970-01-01
  • 2019-04-07
  • 2016-09-25
  • 1970-01-01
  • 2018-01-21
相关资源
最近更新 更多