繁体   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