简体   繁体   English

将图像添加到 Wordpress 标题

[英]Add image to Wordpress Header

I'm using Wordpress to set up a content managed website and I'm fairly competent with HTML and CSS.我正在使用 Wordpress 来建立一个内容管理的网站,而且我对 HTML 和 CSS 相当能干。 I can change the background image of the site in the CSS and can change the header image (background).我可以在 CSS 中更改站点的背景图像,并且可以更改标题图像(背景)。

What I want to do is add an image within the header which I can position absolutely.我想要做的是在标题中添加一个我可以绝对定位的图像。 However, when I try to add an image with the tag into the header.php, it shows the 'broken image' icon, even though I am typing the code correctly.但是,当我尝试将带有标记的图像添加到 header.php 中时,它会显示“损坏的图像”图标,即使我正确键入了代码。

If I add a div, I can set a background image, but it resizes it all oddly and doesn't look right.如果我添加一个 div,我可以设置一个背景图像,但它会奇怪地调整它的大小并且看起来不正确。

I'm using the Wordpress TwentyEleven or TwentyTen theme and just cannot figure this out.我正在使用 Wordpress TwoEleven 或 TwoTen 主题,但无法弄清楚这一点。 I presume it's some limitation of Wordpress.我认为这是 Wordpress 的一些限制。 Any help would be appreciated.任何帮助,将不胜感激。

The fact that it does not work directly in header.php and works as a background for a div means (although you are not showing your code, and you should) that you are probably using relative links.它不能直接在 header.php 中工作并且用作 div 的背景这一事实意味着(尽管您没有显示代码,但您应该)您可能正在使用相对链接。 The problem is that the inclusion of template parts in WordPress makes the use of relative links more difficult.问题是在 WordPress 中包含模板部分使得使用相对链接更加困难。 However, if you use a div, you shouldn't have any problem and the background image shouldn't resize (if the CSS rule is correctly defined).但是,如果您使用 div,则不会有任何问题,并且背景图像不应调整大小(如果正确定义了 CSS 规则)。 But solving a path problem by inserting a div is bad practice.但是通过插入 div 来解决路径问题是不好的做法。 Just use absolute path to your image (or at least relative to the website root, like /wp-content/themes/yourtheme/yourimage.png ).只需使用图像的绝对路径(或至少相对于网站根目录,如/wp-content/themes/yourtheme/yourimage.png )。
However, please post your code (the img tag and/or the CSS rule for the div).但是,请发布您的代码(img 标签和/或 div 的 CSS 规则)。

You are probably getting the URL for the image wrong.您可能将图像的 URL 弄错了。 You can use relative paths safely in the stylesheet because all URLs in there are relative to the location of style.css (generally your wp-content/themes/yourtheme directory).您可以在样式表中安全地使用相对路径,因为那里的所有 URL 都相对于 style.css 的位置(通常是您的 wp-content/themes/yourtheme 目录)。 However this is not the case if you call an image in header.php - the relative path will change based on the URL of the page that is loaded (it will just add whatever is in the SRC attribute of the img element on to whatever the URL is).但是,如果您在 header.php 中调用图像,则情况并非如此 - 相对路径将根据加载的页面的 URL 更改(它只会将 img 元素的 SRC 属性中的任何内容添加到任何网址是)。

You can get around this by either specifying an absolute path (eg http://yoursite.com/wp-content/themes/yourtheme/images/yourimage.jpg ) OR by using a WP template tag that gets your stylesheet directory and add that to the img SRC attribute like so:您可以通过指定绝对路径(例如http://yoursite.com/wp-content/themes/yourtheme/images/yourimage.jpg )或使用 WP 模板标签来获取您的样式表目录并添加它来解决这个问题到 img SRC 属性,如下所示:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/yourimage.png" />
<img src="<?php echo get_bloginfo('template_url') ?>/img/logo2.png"/>

****这对我有用****

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

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