给你的子比站点前端发文时侧边栏添加一个置顶开关选项
预览图

将以下代码放置带子比主题的func.php里,没有就自行创建。个人测试置顶没问题
/**
* 前台投稿页:在提交按钮上方注入“置顶设置”开关
*/
add_action('wp_footer', 'lcy_newposts_sticky_field', 20);
function lcy_newposts_sticky_field()
{
if (!is_page_template('pages/newposts.php')) {
return;
}
if (!lcy_can_set_sticky_post()) {
return;
}
$edit_id = !empty($_GET['edit']) ? absint($_GET['edit']) : 0;
$checked = $edit_id ? is_sticky($edit_id) : false;
$html = '<div id="lcy-sticky-box" class="zib-widget mb10-sm">';
$html .= '<div class="title-theme mb10">' . esc_html__('置顶设置', 'zib_language') . '</div>';
$html .= '<div class="flex ac jsb padding-h10">';
$html .= '<div class="flex ac"><i class="fa fa-fw fa-thumb-tack mr6" aria-hidden="true"></i>' . esc_html__('将文章置顶', 'zib_language') . '</div>';
$html .= '<label style="margin:0;"><input class="hide" type="checkbox" name="lcy_sticky" value="1" aria-label="' . esc_attr__('将文章置顶', 'zib_language') . '"' . checked($checked, true, false) . '><div class="form-switch flex0"></div></label>';
$html .= '</div>';
$html .= '<div class="muted-3-color em09 mt6">' . esc_html__('开启后,文章会按主题的置顶规则显示在列表顶部。', 'zib_language') . '</div>';
$html .= '<input type="hidden" name="lcy_sticky_present" value="1">';
$html .= wp_nonce_field('lcy_frontend_sticky', 'lcy_sticky_nonce', false, false);
$html .= '</div>';
?>
<script>
(function () {
function lcyInsertStickyBox() {
var submit = document.querySelector('.new-posts-submit') || document.querySelector('[action="posts_save"]');
if (!submit || document.getElementById('lcy-sticky-box')) {
return;
}
var holder = document.createElement('div');
holder.innerHTML = <?php echo wp_json_encode($html, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>;
var box = holder.firstElementChild;
if (!box) {
return;
}
var widget = submit.closest('.zib-widget');
if (widget && widget.parentNode) {
widget.parentNode.insertBefore(box, widget);
return;
}
var wrap = document.querySelector('.newposts-wrap');
var main = wrap ? (wrap.closest('main') || document) : document;
var sidebar = main.querySelector('.sidebar.show-sidebar') || main.querySelector('.sidebar');
if (sidebar) {
sidebar.insertAdjacentHTML('afterbegin', box.outerHTML);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', lcyInsertStickyBox);
} else {
lcyInsertStickyBox();
}
})();
</script>
<?php
}
if (!defined('ABSPATH')) {
exit;
}
/**
* 前端投稿页增加置顶开关(仅管理员)
*/
add_action('wp_footer', 'lcy_zib_sticky_frontend_field', 20);
function lcy_zib_sticky_frontend_field()
{
if (!is_page_template('pages/newposts.php')) {
return;
}
// 只允许管理员看到和设置
if (!is_user_logged_in() || !current_user_can('manage_options')) {
return;
}
$edit_id = 0;
if (isset($_REQUEST['edit']) && is_scalar($_REQUEST['edit'])) {
$edit_id = absint(wp_unslash($_REQUEST['edit']));
}
$checked = $edit_id ? is_sticky($edit_id) : false;
echo '<template id="lcy-zib-sticky-template">';
echo '<div id="lcy-zib-sticky-box" class="zib-widget mb10-sm">';
echo '<div class="title-theme mb10">' . esc_html__('置顶设置', 'zib_language') . '</div>';
echo '<div class="flex ac jsb padding-h10">';
echo '<div class="flex ac">' . esc_html__('将文章置顶', 'zib_language') . '</div>';
echo '<label style="margin:0;"><input class="hide" type="checkbox" name="lcy_zib_sticky" value="1" aria-label="' . esc_attr__('将文章置顶', 'zib_language') . '"' . checked($checked, true, false) . '><div class="form-switch flex0"></div></label>';
echo '</div>';
echo '<div class="muted-3-color em09 mt6">' . esc_html__('开启后,文章会按主题的置顶规则显示在列表顶部。', 'zib_language') . '</div>';
echo '<input type="hidden" name="lcy_zib_sticky_present" value="1">';
echo wp_nonce_field('lcy_zib_sticky', 'lcy_zib_sticky_nonce', false, false);
echo '</div>';
echo '</template>';
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
var template = document.getElementById('lcy-zib-sticky-template');
if (!template || !template.content || document.getElementById('lcy-zib-sticky-box')) {
return;
}
var wrap = document.querySelector('.newposts-wrap');
var form = wrap ? wrap.closest('form') : null;
if (!form) {
return;
}
var box = template.content.firstElementChild.cloneNode(true);
var sidebar = form.querySelector('.sidebar.show-sidebar') || form.querySelector('.sidebar');
if (sidebar) {
sidebar.insertBefore(box, sidebar.firstChild);
return;
}
var submit = form.querySelector('.new-posts-submit') || form.querySelector('[action="posts_save"]');
var submitWidget = submit ? submit.closest('.zib-widget') : null;
if (submitWidget && submitWidget.parentNode) {
submitWidget.parentNode.insertBefore(box, submitWidget);
}
});
</script>
<?php
}
/**
* 前端投稿保存:写入或取消置顶(仅管理员)
*/
add_action('new_add_posts', 'lcy_zib_sticky_save_frontend', 20, 1);
add_action('new_edit_posts', 'lcy_zib_sticky_save_frontend', 20, 1);
function lcy_zib_sticky_save_frontend($post)
{
if (!is_object($post) || empty($post->ID) || 'post' !== $post->post_type) {
return;
}
// 没有这个标记,说明不是本功能的前端表单
if (empty($_POST['lcy_zib_sticky_present'])) {
return;
}
// 只允许管理员设置
if (!is_user_logged_in() || !current_user_can('manage_options')) {
return;
}
$nonce = isset($_POST['lcy_zib_sticky_nonce']) && is_scalar($_POST['lcy_zib_sticky_nonce'])
? sanitize_text_field(wp_unslash($_POST['lcy_zib_sticky_nonce']))
: '';
if (!$nonce || !wp_verify_nonce($nonce, 'lcy_zib_sticky')) {
return;
}
$post_id = absint($post->ID);
if (!empty($_POST['lcy_zib_sticky'])) {
stick_post($post_id);
} else {
unstick_post($post_id);
}
}





没有回复内容