简体   繁体   中英

How to add PHP or Javascript code to a custom content type

I am working with Drupal 7.17.

I have a custom content type I created called Sidebar Ad (machine name sidebar_ad) that I want to add Javascript and PHP to. From what I understand, in the templates directory of the theme I am working with, I need to create a file called 'page--sidebar_ad.tpl.php' and I can put PHP and Javascript code in there, and it will only run in blocks and pages that contain the custom content type Sidebar Ad.

I am trying this out with one of the stock themes, Bartik, and it is not working for me. I tried it by copying the page.tpl.php file, renaming it page--sidebar_ad.tpl.php, and adding a bit of text in a few places - at the top of the php file, and in the content div. But I don't see anything change on the pages on my site that contain that custom content type.

Is there something I am missing?

Most likely the thing you're missing is flushing Drupal's cache...any time you add a hook or template file, you need to flush the cache so the various registries can pick it up.

In the case of an overridden page template file you also need a copy of the standard page.tpl.php file in your theme. Same goes for the node template and most likely others, you need the original in there for the overrides to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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