^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

WordPress不同页面加载不同css

对于喜欢折腾的盆友来说,会给页面加载许多绚烂的效果,(如点击本文章标题下的 high 一下)可是有些特效代码或 css 只是在文章中或者在单页中需要调用,这个时候,首页或者其他页面是不需要调用的,那么下面的代码就很有用了:

<?php if (is_category()||is_archive()){?>
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/css/list.css" type="text/css" />
<?php }elseif(is_single()){ ?>
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/css/article.css" type="text/css" />
<?php }else{ ?>
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css" type="text/css" />
<?php }?>

上述代码的意思是如果是分类则加载list.css、如果是文章页则加载article.css,如果是其他页则加载style.css

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

大家可以举一反三。

2018-4-2 有小伙伴希望写入到 function.php 文件中,可以按照这样来写,写个函数,然后调用

function load_css() {
if (is_category()||is_archive()){
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/list.css" type="text/css" />';}
elseif(is_single()){
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/article.css" type="text/css" />';
}
else{
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/style.css" type="text/css" />';}
}

如果有多个的话,为了在源代码中代码看起来美观,自动换行可以这样写

function load_css() {
if (is_category()||is_archive()){
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/list.css" type="text/css" />'. PHP_EOL;}
elseif(is_single()){
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/article.css" type="text/css" />'. PHP_EOL;
}
else{
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/style1.css" type="text/css" />'. PHP_EOL;
echo '<link rel="stylesheet" href="';
echo bloginfo('template_directory');
echo '/css/style2.css" type="text/css" />'. PHP_EOL;}
}

赠人玫瑰,手有余香。