Php 使模板中的常用元素可编辑
所以我正在为一个客户开发一个Wordpress主题,我想让他们能够编辑模板代码中出现的一些内容。例如页脚中的单行文字或页眉中的电话号码 我一直在寻找一个插件,它允许我有一个管理“common elements”的部分,或者类似的东西,他们可以编辑简单的文本字段,我可以在模板中粘贴一些PHP代码,这样当他们更新他们的电话号码时,它会更新他们模板中所有地方的号码 在某种意义上,有点像可编辑的“包含” 因此,我很想知道是否有人知道一个插件可以很容易做到这一点,或者是否有人知道如何编写一些小的东西(我在functions.php文件中假设)来实现这一点 谢谢,Php 使模板中的常用元素可编辑,php,wordpress,plugins,include,themes,Php,Wordpress,Plugins,Include,Themes,所以我正在为一个客户开发一个Wordpress主题,我想让他们能够编辑模板代码中出现的一些内容。例如页脚中的单行文字或页眉中的电话号码 我一直在寻找一个插件,它允许我有一个管理“common elements”的部分,或者类似的东西,他们可以编辑简单的文本字段,我可以在模板中粘贴一些PHP代码,这样当他们更新他们的电话号码时,它会更新他们模板中所有地方的号码 在某种意义上,有点像可编辑的“包含” 因此,我很想知道是否有人知道一个插件可以很容易做到这一点,或者是否有人知道如何编写一些小的东西(我在
Alex.为什么不添加主题选项页 这将使用户能够添加/更改电话号码以及页脚文本等 将此添加到themes function.php文件中
require_once ( get_template_directory() . '/theme-options.php' );
创建名为“theme options.php”的新文件,该文件将驻留在theme文件夹中
复制并粘贴下面的代码
在header.php文件的顶部或要显示电话号码的位置,只需打个电话获取主题选项
$themeOptions = get_option( 'my_theme_options' );
从这里,您可以使用
打印出保存的电话号码。。。。在你的页脚里
您可以将
作为额外的奖励:)我提供了一个简单的快捷码,你可以在帖子和页面中使用它来拨打主题电话号码
在帖子或页面中,只需输入短码[tel]即可打印出帖子中的号码。。
如果,当电话号码需要更改时,只需在“主题选项”页面中更改它,它将在站点范围内更改
祝你好运
马蒂
<?php
// CUSTOM THEME OPTIONS PAGE
add_action( 'admin_init', 'my_theme_options_init' );
add_action( 'admin_menu', 'my_theme_options_add_page' );
/**
* Init plugin options to white list our options
*/
function my_theme_options_init()
{
register_setting( 'my_theme_options',
'my_theme_options',
'my_theme_options_validate' );
}
/**
* Load up the menu page
*/
function my_theme_options_add_page()
{
add_theme_page( __( 'Theme Options',
'my_theme' ),
__( 'Theme Options',
'my_theme' ),
'edit_theme_options',
'my_theme_options',
'my_theme_options_do_page'
);
}
/**
* Create the options page
*/
function my_theme_options_do_page()
{
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php echo "<h2>" . get_current_theme() . __( ' Theme Options', 'my_theme' ) . "</h2>"; ?>
<?php
if ( false !== $_REQUEST['settings-updated'] ) :
?>
<div class="updated fade">
<p>
<strong>
<?php _e( 'Theme Options saved', 'my_theme' ); ?>
</strong>
</p>
</div>
<?php
endif;
?>
<form method="post" action="options.php">
<?php
settings_fields( 'my_theme_options' );
$options = get_option( 'my_theme_options' );
?>
<table class="form-table">
<?php //SITE TELEPHONE NUMBER ?>
<tr valign="top">
<th scope="row">
<?php _e( 'Telephone Number', 'my_theme' ); ?>
</th>
<td>
<input id="my_theme_options[sitetel]" class="regular-text" type="text" name="my_theme_options[sitetel]" value="<?php esc_attr_e( $options['sitetel'] ); ?>" />
<label class="description" for="my_theme_options[sitetel]"><?php _e( 'Website Telephone Number', 'my_theme' ); ?></label>
<br />
<small>Use the shortcode <strong>[tel]</strong> to display telephone number in posts</small>
</td>
</tr>
<?php // COPYRIGHT ?>
<tr valign="top"><th scope="row"><?php _e( 'Copyright', 'my_theme' ); ?></th>
<td>
<input id="my_theme_options[copyright]" class="regular-text" type="text" name="my_theme_options[copyright]" value="<?php esc_attr_e( $options['copyright'] ); ?>" />
<label class="description" for="rehab_theme_options[copyright]"><?php _e( 'footer Copyright text', 'my_theme' ); ?></label>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'my_theme' ); ?>" />
</p>
</form>
</div>
<?php
}
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function my_theme_options_validate( $input ) {
global $select_options, $radio_options;
// Say our text option must be safe text with no HTML tags
$input['sitetel'] = wp_filter_post_kses( $input['sitetel'] );
$input['copyright'] = wp_filter_post_kses( $input['copyright'] );
return $input;
}
// SHOW THEME OPTION TELEPHONE NUMBER
function my_theme_show_tel_number()
{
$options = get_option( 'my_theme_options' );
return "<strong style='color:#036;'>".$options['sitetel']."</strong>";
}
// ADD TEL SHORTCODE
function my_theme_register_shortcodes(){
add_shortcode('tel', 'my_theme_show_tel_number');
}
add_action( 'init', 'my_theme_register_shortcodes');
?>
好的,这正是我要找的!我曾想过使用一个“内容块”插件,但这似乎有些过分,但这个解决方案可能是理想的。非常感谢。凉的只需播放theme-options.php文件,将其更改为适合您的需要和主题,最终应该都能解决问题,此外,您还可以在div wrap中添加自己的徽标/html等。。。要在“主题选项”页面上显示。。