I'm poor php developer and i need code for my template.php to set specific template for some pages:
Example:
mypage/blog/about-me
mypage/blog/about-you
mypage/blog/about-us
Uses page--blog-about-first.tpl.php.
mypage/blog/about-him
mypage/blog/about-her
mypage/blog/about-them
Uses page--blog-about-second.tpl.php.
I can't find it anywhere so i'm asking here.
Check the naming conventions here:
https://drupal.org/node/1089656
If you need to do something more complex you can use the instructions here/:
https://drupal.org/node/223440#custom-suggestions
// Page template suggestions based off URL alias
$alias=drupal_get_path_alias($_GET['q']);
$args=explode('/', $alias);
if ($args[0]=='blog/about-me') {
$vars['theme_hook_suggestions'][] = 'page__simple_blog';
}
elseif ($args[0]=='artist') {
$vars['theme_hook_suggestions'][] = 'page__simple_blog';
}
This is the code. I got link /blog/about-me and link /artist. Suggestion works for artist page, but not for blog/about-me. Solution?
Related
I have worked on a few project with drupal, and the main suggestion I got when learning was to use Views to output your data, although this was fine for some items I found it cumbersome to achieve my projects requirements in the set time and to style it the controls were too limiting.
I want to know whats wrong with doing it like this, or whats right about doing it like this?
You run a query to get the specific data you want.
function custom_block_get_item($nid){
$result = db_query("SELECT `field_custom_item` FROM {field_data_field_custom_item} WHERE entity_id = :entity_id",
array(
':entity_id' => $nid,
));
return $result;
}
Then you loop through creating the html
function custom_block_display_blurb($nid){
$html='<div id="blurb_wrapper"><div id="recent_projects_blurb">';
$result=custom_block_get_blurb($nid);
while($row1 = $result->fetchAssoc()){
$a=check_markup($row1['field_custom_item'],'full_html','','');
$html.='
<span class="recent_project_text">'.
$a.'</span>';
}
$html.='</div></div>';
return $html;
}
Finally display it at the end
function custom_block_block_view($delta=''){
if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
switch($delta) {
case 'custom_block':
$block['content'] = custom_block_display_blurb($nodeid);
break;
}
return $block;
}
This seems like a kind of hacky way to do things, what is the correct drupal way?
Maybe you mean using the drupal theme layer? This allows for use of .tpl files and overriding. I have a simple block module that does it here Source here.
Here is some documentation on using the theme layer within a module.
Here is some official Drupal docs on themeing (mainly themes rather than using the theme layer in a module).
**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.
I know how to enable/disable comments on all pages. But how can I enable only a few, WITHOUT USING THE UI. In functions.php I just wanna go
if($this_is_a_page_i_want_comments_on) { enable_comments(); }
enable_comments(); doesn't exist, this is the part I need help with.
I could do this using WordPress Admin by allowing comments on all pages, then going to each page and disabling comments where I don't want them. This would take too long though.
P.S. I'm using Genesis framework.
Simply hook to the comments_open filter :) If you look-up in the source how the comments_open() function works, you'll notice that the function gets the post in question and then runs it's reposnse through the comments_open filter. Here's an example function to override that:
function my_override_comments_open( $open ) {
if ( $this_is_a_page_i_want_comments_on ) {
$open = true;
}
return $open;
}
add_filter('comments_open', 'my_override_comments_open', 1000);
I assume that you know how you'll identify the pages that you want to enable comments on - so that's up to you.
PP: I don't know if it will work in Genesis or not(I presume that it should though).
if (is_single('page_name_here') || is_single('other_page')) {
// Show the comment form here
}
Not sure if there's a better way, that's just my solution!
Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.
All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.
Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.
To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.
Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:
function basic_preprocess_page(&$vars, $hook) {
...
if( module_exists('path') ) {
$alias = drupal_get_path_alias( $_GET['q'] );
$site_section = "blog";
if( strpos( $alias, $site_section ) === 0 ) {
$VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
}
}
}
I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?
Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!
Thanks a million! (c:
EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?
They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:
function myTheme_preprocess_page(&$vars) {
global $node;
if ($node->type == 'blog') {
$vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
$vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}
Oh and don't forget to flush the Drupal caches after making changes to your template.php.
Ok, I found it:
http://drupal.org/node/223440#comment-991840
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
Credit to this function goes to user mfb.
I had a lot of trouble with this so I will explain it here in case anyone finds it useful.
This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:
page-blog, and page-blog-blog-post-title
Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.
What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.
Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)
I have an idea for running multiple wordpress themes at ounce. This might be a good thing to build into a wordpress plugin, if possible. And yes, I might undertake such a task if it isn't ridiculously hard.
(Also, if you are interested in teaming up with me, let me know (leave a comment), I am decent at javascript and php, but not to great, and would love some help!)
This is how I see it working:
Current "set" theme is accessible here: "www.foo.com/"
Second theme accessible here: "www.foo.com/index.php?set_theme=theme2&"
Third theme accessible here: "www.foo.com/index.php?set_theme=THEME_NAME_HERE&"
etc...
This could be used for javascript fall backs. For example, if you go to www.foo.com/?page_id=9 and have javascript turned on, you will hit a javascript redirect to "www.foo.com/index.php?set_theme=THEM_WITH_JAVASCRIPT&page_id=9".
This is how I imagine the plugin code looking/working:
if(isset($_GET['set_theme'])){
$loadme = cleaned($_GET['set_theme']);
if($loadme exists){
loadtheme($loadme);
} else {
//go on as usual, as if this plugin doesnt exist
}
} else {
//go on as usual, as if this plugin doesnt exist
}
And of course, all the links would have to ad ?set_theme=FOOBAR&
So, my main questions are:
How and where does Wordpress select the current theme?
How/where would you ad this to modify internal links-
if(isset($_GET['set_theme'])){
echo "?set_theme=" . $_GET['set_theme'];
}
Do you know of any good websites to point me in the right direction as to how to make WP plugins?
You might like to look at the Theme Switcher plugin to see how that accomplishes this task - should give you some ideas.
Don't really need a plugin, just add this to functions.php:
function theme_switcher() {
if(isset($_GET['theme']) $theme = $_GET['theme'];
global $wpdb;
if (isset($theme)) {
$wpdb->prefix
$queries = “UPDATE “.$wpdb->prefix.”options SET option_value = ‘”.$theme.”‘ WHERE option_name = ‘template’ OR option_name = ‘stylesheet’ OR option_name = ‘current_theme’;”;
$wpdb->query($query);
}
}
add_action('wp_head','switchTheme')
then use mywebsite.com/?theme=myNewTheme to switch them
Disclaimer: untested!!
Found a plugin that was 98% of the way there. Wordpress Theme Switcher Reloaded.
Just changed the ts_get_theme() function as follows:
function ts_get_theme() {
if (!empty($_GET["wptheme"])) {
return $_GET["wptheme"];
} else {
return '';
}
}