当前位置:首页技术教程WordPress文章与页面互换
🎉 恭喜你发现了宝藏!

WordPress文章与页面互换

有些时候可能准备将一个或多个页面转换为文章,可以参考下面的方法。将代码添加到当前主题函数模板functions.php中:单个页面转换为文章// 页面的ID
$page_id = 123;

// 设置页面为文章
$post = array(
‘ID’ => $page_id,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’
);

// 使用wp_update_post函数更新页面
$updated_post_id = wp_update_post( $post );

// 检查是否成功转换
if( !is_wp_error($updated_post_id) ) {
echo “页面已转换为文章,新文章ID:” . $updated_post_id;
} else {
echo “转换失败:” . $updated_post_id->get_error_message();
}多个页面转换为文章// 指定的页面ID列表
$page_ids_to_convert = array(123, 456, 789); // 替换为你想要转换的页面ID

// 遍历每个页面ID
foreach ($page_ids_to_convert as $page_id) {
// 设置页面为文章
$post = array(
‘ID’ => $page_id,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’ // 或者你想要的其他状态
);

// 使用wp_update_post函数更新页面
$updated_post_id = wp_update_post($post);

// 检查是否成功转换
if (!is_wp_error($updated_post_id)) {
echo “页面ID ” . $page_id . ” 已成功转换为文章ID ” . $updated_post_id . “”;
} else {
echo “页面ID ” . $page_id . ” 转换失败: ” . $updated_post_id->get_error_message() . “”;
}
}所有页面批量转换为文章// 获取所有页面的ID列表
$args = array(
‘post_type’ => ‘page’,
‘posts_per_page’ => -1, // 检索所有页面
‘post_status’ => ‘any’ // 检索所有状态的页面
);

$pages = new WP_Query($args);

// 遍历每个页面并转换为文章
while ($pages->have_posts()) : $pages->the_post();
$page_id = get_the_ID();

// 设置页面为文章
$post = array(
‘ID’ => $page_id,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’ // 或者你想要的其他状态
);

// 使用wp_update_post函数更新页面
$updated_post_id = wp_update_post($post);

// 检查是否成功转换
if (!is_wp_error($updated_post_id)) {
echo “页面ID ” . $page_id . ” 已转换为文章ID ” . $updated_post_id . “”;
} else {
echo “页面ID ” . $page_id . ” 转换失败: ” . $updated_post_id->get_error_message() . “”;
}

endwhile;

// 重置查询
wp_reset_query();同理,将文章转换为页面// 文章的ID
$post_id = 456;

// 设置文章为页面
$post = array(
‘ID’ => $post_id,
‘post_type’ => ‘page’,
‘post_status’ => ‘publish’ // 或者你想要的其他状态
);

// 使用wp_update_post函数更新文章
$updated_post_id = wp_update_post($post);

// 检查是否成功转换
if (!is_wp_error($updated_post_id)) {
echo “文章已转换为页面,新页面ID:” . $updated_post_id;
} else {
echo “转换失败:” . $updated_post_id->get_error_message();
}如果嫌使用代码麻烦,可以考虑使用WordPress文章类型转换插件:Post Type SwitcherPluginsWordPress文章类型转换插件:Post Type SwitcherWordPress本身不能直接将页面转换到正常的文章,除了复制内容重新发表外,别无它法,而通过Post Type Switc…668该插件不仅能将页面和文章相互转换,还能转换自定义文章类型。

提示: 本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:522552094@qq.com

给TA打赏
共{{data.count}}人
人已打赏
技术教程

WordPress用户切换插件:user-switchin

2024-6-18 22:16:27

技术教程

图片为王,标题为皇

2024-6-18 22:16:34

!
你也想出现在这里?立即 联系我们吧!
终生年SVIP赞助仅需188
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
  • 5477 用户总数
  • 37042 文章总数
  • 154536717 浏览总数
  • 0 今日发布
  • 2704 稳定运行
立即加入

加入本站VIP,全站资源任意下载!