// 获取最新的5个标签
add_shortcode('new_tags','new_tags_function' );
function new_tags_function($atts,$content){
//短链接参数,合并一个短码的属性和默认属性
$recent_atts=shortcode_atts(
array(
'count'=>5
),
$atts
);
extract($recent_atts);
//打开输出缓冲区,所有的输出信息不再直接发送到浏览器
ob_start();
// 获取最新的5个标签
$args = array(
'number' => $recent_atts['count'], // 获取的标签数量
'orderby' => 'id', // 根据标签ID排序
'order' => 'DESC', // 降序排序
'hide_empty' => false, // 不隐藏空标签
);
$tags = get_tags( $args );
?>
<style>
.new_tags a{
display: inline-block;
border: 1px solid #dedede;
font-size: 14px !important;
text-decoration: none;
text-decoration: none !important;
padding: 5px 10px;
margin-right: 10px;
margin-bottom: 15px;
color: #333;
}
.new_tags a:hover{
background: #dedede;
}
</style>
<?php
echo "<div class='new_tags'>";
// 输出最新的5个标签
foreach ( $tags as $tag ) {
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
}
echo "</div>";
$output = ob_get_clean();
return $output;
}
// 获取最热门的5个tags
add_shortcode('hot_tags','hot_tags_function' );
function hot_tags_function($atts,$content){
//短链接参数,合并一个短码的属性和默认属性
$recent_atts=shortcode_atts(
array(
'count'=>5
),
$atts
);
extract($recent_atts);
//打开输出缓冲区,所有的输出信息不再直接发送到浏览器
ob_start();
// 获取最热门的5个tags
$args = array(
'number' => $recent_atts['count'], // 获取的标签数量
'orderby' => 'count', // 按照名称排序
'order' => 'DESC', // 升序排序
'echo' => false // 不直接输出,而是返回结果
);
$html = wp_tag_cloud( $args );
// 输出HTML
?>
<style>
.hot_tags a{
display: inline-block;
border: 1px solid #dedede;
font-size: 14px !important;
text-decoration: none;
text-decoration: none !important;
padding: 5px 10px;
margin-right: 10px;
margin-bottom: 15px;
color: #333;
}
.hot_tags a:hover{
background: #dedede;
}
</style>
<?php
echo "<div class='hot_tags'>";
echo $html;
echo "</div>";
$output = ob_get_clean();
return $output;
}
// 获取所有的tags
add_shortcode('all_tags','all_tags_function' );
function all_tags_function($atts,$content){
//短链接参数,合并一个短码的属性和默认属性
$recent_atts=shortcode_atts(
array(),
$atts
);
extract($recent_atts);
//打开输出缓冲区,所有的输出信息不再直接发送到浏览器
ob_start();
// 获取最新的5个tags
$args = array(
'orderby' => 'term_id', // 按照名称排序
'order' => 'DESC', // 升序排序
'echo' => false // 不直接输出,而是返回结果
);
$html = wp_tag_cloud( $args );
// 输出HTML
?>
<style>
.all_tags a{
display: inline-block;
border: 1px solid #dedede;
font-size: 14px !important;
text-decoration: none;
text-decoration: none !important;
padding: 5px 10px;
margin-right: 10px;
margin-bottom: 15px;
color: #333;
}
.all_tags a:hover{
background: #dedede;
}
</style>
<?php
echo "<div class='all_tags'>";
echo $html;
echo "</div>";
$output = ob_get_clean();
return $output;
}
//5.15修改
//新闻资讯指定分类
function custom_category_posts($query) {
//判断是否在博客页面,并且是主查询
if ( !is_admin() && $query->is_home() ) {
// 在这里设置你想要显示的分类ID或分类别名
$category_ids = array(13,25,24,19,14,12); // 替换为你的分类ID数组
// 设置查询参数以仅包括指定分类的文章
$query->set('category__in', $category_ids);
}
if ( !is_admin() && $query->is_main_query() && !is_page() ) {
$current_language = get_locale();
// 假设我们要过滤的自定义字段键为 'my_custom_field_key'
$meta_key = 'language';
// 获取查询参数中的自定义字段值
$meta_value = $current_language;
// 如果存在自定义字段值,我们可以进行过滤
if (!empty($meta_value)) {
$query->set('meta_key', $meta_key);
$query->set('meta_value', $meta_value);
}
}
return $query;
}
add_action('pre_get_posts', 'custom_category_posts');
//搜索限制
function search_in_specific_category($query) {
if ($query->is_search() && !$query->is_admin()) {
// 使用多个分类ID来限制搜索结果
$category_ids = array(13,25,24,19,20,14,12); // 替换为你想要搜索的分类ID数组
$query->set('category__in', $category_ids);
}
return $query;
}
add_filter('pre_get_posts', 'search_in_specific_category');
//侧边栏最近文章过滤
add_filter('widget_posts_args', 'filter_recent_posts_widget_parameters', 10, 2);
function filter_recent_posts_widget_parameters($args, $instance) {
$current_language = get_locale();
$args['meta_query'] = array(
array(
'key' => 'language', // 你想查询的 meta 键
'value' => $current_language, // 你想匹配的 meta 值
'compare' => '=' // 匹配条件,这里是等于
)
);
return $args;
}
function custom_category_list_shortcode( $atts ) {
$atts = shortcode_atts( array(
'orderby' => 'name',
'include' => '13,25,24,19,14,12,20', // 删除了多余的空格
'show_count' => true, // 显示文章数量
'title_li' => '' // 删除默认标题
), $atts );
ob_start(); ?>
<ul>
<?php wp_list_categories( $atts ); ?>
</ul>
<?php
return ob_get_clean();
}
add_shortcode( 'custom_category_list', 'custom_category_list_shortcode' );
function custom_timeline_shortcode($atts) {
$atts = shortcode_atts(array(
'categories' => 'app_dev,ylly,szfzx,szyx,wlwkf,nydl', // 包含多个分类别名的字符串,用逗号分隔
'posts_per_page' => 10, // 每页显示的文章数量
'order' => 'desc', // 默认升序排列
), $atts);
// 将字符串转换为逗号分隔的列表,适用于 category_name 参数
$category_names = !empty($atts['categories']) ? explode(',', $atts['categories']) : array();
// 查询参数
$query_args = array(
'posts_per_page' => intval($atts['posts_per_page']), // 每页显示的文章数量
'category_name' => implode(',', $category_names), // 将数组转换为逗号分隔的字符串
'order' => $atts['order'],
'paged' => get_query_var('paged') ? get_query_var('paged') : 1, // 当前页数
);
// 查询文章
$query = new WP_Query($query_args);
// 开始输出文章列表
ob_start();
if ($query->have_posts()) {
echo '<ul class="custom-category-list">';
while ($query->have_posts()) {
$query->the_post();
// 输出每篇文章的标题和发布时间
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> - ' . get_the_date() . '</li>';
}
echo '</ul>';
// 分页导航
$big = 999999999; // 需要一个不太可能的整数
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $query->max_num_pages,
'mid_size' => 4, // 两边显示的页数
'end_size' => 2, // 开始和结尾显示的页数
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
));
} else {
echo '<p>No posts found.</p>';
}
wp_reset_postdata();
// 返回生成的内容
return ob_get_clean();
}
add_shortcode('custom_category_timeline', 'custom_timeline_shortcode');
function custom_categories_total_post_count_shortcode($atts) {
// 设置简码的默认属性
$atts = shortcode_atts(
array(
'categories' => 'app_dev,ylly,szfzx,szyx,wlwkf,nydl', // 默认为空字符串
),
$atts,
'total_posts_count' // 简码名称
);
// 从属性中提取分类列表
$categories = $atts['categories'];
$categories_array = explode(',', $categories); // 将字符串转换为数组
$total_count = 0; // 初始化文章总数
// 循环处理每个分类
foreach ($categories_array as $category_slug) {
$term = get_term_by('slug', trim($category_slug), 'category'); // 使用trim去除可能的空格
if ($term) {
$total_count += $term->count; // 累加每个分类的文章数量
}
}
// 格式化返回的字符串,确保没有在 return 前多余的空格
return "非常好,新闻资讯目前共计 {$total_count} 篇文章,请继续努力!";
}
add_shortcode('total_posts_count', 'custom_categories_total_post_count_shortcode');
//上下页导航需要显示同一种语言
function previous_posts_from_same_category( $args ) {
$args['in_same_term'] = true;
$meta_key = 'language';
if (!empty($meta_value)) {
if (!isset($args['meta_query'])) {
$args['meta_query'] = array();
}
$args['meta_query'][] = array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => '='
);
}
$current_language = get_locale();
return $args;
}
add_filter( 'astra_single_post_navigation', 'previous_posts_from_same_category' );
//5.20 post组件根据语言显示调整
/**
* 根据自定义字段 'language' 过滤 Astra Posts Widget 的查询参数
*
* @param array $query_args WP_Query 参数数组。
* @param array $settings Astra Posts Widget 的设置数组。
* @return array 修改后的 WP_Query 参数数组。
*/
add_filter('uael_posts_query_args', 'custom_filter_posts_by_language', 10, 2);
function custom_filter_posts_by_language($query_args, $settings) {
// 获取当前语言
$current_language = get_locale();
// 定义自定义字段键
$meta_key = 'language';
// 设置自定义字段值为当前语言
$meta_value = $current_language;
// 添加 meta_query 以根据 'language' 自定义字段进行过滤
if (!empty($meta_value)) {
if (!isset($query_args['meta_query'])) {
$query_args['meta_query'] = array();
}
$query_args['meta_query'][] = array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => '='
);
}
return $query_args;
}
//当前文章翻译无对应对立语言返回到首页
add_action('template_redirect', 'redirect_to_home_on_404_with_translatepress');
function redirect_to_home_on_404_with_translatepress() {
if (is_404()) {
// 获取当前语言
$current_language = function_exists('pll_current_language') ? pll_current_language() : get_locale();
// 可以根据需要对不同语言进行不同的处理
if ($current_language) {
// 重定向到首页
wp_redirect(home_url());
exit;
}
}
}
代码位置:
关于多语言使用的插件是translatepress插件。