Wordpress教程 2023年04月3日
0 收藏 0 点赞 558 浏览 3748 个字

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++;
    }
?>

 

此代码适用觅站网开发的主题,其他主题可以参考借鉴。二开请联系站长~有偿!

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2048.html

相关推荐
wordpress 在购物车中的特定产品的WooCommerce结帐上显示某些国家/地区
要在 WooCommerce 结账页面上只显示特定的国家/地区,您可以使用以下代码在函数文件(functions.php)中添加相应的过滤器…
日期:2023-05-29 点赞:0 阅读:920
最新一键关闭WordPress后台主题、WordPress插件的自动更新提醒,禁止WordPress更新提醒
PHP版本低于7.2用这个代码: 将以下代码放入您的主题functions.php //修改代码在后台显示更新 add_filter('pr…
日期:2023-05-24 点赞:0 阅读:172
wordpress相关文章功能代码示例
本文来介绍下wordpress的相关文章功能实现代码。 首先还是来说明下这个相关文章是个什么逻辑,文章的相关性都是通过tag标签来关联的,如…
日期:2023-05-23 点赞:0 阅读:935
woocommerce添加结账页面缩略图和货号+购物车页面货号
购物车上添加图片和货号SKU functions.php文件里面添加: /** * Display SKU in cart table wi…
日期:2023-05-18 点赞:0 阅读:1,052
codestar-framework获取后台设置控制指定分类文章和显示数量
可以在主题模板中使用 _izhanke() 函数获取后台设置的 xinwen_num 字段的值,然后将其传递到 query_posts() …
日期:2023-05-10 点赞:0 阅读:999
怎么指定邮箱可注册?
一些做会员网站的站长,肯定很讨厌那种用临时邮箱注册的用户吧,这次分享一段WordPress代码,让用户只能用指定的邮箱注册。 在主题根目录下…
日期:2023-05-09 点赞:0 阅读:694
发表评论
暂无评论

还没有评论呢,快来抢沙发~