如何添加的版本style.css
在下面的WordPress中,我可以在Joomla中完成。
<link rel="stylesheet" href="/templates/example/css/style.css?v=1.2">
我知道style.css
将动态加载。请帮助我怎么做。如何添加的版本style.css
在下面的WordPress中,我可以在Joomla中完成。
<link rel="stylesheet" href="/templates/example/css/style.css?v=1.2">
我知道style.css
将动态加载。请帮助我怎么做。版本号是的参数wp_enqueue_style()
.
根据法典,以下是wp_enqueue_style
接受。
wp_enqueue_style( $handle, $src, $deps, $ver, $media );
例如,要加载带有版本号的样式表,您需要执行以下操作:function wpa_90820() {
wp_enqueue_style(\'my-styles\', get_stylesheet_directory_uri() .\'/my-styles.css\', array(), \'1.0\' );
}
add_action(\'wp_enqueue_scripts\', \'wpa_90820\');
如果您是一名主题开发人员,您可能希望在推送新版本时强制重新加载资产。
因此,主题的版本控制是在style.css
/*
Theme Name: Your Theme Name
Version: 1.0.2
*/
在您的functions.php
:$theme = wp_get_theme();
define(\'THEME_VERSION\', $theme->Version); //gets version written in your style.css
稍后当您将CSS或JS排入队列时,请使用THEME_VERSION
作为第四个参数:function theme_styles()
{
wp_enqueue_style(\'main\', get_template_directory_uri().\'/css/main.css\', [], THEME_VERSION, \'all\');
}
add_action(\'wp_enqueue_scripts\', \'theme_styles\');
将输出到页面:.../your-theme-name/css/main.css?ver=1.0.2
当您有更多需要关注的资产并且不想手动更改它们时,这将非常方便。与硬连接版本不同,您可能会发现在某些情况下,动态更新样式表会更好,因此每当您更改样式表时,它会立即自动更改和刷新浏览器缓存,而无需编辑您的函数。php一次又一次。
您可以使用filemtime()来完成此操作。下面是如何在引用parent\\u样式的子样式中执行此操作
wp_enqueue_style( \'child-style\', get_stylesheet_directory_uri() . \'/style.css\', array( $parent_style ), filemtime( get_stylesheet_directory() . \'/style.css\' ) );
可以使用以下方法之一实现此目的:
1) 在标题中添加以下标记。主题的php文件。
<link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); ?>\'?v=1.2" type="text/css" media="all" />
2)在函数中添加以下代码。主题的php文件。function theme_styles()
{
// Register the style like this for a theme:
// (First the unique name for the style (custom-style) then the src,
// then dependencies and ver no. and media type)
wp_enqueue_style( \'style-css\', get_template_directory_uri() . \'/style.css\', array(), \'1.2\', \'all\' );
}
add_action(\'wp_enqueue_scripts\', \'theme_styles\');
有关更多信息,请参阅this page.将css加载到wordpress主题中的最佳方法是在函数中使用以下代码。php文件:
function theme_styles()
{
global $ver_num; // define global variable for the version number
$ver_num = mt_rand() // on each call/load of the style the $ver_num will get different value
wp_enqueue_style( \'style-css\', get_template_directory_uri() . \'/style.css\', array(), $ver_num, \'all\' );
}
add_action(\'wp_enqueue_scripts\', \'theme_styles\');
这是在主题中加载样式的正确方法,也是进行阶段/测试的最佳方法,因为每次刷新都会提供样式的更新版本。如果要避免第1种方式加载,可以使用此简短版本,并将以下行放在标题中。php文件,并将得到相同的结果:
<link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); echo \'?ver=\' . filemtime( get_stylesheet_directory() . \'/style.css\'); ?>" type="text/css" media="screen, projection" />
干杯尝试以下操作:
将此添加到函数。php
function autoVer($filename){
$url = get_template_directory_uri() . $filename;
$file = get_template_directory() . $filename;
if ( file_exists($file)) {
return $url . \'?v=\' .md5(date("FLdHis", filectime($file)) . filesize($file));
}
clearstatcache();
}
将其添加到页眉或页脚->自动版本(\'/js/main.js\');与目前介绍的方法相反,我认为最好使用样式顶部的版本号。css文件:
// style.css
/**
Theme Name: My theme
...
Version: 1.2.3
...
**/
要在css中使用主题的版本,请在函数中添加以下内容。php(或等效)脚本:<?php
wp_enqueue_style(
\'my-theme\',
get_stylesheet_directory_uri() . \'/style.css\',
[],
wp_get_theme()->get(\'Version\')
);
这意味着,在编辑样式之后。css文件,您只需更改同一文件顶部的版本号即可查看前端的更改。如果检查主题HTML的标题部分,您将看到以下内容:
<link rel=\'stylesheet\'
id=\'my-theme-css\'
href=\'... style.css?ver=1.2.3\'
type=\'text/css\'
media=\'all\'
/>
另一个解决方案是,如果你还不能弄清楚,那就是更改子主题样式的文件名。css到例如style2。css,然后在子主题的函数中更改它。php文件,如下所示:
wp_enqueue_style( \'style\', get_stylesheet_directory_uri() .\'/style2.css\' );
这将导致Wordpress请求并为您的网站提供新的CSS文件。这里,我们使用WordPress“wp\\u get\\u theme()来获取主题版本(如主题的样式表style.css中所指定):
// get theme version
function rave_get_version() {
$theme_data = wp_get_theme();
return $theme_data->Version;
}
$theme_version = rave_get_version();
global $theme_version;
wp_enqueue_style( \'main-style\', get_template_directory_uri() .\'/assets/css/style.css\', [], THEME_VERSION, \'all\' );
实际上,您不需要取消缓存该样式。css文件。
您可以编写自己的css文件,然后像@vinod dalvihas编写的那样排队。
function theme_styles()
{
wp_enqueue_style(\'stylecss\',get_template_directory_uri().\'/yourname.css\', array(), \'1.2\', \'all\' );
}
add_action(\'wp_enqueue_scripts\', \'theme_styles\');
根据浏览器规则,最新的css文件具有更高的优先级,因此请确保使用!如有必要,请在css代码中输入重要信息。祝你好运
这是通过调用function bloginfo($show)
两次首先是样式表,其次是版本号。
<link rel="stylesheet" id="style-css" href="<?php bloginfo(\'stylesheet_url\'); ?>?ver=<?php bloginfo(\'version\'); ?>" type="text/css" media="all" />
这就是全部。希望有帮助或有用。您可以查看所有可用的参数,并查看使用bloginfo()
作用Ignore my comment since @Ravs has pointed out my error regarding the \'versions\' parameter for the bloginfo() function. It does indeed print the Wordpress version number.
我需要在每个<a> 元素检索者the_terms() (或类似函数),以便在fancybox模式窗口中打开分类permalink。我整个下午都在寻找一个可以处理它的过滤器,但没有成功。无论如何,我得到了下面这个(糟糕的)解决方案:$cities = get_the_terms($post->ID, \'cities\'); foreach ($cities as $city) { echo \'<