继续分享wordpress外贸建站教程。近期有一个wordpress外贸建站目录需要自定义分类目录模板,一般情况下这种需求可以使用自定义分类代码,ACF插件,或者是Elementor主题生成器来实现。但是这个项目为了样式统一,我准备都使用Blocksy主题自带的短代码功能,不过需要扩展Blocksy主题短代码功能。
默认情况下Blocksy主题短代码是没办法自动获取当前分类文章的,只能指定分类或标签,所以我们需要通过以下代码给它加一个扩展。
// 创建动态分类短代码 add_shortcode('blocksy_current_category_posts', function($atts) { $current_category_id = ''; if (is_category()) { $current_category_id = get_queried_object_id(); // global $wp_query; // $current_category_id = get_query_var('cat'); } if (empty($current_category_id)) { return '<!-- Currently not on the category page -->'; } $atts = shortcode_atts(array( 'limit' => '6', 'has_pagination' => 'no', 'view' => 'archive', 'orderby' => 'date', 'order' => 'DESC' ), $atts); $shortcode = sprintf( '[blocksy_posts term_ids="%s" limit="%s" has_pagination="%s" view="%s" orderby="%s" order="%s"]', $current_category_id, $atts['limit'], $atts['has_pagination'], $atts['view'], $atts['orderby'], $atts['order'] ); return do_shortcode($shortcode); });
上面的代码大家可以直接复制,也可以按需修改,比如limit后面的数量是默认的显示数量,这个需要和主题的自定义设置配置,要以那边设置多少,这里就填写多少。
更多参数说明
limit
has_pagination
view
orderby
order
[blocksy_current_category_posts has_pagination="yes"]
以悦然wordpress建站的实际项目为例,我需要的是自动读取当前分类文章全部内容,需要翻页,所以可以使用上面的短代码,添加到古腾堡或Elementor编辑器中都可以。更多用法大家也可以自行发挥。
© Copyright 2024. 悦然网络工作室/悦然wordpress建站 专注中小企业wordpress建站 All Rights Reserved.网站地图本站图片来源为Pexels、Pixabay、Freepik、Unsplash等图片库的免费许可,CC0协议;还有部分为自己手绘或AI生成,版权碰瓷请自重!法律服务:law@yueranseo.com 蜀ICP备20016391号-1 川公网安备 51011502000367号