dtqqq24248 2016-12-09 16:35
浏览 6
已采纳

比较不同模板部件中的值

For certain reasons I have to add a bit of style (margin) to the footer of the page. It should only be applied when the content part of the page is a product archive.

I tried to create a variable in the archive-products.php file like this:

<?php global $template;

$template = 'prodarchive';

var_dump($template);?>

According to the var_dump the value is set. Next I tried to call it in my footer.php like this

<?php if ($template === 'prodarchive' ) {?>

and then the stuff it needs to do.

Some tests shows me that the footer.php file is not able to reproduce the value of $template. What is wrong?

  • 写回答

2条回答 默认 最新

  • donglu9978 2016-12-10 11:03
    关注

    The easiest solution (as pointed out by @seemly) is to apply the body_class() function to the <body> tag:

    <body <?php body_class(); ?>>
    

    This will apply a range useful classes that you can use for CSS, including one of the product archive pages.

    Another approach is to use is_post_type_archive('product'), which returns a boolean value.

    As to why your global variable isn't working in footer.php, I'm pretty sure get_footer() isn't passed all global variables when it's called.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)