【问题标题】:Unable to link stylesheet and images in wordpress using my own themes无法使用我自己的主题在 wordpress 中链接样式表和图像
【发布时间】:2014-08-12 12:51:02
【问题描述】:

我上传了我自己的设计。我只是无法正确链接样式表。目前我的主题位于 wp-content-themes-(carrental->my theme folder name)-style.css 中。 我的 index.php 页面也和 style.css 在同一个文件夹中。

我是这样链接的:

// for the style.css
<link rel="stylesheet" href="<?php bloginfo('style.css'); ?>" type="text/css"  />

// for image in index.php
<img src="<?php bloginfo('carrental'); ?>/assets/acr-logo-1.jpg" width="200" height="100">

请问正确的链接方式是什么?

【问题讨论】:

  • 请参阅我的回答下添加的评论。

标签: php html css wordpress


【解决方案1】:

包含样式表和脚本文件的最佳方式是通过wp_enqueue_script()wp_enqueue_style() 在functions.php 中。以 wordpress 上的默认激活主题(例如 241)作为参考。

例如,使用 wp_enqueue_script 将确保脚本文件加载一次,无论如何,因此不会在途中出现令人不快的意外。以 jquery 为例,您可以将其添加为其他脚本的依赖项。

如果您喜欢包含在 index.php 的标头中,您可以使用更多选项: - ">,另一种选择是使用 ...src="echo get_stylesheet_uri()".. 或者您可以使用其他返回主题路径的函数,就像其他人已经写在这里一样。

想法是输出函数返回,所以 get_stylesheet_uri() 不起作用,因为你不回显它。当您想将值存储在变量中时,您可以使用 get_stylesheet_uri()。

但首选方式仍然是使用 wp_enqueue_script 和 wp_enqueue_style 函数。

【讨论】:

    【解决方案2】:

    以下代码将返回样式表路径

    bloginfo('stylesheet_url');
    

    所以使用:

    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_url'); ?>">
    

    回显样式表链接!

    更多关于bloginfo的信息可以在here

    找到

    【讨论】:

    • 我确实按照你的建议使用了,但没有效果。
    • 你上面贴的不是我建议的。复制我答案的第二个代码块中列出的完整代码并将其粘贴到您的 php 文件中。它会起作用的。函数返回路径,你不将'style.css'作为参数传递给函数,你传递'stylesheet_url'来获取样式表路径的返回值。
    • 对于图像,只需使用 在你的图片路径之前 :)
    【解决方案3】:

    对于样式表,您必须使用样式的链接元素

    bloginfo('stylesheet_url');
    

    对于图片,您必须提供图片的完整网址 就像如果你的图像目录在你的主题中,那么你可以在图像源中给出这个

    bloginfo('stylesheet_directory').'/images/imagname.png'
    

    【讨论】:

    • Shah Rukh..你能告诉我如何准确地显示图像..我的图像存储在里面..assets 文件夹。我这样显示," width="200" height="100">
    • assets 文件夹位置在哪里?是在public_html 还是在哪里?
    • localhost/wordpress/wp-content/themes/(carrental)/assets
    • 我想那你一定要用这个&lt;img src="&lt;?php bloginfo('stylesheet_directory').'/assets/acr-logo-1.jpg'); ?&gt;" width="200" height="100"&gt;
    • 非常感谢..我试过这种方法,它奏效了../assets/acr-logo-1.jpg" 宽度="200" 高度="100">
    【解决方案4】:

    不要使用绝对网址。使用完整网址。见下面代码

    <link href="<?php echo get_template_directory_uri().'/style.css" />
    <img src="<?php echo get_template_directory_uri().'/assets/acr-logo-1.jpg' ?>" />
    

    【讨论】:

    • &lt;style&gt; 标签包含src 怎么办?
    • 抱歉写错了...但我的主要观点是不要使用绝对网址..希望你明白
    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2013-06-02
    • 2013-05-29
    • 2020-01-09
    • 2014-11-10
    • 1970-01-01
    • 2017-01-19
    • 2013-06-02
    相关资源
    最近更新 更多