feat(pages): 1:1 文章导航 posts-navs (zibll postsnavs)
Per-category full post lists, affix TOC shell, admin drag-sort settings modal + AJAX save (navs_show_cat_id / name / desc).
This commit is contained in:
1 parent
c27a36faff
commit
1cb534e4fe
6 files changed
+835
-43
No files matched your search
@@ -1,65 +1,228 @@
|
||||
<?php
|
||||
/**
|
||||
* Template Name: AKSRT - Posts Navigation
|
||||
* Template Name: AKSRT - 文章导航
|
||||
* Template Post Type: page
|
||||
*
|
||||
* 文章导航页:分类+标签的导航聚合页。
|
||||
* Zibll pages/postsnavs.php 1:1 — selected categories with full post lists,
|
||||
* affix TOC sidebar, admin drag-sort settings modal.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
get_header();
|
||||
$post = get_queried_object();
|
||||
$post_id = $post instanceof WP_Post ? (int) $post->ID : (int) get_queried_object_id();
|
||||
|
||||
$categories = get_categories(['hide_empty' => true, 'orderby' => 'count', 'order' => 'DESC', 'number' => 20]);
|
||||
$tags = get_tags(['hide_empty' => true, 'orderby' => 'count', 'order' => 'DESC', 'number' => 40]);
|
||||
|
||||
if (function_exists('aksrt_archive_open_shell')) {
|
||||
aksrt_archive_open_shell([
|
||||
'main_class' => 'site-main container content-shell content-shell--page page-content-default',
|
||||
'has_sidebar' => false,
|
||||
'page_kind' => 'posts-navs',
|
||||
]);
|
||||
$show_cat_id = get_post_meta($post_id, 'navs_show_cat_id', true);
|
||||
if (!is_array($show_cat_id)) {
|
||||
$show_cat_id = $show_cat_id !== '' && $show_cat_id !== null
|
||||
? array_values(array_filter(array_map('intval', (array) $show_cat_id)))
|
||||
: [];
|
||||
} else {
|
||||
$show_cat_id = array_values(array_filter(array_map('intval', $show_cat_id)));
|
||||
}
|
||||
|
||||
$page_desc = (string) get_post_meta($post_id, 'navs_page_desc', true);
|
||||
$page_name = (string) get_post_meta($post_id, 'navs_page_name', true);
|
||||
$title = get_the_title($post_id);
|
||||
$page_name = $page_name !== '' ? $page_name : $title;
|
||||
|
||||
$all_cats = get_categories([
|
||||
'orderby' => 'term_group',
|
||||
'order' => 'ASC',
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
|
||||
$can_admin = is_user_logged_in() && current_user_can('manage_options');
|
||||
|
||||
get_header();
|
||||
?>
|
||||
<div class="content-main content-main--page content-main--posts-navs">
|
||||
<?php
|
||||
get_template_part('templates/parts/page-header', null, [
|
||||
'title' => esc_html(get_the_title() ?: __('文章导航', 'aksrt')),
|
||||
<style>
|
||||
.page-share a { margin-right: 10px; }
|
||||
.page-share a > text { display: none; }
|
||||
</style>
|
||||
|
||||
<div class="container">
|
||||
<div class="main-bg theme-box radius8 main-shadow box-body">
|
||||
<div class="box-body">
|
||||
<h2 class="theme-box">
|
||||
<?php echo esc_html($page_name); ?>
|
||||
<?php if ($can_admin) : ?>
|
||||
<small>
|
||||
<a style="cursor:pointer" href="javascript:;" data-toggle="modal" data-target="#post-navs-sz">
|
||||
[<?php esc_html_e('设置', 'aksrt'); ?>]
|
||||
</a>
|
||||
</small>
|
||||
<?php endif; ?>
|
||||
</h2>
|
||||
<div class="flex ab jsb hh">
|
||||
<div class="muted-color mr20">
|
||||
<?php echo esc_html($page_desc !== '' ? $page_desc : __('暂无简介内容,请设置', 'aksrt')); ?>
|
||||
</div>
|
||||
<div class="pull-right page-share mt20">
|
||||
<?php
|
||||
if (function_exists('aksrt_render_share_buttons')) {
|
||||
aksrt_render_share_buttons(['compact' => true]);
|
||||
} elseif (function_exists('aksrt_get_share_html')) {
|
||||
echo aksrt_get_share_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<main class="container page-template-posts-navs">
|
||||
<div class="content-wrap">
|
||||
<div class="content-layout">
|
||||
<div class="posts-navs" data-nav="posts">
|
||||
<?php
|
||||
if (!empty($show_cat_id)) {
|
||||
foreach ($show_cat_id as $c_id) {
|
||||
$the_cat = get_category((int) $c_id);
|
||||
if (!$the_cat || is_wp_error($the_cat)) {
|
||||
continue;
|
||||
}
|
||||
$posts = get_posts([
|
||||
'category' => (int) $c_id,
|
||||
'numberposts' => -1,
|
||||
'post_status' => 'publish',
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'suppress_filters' => false,
|
||||
]);
|
||||
?>
|
||||
if (empty($posts)) {
|
||||
continue;
|
||||
}
|
||||
$section_id = 'navs-cat-' . (int) $c_id;
|
||||
echo '<div class="theme-box">';
|
||||
echo '<div class="box-body notop"><div class="title-theme"><h2 id="' . esc_attr($section_id) . '">' . esc_html($the_cat->name) . '</h2></div></div>';
|
||||
echo '<div class="main-bg radius8 main-shadow box-body"><ul class="posts-navs__list">';
|
||||
foreach ($posts as $p) {
|
||||
$time_tip = get_the_time('Y年m月d日 H:i', $p);
|
||||
$time_ago = human_time_diff(get_the_time('U', $p), current_time('timestamp')) . __('前', 'aksrt');
|
||||
$views = function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($p->ID) : 0;
|
||||
$likes = (int) get_post_meta($p->ID, 'like', true);
|
||||
if ($likes <= 0) {
|
||||
$likes = (int) get_post_meta($p->ID, 'aksrt_like', true);
|
||||
}
|
||||
$post_tip = sprintf(
|
||||
/* translators: 1: views 2: likes */
|
||||
__('阅读(%1$s) 点赞(%2$s)', 'aksrt'),
|
||||
(string) $views,
|
||||
(string) $likes
|
||||
);
|
||||
$subtitle = (string) get_post_meta($p->ID, 'subtitle', true);
|
||||
if ($subtitle === '') {
|
||||
$subtitle = (string) get_post_meta($p->ID, 'aksrt_subtitle', true);
|
||||
}
|
||||
echo '<li class="posts-navs__item">';
|
||||
echo '<div class="text-ellipsis">';
|
||||
echo '<span title="' . esc_attr($time_tip) . '" class="muted-2-color">' . esc_html($time_ago) . '</span>';
|
||||
echo '<span class="icon-circle"></span>';
|
||||
echo '<a title="' . esc_attr($post_tip) . '" href="' . esc_url(get_permalink($p->ID)) . '">';
|
||||
echo esc_html(get_the_title($p->ID));
|
||||
if ($subtitle !== '') {
|
||||
echo ' <span class="muted-2-color">' . esc_html($subtitle) . '</span>';
|
||||
}
|
||||
echo '</a></div></li>';
|
||||
}
|
||||
echo '</ul></div></div>';
|
||||
}
|
||||
} elseif ($can_admin) {
|
||||
echo '<div class="theme-box main-bg radius8 main-shadow box-body">';
|
||||
echo '<p class="muted-2-color">' . esc_html__('尚未选择分类,请点击标题旁「设置」配置要展示的分类。', 'aksrt') . '</p>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<div class="theme-box main-bg radius8 main-shadow box-body">';
|
||||
echo '<p class="muted-2-color">' . esc_html__('暂无导航内容', 'aksrt') . '</p>';
|
||||
echo '</div>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<div class="posts-nav-box" data-affix="true" data-title="<?php esc_attr_e('导航目录', 'aksrt'); ?>"></div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<div class="aksrt-posts-navs">
|
||||
<?php if (!empty($categories)) : ?>
|
||||
<section class="aksrt-posts-navs__section">
|
||||
<h2 class="aksrt-posts-navs__heading"><?php esc_html_e('热门分类', 'aksrt'); ?></h2>
|
||||
<div class="aksrt-posts-navs__cats">
|
||||
<?php foreach ($categories as $cat) : ?>
|
||||
<a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>" class="aksrt-posts-navs__cat-chip">
|
||||
<span class="aksrt-posts-navs__cat-name"><?php echo esc_html($cat->name); ?></span>
|
||||
<span class="aksrt-posts-navs__cat-count"><?php echo esc_html((string) $cat->count); ?></span>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
<?php if ($can_admin) : ?>
|
||||
<div class="modal fade" id="post-navs-sz" tabindex="-1" role="dialog" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<form id="aksrt-posts-navs-settings" class="aksrt-posts-navs-settings">
|
||||
<div class="modal-header">
|
||||
<p class="modal-title">
|
||||
<?php esc_html_e('当前页面设置', 'aksrt'); ?>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="<?php esc_attr_e('关闭', 'aksrt'); ?>">
|
||||
<i class="fa fa-times" aria-hidden="true"></i>
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label for="navs_page_name"><?php esc_html_e('标题', 'aksrt'); ?></label>
|
||||
<input type="text" class="form-control" id="navs_page_name" name="navs_page_name" placeholder="<?php esc_attr_e('自定义标题', 'aksrt'); ?>" value="<?php echo esc_attr($page_name); ?>">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="navs_page_desc"><?php esc_html_e('简介', 'aksrt'); ?></label>
|
||||
<input type="text" class="form-control" id="navs_page_desc" name="navs_page_desc" placeholder="<?php esc_attr_e('自定义简介', 'aksrt'); ?>" value="<?php echo esc_attr($page_desc); ?>">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label><?php esc_html_e('请将需要显示的分类拖动到选区并排序', 'aksrt'); ?></label>
|
||||
<div class="row text-center">
|
||||
<div class="col-xs-6 col-sm-6">
|
||||
<p class="text-muted"><?php esc_html_e('未选择的分类', 'aksrt'); ?></p>
|
||||
<div class="cat-group mini-scrollbar" id="cat-sortable">
|
||||
<?php
|
||||
foreach ($all_cats as $the_cat) {
|
||||
$c_id = (int) $the_cat->term_id;
|
||||
if (in_array($c_id, $show_cat_id, true)) {
|
||||
continue;
|
||||
}
|
||||
$c_name = rtrim((string) get_category_parents($c_id, false, '>'), '>');
|
||||
echo '<span class="sortable-cat-item" draggable="true" data-id="' . esc_attr((string) $c_id) . '">'
|
||||
. esc_html($c_name)
|
||||
. '<span class="badge">' . (int) $the_cat->count . '</span></span>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</section>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($tags)) : ?>
|
||||
<section class="aksrt-posts-navs__section">
|
||||
<h2 class="aksrt-posts-navs__heading"><?php esc_html_e('热门标签', 'aksrt'); ?></h2>
|
||||
<div class="aksrt-posts-navs__tags">
|
||||
<?php foreach ($tags as $tag) : ?>
|
||||
<a href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>" class="aksrt-posts-navs__tag"><?php echo esc_html($tag->name); ?></a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<div class="col-xs-6 col-sm-6">
|
||||
<p class="text-muted"><?php esc_html_e('已选择的分类', 'aksrt'); ?></p>
|
||||
<div class="cat-group mini-scrollbar" id="cat-sortable-ok">
|
||||
<?php
|
||||
foreach ($show_cat_id as $c_id) {
|
||||
$the_cat = get_category((int) $c_id);
|
||||
if (!$the_cat || is_wp_error($the_cat)) {
|
||||
continue;
|
||||
}
|
||||
$c_name = rtrim((string) get_category_parents((int) $c_id, false, '>'), '>');
|
||||
echo '<span class="sortable-cat-item" draggable="true" data-id="' . esc_attr((string) $c_id) . '">'
|
||||
. esc_html($c_name)
|
||||
. '<span class="badge">' . (int) $the_cat->count . '</span></span>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</section>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<input type="hidden" name="action" value="aksrt_post_navs_settings">
|
||||
<input type="hidden" name="page_id" value="<?php echo (int) $post_id; ?>">
|
||||
<?php wp_nonce_field('aksrt_post_navs_settings', '_wpnonce', false); ?>
|
||||
<button type="submit" class="btn btn-primary" evt="action"><?php esc_html_e('确认修改', 'aksrt'); ?></button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="navs-bj"></div>
|
||||
<?php
|
||||
if (function_exists('aksrt_archive_close_shell')) {
|
||||
aksrt_archive_close_shell(['has_sidebar' => false]);
|
||||
}
|
||||
endif;
|
||||
|
||||
get_footer();
|
||||
Reference in new issue
Block a user