简体   繁体   English

如何在店面主题中使用 SVG 作为标志

[英]How to use SVG for logo in storefront theme

I want to use a SVG logo in my woocommerce storefront theme.我想在我的 woocommerce 店面主题中使用 SVG 徽标。 I can't use the Customizer b/c it won't allow SVG uploads.我不能使用定制器 b/c 它不允许上传 SVG。 Is there a way around that restriction?有没有办法绕过这个限制?

If not, how would I edit the hook/action to use the SVG?如果没有,我将如何编辑钩子/动作以使用 SVG?

I see the storefront_site_branding template function in the storefront/inc folder and I tried creating an inc folder in the child theme but apparently the override does not work on files within the inc folder.我在 storefront/inc 文件夹中看到 storefront_site_branding 模板函数,我尝试在子主题中创建一个 inc 文件夹,但显然覆盖对 inc 文件夹中的文件不起作用。

If you are using a child-theme you can add this to your functions.php, if you update you themes functions.php it may override during your next patch/update.如果您使用子主题,您可以将其添加到您的functions.php,如果您更新主题functions.php,它可能会在您的下一个补丁/更新期间覆盖。

I sourced this code from: https://themeisle.com/blog/add-svg-to-wordpress/我从以下来源获取此代码: https : //themeisle.com/blog/add-svg-to-wordpress/

Method 1: Use the SVG Support plugin If you're looking for the fastest way to add SVG to WordPress, this is it.方法 1:使用 SVG 支持插件如果您正在寻找将 SVG 添加到 WordPress 的最快方法,就是这样。 We're going to use the SVG Support plugin, which enables this particular image format and adds support for it to your media library:我们将使用 SVG Support 插件,它启用这种特定的图像格式并将对它的支持添加到您的媒体库中:

The process is simple.这个过程很简单。 You just need to install and activate the plugin as usual, and then you'll be able to add SVGs to your WordPress site.您只需要像往常一样安装和激活插件,然后您就可以将 SVG 添加到您的 WordPress 站点。

WordPress now requires us to have the tag in our SVG files before uploading. WordPress 现在要求我们在上传之前在我们的 SVG 文件中有标签。 Please open your SVG file in any code editor (such as sublime text) and add the following to the very first line of your SVG file and save, so that you don't encounter security errors:请在任何代码编辑器(例如 sublime text)中打开您的 SVG 文件,并将以下内容添加到 SVG 文件的第一行并保存,以免遇到安全错误:

<?xml version="1.0" encoding="utf-8"?>

However, there are two more settings you might want to change depending on your needs.但是,您可能需要根据需要更改另外两个设置。 First off, let's go to the Settings → SVG Support tab:首先,让我们转到设置 → SVG 支持选项卡:

How to add SVG to WordPress using SVG Support plugin如何使用 SVG 支持插件将 SVG 添加到 WordPress

Inside, you'll find two options.在里面,你会发现两个选项。 The first turns on the plugin's Advanced Mode, which lets you target your SVGs with CSS.第一个打开插件的高级模式,它允许您使用 CSS 定位您的 SVG。 If you don't want to animate your SVGs, then you can skip this option.如果您不想为 SVG 设置动画,则可以跳过此选项。

Second, you can also restrict the ability to upload SVGs to administrators only by enabling the Restrict to Administrators?其次,您还可以通过启用限制管理员? feature.特征。 That one's up to you!这取决于你!

Method 2: Modify your site's functions.php file Every WordPress website has its own functions.php file.方法二:修改站点的functions.php文件 每个WordPress网站都有自己的functions.php文件。 This essential component includes important functions, classes, and filters.这个基本组件包括重要的函数、类和过滤器。 It's also your ticket to adding SVG support to WordPress through a few lines of code.这也是您通过几行代码向 WordPress 添加 SVG 支持的门票。

To reach this file, you'll need to access your website via FTP.要访问此文件,您需要通过 FTP 访问您的网站。 If you don't have a client, we recommend using FileZilla.如果您没有客户端,我们建议您使用 FileZilla。 Once you've found your FTP credentials and accessed your site, you'll want to head to your root folder, which is usually either called public_html or named after your site:找到您的 FTP 凭据并访问您的站点后,您将需要前往您的根文件夹,该文件夹通常称为 public_html 或以您的站点命名:

The WordPress root folder. WordPress 根文件夹。

Now, enter the wp-includes folder and look for the functions.php file within.现在,进入 wp-includes 文件夹并在其中查找 functions.php 文件。 It's important to note that this is the parent file, while there are also individual functions.php files for each of your themes:重要的是要注意这是父文件,同时每个主题也有单独的 functions.php 文件:

The wp-includes folder. wp-includes 文件夹。

For this example, we'll add the code to the parent file.对于此示例,我们将代码添加到父文件中。 However, you may find the changes are lost when WordPress is updated, so feel free to alternatively add it to your theme-specific functions.php file depending on the approach you're more comfortable with.但是,您可能会发现更新 WordPress 时更改会丢失,因此可以根据您更习惯的方法随意将其添加到特定于主题的 functions.php 文件中。

(Editor's note: Doing this in your theme's functions file is actually the recommended approach.) (编者注:在主题的函数文件中执行此操作实际上是推荐的方法。)

Access the functions.php file now by right-clicking on it and choosing the View/Edit option.现在通过右键单击它并选择查看/编辑选项来访问 functions.php 文件。 This will open it using your default text editor.这将使用您的默认文本编辑器打开它。 Now, scroll to the bottom and paste this code snippet there:现在,滚动到底部并将此代码片段粘贴到那里:

function add_file_types_to_uploads($file_types){
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_filter('upload_mimes', 'add_file_types_to_uploads');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM