簡體   English   中英

如何讓用戶僅從主題編輯器更改我的 wp 主題上的任何圖像?

[英]How can I make the user change any image on my wp theme just from the theme editor?

如何讓用戶僅從主題編輯器更改我的 wp 主題上的任何圖像?

例如:我的主題頁腳上有一個我硬編碼的背景圖像,我想讓用戶(管理員)能夠從主題編輯器中更改它,感謝高級

我不想使用這樣的東西:

<div class="footer-background background-image" style="background-image: url(<?php echo get_theme_file_uri( 'assets/images/bg.jpg' ) ?>)" >

如果你能給我一個關於這個的 wp 法典,那將會很有幫助,因為我在谷歌上找不到任何與我的問題相關的東西。

如果我是你,我會做以下事情

  1. 安裝 ACF 插件
  2. 創建選項頁面
  3. 創建新的自定義字段(圖像字段)並將其分配給先前創建的選項頁面。
  4. 更新頁腳模板以顯示來自后端的圖像,如下所示
  5. 更新頁腳圖像 src 以獲取后端 ACF 字段值。

https://www.advancedcustomfields.com/resources/options-page/ https://www.advancedcustomfields.com/resources/image/

所以,你可以做這樣的事情(在functions.php中):

add_action( 'customize_register', function( $wp_customize ) {
    $wp_customize->add_section(
        'section_img',
        array(
            'title' => 'Images',
            'priority' => 35,
        )
    );
    $wp_customize->add_setting(
        'footer_image'
    );
    $wp_customize->add_control(
        new WP_Customize_Image_Control(
            $wp_customize,
            'footer_image',
            array(
                'label' => 'Footer image',
                'section' => 'section_img',
                'settings' => 'footer_image'
            )
        )
    );
});

您可以通過以下方式獲得價值(在 footer.php 中):

get_theme_mod('footer_image');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM