【问题标题】:changing favicon in wordpress theme更改 wordpress 主题中的图标
【发布时间】:2013-08-06 07:42:11
【问题描述】:

我正在为我的博客定制一个文字新闻主题。我正在尝试更改图标,但我不知道该怎么做。我检查了所有设置,没有 favicon 选项。谁能告诉我如何改变它。

【问题讨论】:

    标签: wordpress customization favicon


    【解决方案1】:

    我通常会在我网站的根目录中添加一个网站图标,因为某些浏览器默认会在那里查看。

    function so18074365_favicon() {
      printf( "<link rel=\"shortcut icon\" type=\"image/vnd.microsoft.icon\" href=\"%s/favicon.ico\" />\n", site_url() );
    }
    
    add_action( 'wp_head', 's018074365_favicon' );
    

    将此功能添加到您的functions.php 并将您的favicon.ico 上传到您的wordpress 根文件夹。

    如果你想在你的主题文件夹中添加图标,添加这个:

    function so18074365_favicon() {
      printf( "<link rel=\"shortcut icon\" type=\"image/vnd.microsoft.icon\" href=\"%s/favicon.ico\" />\n", get_stylesheet_directory_uri() );
    }
    
    add_action( 'wp_head', 's018074365_favicon' );
    

    【讨论】:

    • 好的。感谢所有的答案。我更新了 header.php 文件。它工作正常。
    • functions.php,不是header.php
    • 为什么叫so18074365_favicon?为什么要对网站图标进行操作?
    • @TheWarlock 给函数添加唯一前缀是一种常见的做法。我已使用此问题的 ID 作为前缀。 SO = 堆栈溢出,18074365 = 问题的 ID。我使用了一个动作,以便有人可以在他们想要使用另一个图标时以编程方式将其删除(如果您正在分发主题)。
    【解决方案2】:

    Сonsidering Wordpress Codex 你会

    1. 转到您的 WordPress 管理面板。
    2. 单击设计(在 WordPress 2.3.x 及更低版本中称为演示,在 WordPress 2.7+ 中称为外观)。
    3. 点击主题编辑器。
    4. 选择名为 Header 或 header.php 的文件进行编辑。
    5. 搜索以&lt;link rel="shortcut icon" 开头并以/favicon.ico" /&gt; 结尾的代码行。覆盖它(如果存在),或在&lt;head&gt; HTML 标记下方添加以下代码。 &lt;link rel="shortcut icon" href="&lt;?php echo get_stylesheet_directory_uri(); ?&gt;/favicon.ico" /&gt;

    6. 保存更改。

    注意。不要忘记通过任何可接受的方式将 favico 上传到您当前的主题根目录中。

    希望对您有所帮助。干杯!

    【讨论】:

      【解决方案3】:

      参考wordpress codex Create a favicon

      搜索以&lt;link rel="shortcut icon" and ends with /favicon.ico" /&gt; 开头的代码行。覆盖它,如果它存在,或者在&lt;head&gt;&lt;/head&gt;之间的HTML标签下面添加以下代码

      <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/favicon.ico" />
      

      确保您的样式表目录中有 favicon.ico

      【讨论】:

        【解决方案4】:

        按照以下步骤操作:

        您是否想弄清楚如何更改您的网站图标?网站图标是一个非常小的细节。我认为这不会损害您的业务,因为您没有自定义网站图标。但是,当您确实拥有一个时,它就是人们注意到的那些小细节之一,并且可以给他们留下深刻印象,即使它是潜意识的。

        我想向您展示的有用工具位于 Favicon (dot) cc。

        您会注意到您拥有一个 16 x 16 像素尺寸的完整编辑器。当我过去使用 Photoshop 完成这项任务时,试图让图像在如此小的尺寸下看起来正确有点具有挑战性。这个工具使它更容易,因为你可以像这样编辑它……并在下面预览它。他们的图像导入功能使它变得更加容易。这样他们的软件就可以完成所有艰苦的工作,您可以快速决定是否喜欢它。

        不用担心放大的图片看起来失真。它实际上看起来像一个小图标。要下载它,请单击“下载 Favicon”。很简单。

        只需按照相同的步骤...

        首先我要上传图片……

        然后看看它的样子……

        如果我喜欢它,我可以下载它。

        给它几个小时,您的网站图标将在整个互联网上更新。当我第一次这样做时,我希望它会立即更新,而当它没有更新时,我以为我错过了一步。这就是你真正需要做的,只要给它时间,你的新网站图标就会出现。

        如果您按照课程中的步骤进行操作,您将开始为您的网站吸引大量流量,并建立庞大的在线追随者。

        http://www.gowallaby.com/how-to-change-favicon-in-wordpress

        【讨论】:

          【解决方案5】:

          如果您想使用“外观/自定义”中的“自定义”选项更改网站图标,您可以这样做:

          在function.php处添加代码:

          function themeslug_theme_customizer_favicon( $wp_customize ) {
              // Fun code will go here
          
              $wp_customize->add_section( 'themeslug_favicon_section' , array(
                'title'       => __( 'Favicon', 'themeslug' ),
                'priority'    => 30,
                'description' => 'Upload a favicon to your Wordpress',
            ) );
              $wp_customize->add_setting( 'themeslug_favicon' );
          
              $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'themeslug_favicon', array(
                'label'    => __( 'Favicon', 'themeslug' ),
                'section'  => 'themeslug_favicon_section',
                'settings' => 'themeslug_favicon',
            ) ) );
          }
          add_action('customize_register', 'themeslug_theme_customizer_favicon');
          

          并在标签&lt;header&gt;&lt;/header&gt;之间的header.php添加代码:

            <?php if ( get_theme_mod( 'themeslug_logo' ) ) : ?>
                <link rel="shortcut icon" href="<?php echo esc_url( get_theme_mod( 'themeslug_favicon' ) ); ?>" />
            <?php endif; ?>
          

          现在转到外观/自定义,将有一个“Favicon”部分,只需上传您的 file.png 即可享受

          【讨论】:

            【解决方案6】:
            function add_my_favicon() {
                $favicon_path = plugins_url( '/favicon.ico', __FILE__ );    
                echo '<link rel="shortcut icon" href="' . $favicon_path . '" />';
            }
            
            add_action( 'wp_head', 'add_my_favicon' ); //front end
            add_action( 'admin_head', 'add_my_favicon' ); //admin end
            

            【讨论】:

            • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
            • 请将此代码复制到您当前主题的function.php文件中
            猜你喜欢
            • 2018-11-22
            • 1970-01-01
            • 2014-06-27
            • 2021-02-10
            • 1970-01-01
            • 2018-02-06
            • 1970-01-01
            • 1970-01-01
            • 2018-08-31
            相关资源
            最近更新 更多