![](/img/trans.png)
[英]Wanna customize Wordpress theme by adding an image section in header but error: syntax error, unexpected 'action' (T_STRING)
[英]Why some of Wordpress theme doesn't have Header Image Section
這取決於WordPress主題開發人員如何設計和開發主題,但是您可以通過在定制工具區域中添加您自己的選項(例如(圖像,文本區域,輸入)字段)來實現。
這是在function.php文件中添加此代碼的示例
// Header Logo
function add_logo_customizer($wp_customize) {
$wp_customize->add_section(
'logo_image',
array(
'title' => 'Logo Image',
'priority' => 45,
)
);
$wp_customize->add_setting( 'Logo-upload' );
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'lp-image_selector',
array(
'label' => 'Logo Image',
'section' => 'logo_image',
'settings' => 'Logo-upload'
)
)
);
}
add_action('customize_register', 'add_logo_customizer');
您可以將其稱為<?php echo get_theme_mod( 'Logo-upload' ); ?>
<?php echo get_theme_mod( 'Logo-upload' ); ?>
上面的代碼已經過測試,可以正常工作
首先,您需要為自定義標題添加支持。
參見編解碼器: https : //developer.wordpress.org/themes/functionality/custom-headers/
顯然,您需要使用該代碼創建一個php文件,並將其包含在functions.php中,或將代碼直接添加到您的functions.php文件中。
function themename_custom_header_setup() {
$defaults = array(
// Default Header Image to display
'default-image' => get_template_directory_uri() . '/images/headers/default.jpg',
// Display the header text along with the image
'header-text' => false,
// Header text color default
'default-text-color' => '000',
// Header image width (in pixels)
'width' => 1000,
// Header image height (in pixels)
'height' => 198,
// Header image random rotation default
'random-default' => false,
// Enable upload of image file in admin
'uploads' => false,
// function to be called in theme head section
'wp-head-callback' => 'wphead_cb',
// function to be called in preview page head section
'admin-head-callback' => 'adminhead_cb',
// function to produce preview markup in the admin screen
'admin-preview-callback' => 'adminpreview_cb',
);
}
add_action( 'after_setup_theme', 'themename_custom_header_setup' );
然后在您的header.php上獲取圖像並將其放在您想要的位置。
<?php if ( get_header_image() ) : ?>
<div id="site-header">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
<img src="<?php header_image(); ?>" width="<?php echo absint( get_custom_header()->width ); ?>" height="<?php echo absint( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
</a>
</div>
* functions.php位於主題文件夾的根目錄下,如果主題使用“子主題”,請使用此文件夾進行更改。
回答您的問題,當您上傳圖像時,可以裁剪圖像並調整其大小。 WordPress無法以編輯器的形式編輯照片,只能調整大小和裁剪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.