0
来自 Discoze 社区
当前还没有回复,欢迎成为第一个参与讨论的人。
B2主题可以给普通文章设置自定义字段,并且支持非常自由的筛选功能。对于一些产品类的网站就很有用。但主题并不没有实现在前台文章页面显示这些自定义字段。研究了一下用以下代码即可实现。可以根据自己需要修改样式和放置的位置。
注意:自定义字段不要用多选,目前有bug。
将以下代码,放置在/TempParts/Single/content.php文件中,默认文章样式下就会显示了。其他样式修改/TempParts/Single/对应的文件。
text<?php $write_custom_arg = b2_get_option('normal_write','write_custom_group'); $customs = get_post_meta($post_id,'b2_custom_key',true); if($customs!=''){ // 构建 edit_customs $edit_customs = array(); foreach($customs as $custom){ $edit_customs[] = array( 'key'=>(string)$custom, 'value'=>get_post_meta($post_id,$custom,true) ); } // 显示字段 foreach($edit_customs as $item){ foreach($write_custom_arg as $config){ if($config['key'] == $item['key']){ // 使用正则表达式解析选项字符串 $options_str = $config['value']; preg_match_all('/(\w+)=([^=]+?)(?=\s+\w+=|$)/', $options_str.' ', $matches); $options = array_combine($matches[1], $matches[2]); if(isset($options[$item['value']])){ echo '<div class="custom-field">'; echo '<strong>' . esc_html($config['name']) . ':</strong> ' . esc_html(trim($options[$item['value']])); echo '</div>'; } break; } } } } ?>