codestar-framework框架 taxonomy.theme.php添加
array(
'id' => 'cate_background_img',
'type' => 'media',
'title' => '<h3>分类列表背景图片</h3>',
'desc' => '自定义分类列表背景图~未独立设置则自动调用主题设置-分类设置中默认背景图',
'button_title' => '上传',
'remove_title' => '删除',
'default' => '',
),
codestar-framework框架 options.theme.php添加
/*
* ------------------------------------------------------------------------------
* 产品中心
* ------------------------------------------------------------------------------
*/
CSF::createSection($prefix, array(
'id' => 'home_xw',
'icon' => 'fa fa-cart-plus',
'title' => '产品中心',
'fields' => array(
array(
'id' => 'home_ppgs',
'type' => 'repeater',
'title' => '首页产品中心模块',
'fields' => array(
array(
'id' => 'id',
'type' => 'select',
'title' => '产品分类',
'placeholder' => '选择分类栏目',
'options' => 'categories',
),
array(
'id' => 'num',
'type' => 'text',
'title' => '显示数量',
'default' => '6',
),
),
),
),
));
调用分类背景图
<?php
// 获取主页展示的分类列表
$home_ppgs = (array) _izhanke('home_ppgs');
$counter = 1;
// 循环展示分类列表
foreach ($home_ppgs as $v) {
if (empty($v['id'])) continue;
// 获取分类名称
$name = get_term($v['id'])->name;
// 获取分类背景图片URL
$cate_background_img = get_term_meta($v['id'], 'cate_background_img', true);
$style = (!empty($cate_background_img['url'])) ? 'style="background-image: url(' . $cate_background_img['url'] . ');"' : '';
// 设置第一个元素为默认选中状态
$class = ($counter == 1) ? 'cmsgoto active' : 'cmsgoto';
// 输出分类名称和背景图片
echo '<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="' . $class . '" data-tab="tab' . $counter . '"' . $style . '>' . $name . '</a>';
$counter++;
}
?>
或者
<?php
// 获取主页展示的分类列表
$home_ppgs = (array) _izhanke('home_ppgs');
$counter = 1;
// 循环展示分类列表
foreach ($home_ppgs as $v) {
if (empty($v['id'])) continue;
// 获取分类名称
$name = get_term($v['id'])->name;
// 获取分类背景图片
$cate_background_img = get_term_meta($v['id'], 'cate_background_img', true);
// 设置第一个元素为默认选中状态
$class = ($counter == 1) ? 'cmsgoto active' : 'cmsgoto';
// 输出分类名称和背景图片
?>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="<?php echo $class; ?>" data-tab="tab<?php echo $counter; ?>"
style="background-image: url(<?php echo $cate_background_img['url'] ?? ''; ?>);">
<?php echo $name; ?>
</a>
<?php
$counter++;
}
?>
cpimg
字段中的图片来显示。可以按照以下方式修改代码:
/*
* ------------------------------------------------------------------------------
* 产品中心
* ------------------------------------------------------------------------------
*/
CSF::createSection($prefix, array(
'id' => 'home_xw',
'icon' => 'fa fa-cart-plus',
'title' => '产品中心',
'fields' => array(
array(
'id' => 'home_ppgs',
'type' => 'repeater',
'title' => '首页产品中心模块',
'fields' => array(
array(
'id' => 'cpimg',
'type' => 'upload',
'title' => '图标',
'library' => 'image',
'placeholder' => 'http://',
'button_title' => '上传',
'remove_title' => '删除'
),
array(
'id' => 'id',
'type' => 'select',
'title' => '产品分类',
'placeholder' => '选择分类栏目',
'options' => 'categories',
),
array(
'id' => 'num',
'type' => 'text',
'title' => '显示数量',
'default' => '6',
),
),
),
),
));
您需要在前端页面中调用cpimg
字段中的图片来显示。可以按照以下方式修改代码:
<?php
// 获取主页展示的分类列表
$home_ppgs = (array) _izhanke('home_ppgs');
$counter = 1;
// 循环展示分类列表
foreach ($home_ppgs as $v) {
if (empty($v['id'])) continue;
// 获取分类名称
$name = get_term($v['id'])->name;
// 获取分类背景图片
$cpimg = $v['cpimg'];
// 设置第一个元素为默认选中状态
$class = ($counter == 1) ? 'cmsgoto active' : 'cmsgoto';
// 输出分类名称和背景图片
?>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="<?php echo $class; ?>" data-tab="tab<?php echo $counter; ?>"
style="background-image: url(<?php echo $cpimg; ?>);">
<?php echo $name; ?>
</a>
<?php
$counter++;
}
?>
可以在循环中添加获取文章数量的代码,可以使用WordPress的内置函数count()和get_posts()来实现。下面是修改后的代码:
<?php
// 获取主页展示的分类列表
$home_ppgs = (array) _izhanke('home_ppgs');
$counter = 1;
// 循环展示分类列表
foreach ($home_ppgs as $v) {
if (empty($v['id'])) continue;
// 获取分类名称
$name = get_term($v['id'])->name;
// 获取分类背景图片
$cpimg = $v['cpimg'];
// 设置第一个元素为默认选中状态
$class = ($counter == 1) ? 'cmsgoto active' : 'cmsgoto';
// 获取分类文章总数
$cat_posts_count = count(get_posts(array('category' => $v['id'])));
// 输出分类名称和背景图片
?>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="<?php echo $class; ?>" data-tab="tab<?php echo $counter; ?>"
style="background-image: url(<?php echo $cpimg; ?>);">
<?php echo $name; ?>
<span class="post-count">(<?php echo $cat_posts_count; ?>)</span>
</a>
<?php
$counter++;
}
?>
此代码适用觅站网开发的主题,其他主题可以参考借鉴。二开请联系站长~有偿!
还没有评论呢,快来抢沙发~