Is there a way to pass some $variables to block & node from function like template_page_preproceess on drupal 6?
$vars should already be available to node.tpl.php (if $vars doesn't work for you, use $variables). To add another variable to the $vars, add the following to template.php:
function yourtheme_preprocess_node(&$vars, $hook) {
$vars['yourvariable'] = "your variable content";
}
And then in node.tpl.php you can output the contents of the variable where you want by adding this:
<?php if ($yourvariable): ?>
<?php print $yourvariable ?>
<?php endif; ?>
Don't forget to flush the theme cache if you're not seeing your new item.
Related
I wanted to display the header of a particular theme. I have done by writing this code in the template like below :
<?php
/*
Template Name:CLIENT LIST
*/
get_header("header1");
?>
Is this correct ? I have named the file as header1.php. I have already header.php.I wanted to make another header file so i named it as header1.php.
The documentation shows you the correct approach:
<?php get_header( $name ); ?>
$name
(string) (optional) Calls for header-name.php.
Default: None
So, name your file header-something.php and call it with get_header( 'something' );
I am creating my own little widget sitemap to put in my footer. Here's my code:
function widget($args, $instance)
{
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if ( $title ) { echo $before_title . $title . $after_title; }
// WIDGET CODE GOES HERE
// ?> THIS IS A TEST! <?php
?><ul><?php
wp_list_pages('title_li=<h2>MAP OF THE SITE</h2>&sort_column=menu_order&depth=0&include=57,55,59,61,63,65,192');
?></ul><?php
echo $after_widget;
}
but looks like wp_list_pages doesn't return anything. Not even the title. Indeed if I uncomment that "this is a test" then it's shown.
The strange thing is the same wp_list_page is implemented also in header.php to obtain menus.
First rule when creating Wordpress custom functions is to check if function with the same name already exists.
<?php if (!function_exists("function_name")) {...} ?>
Second rule is to name your function with really unique name, in order to avoid conflicts. In your case this function already exists, that's why you cannot extract the data as you expect...
One more thing, I assume you will put this whole function code in your theme's functions.php file.
I created a template, my-node-form.tpl.php, to theme a particular form. If this is the template content, then drupal_render works fine:
<?php
?>
<?php
dsm ("debug. inside my-node-form.tpl.php");
foreach ($fieldnames as $fieldname){
print drupal_render($form[$fieldname]);
}
return;
?>
However, if there is a blank line between the two sets of php-tags, then drupal_render returns nothing:
<?php
?>
<?php
dsm ("debug. inside my-node-form.tpl.php");
foreach ($fieldnames as $fieldname){
print drupal_render($form[$fieldname]);
}
return;
?>
Anyone know why this is?
I had a bug which corrupted some of the formfield data. I fixed the bug and drupal_render now works fine.
(sorry for my English) and I hope to explain!
I'm looking for a solution to display different output for a custom filed, named "cw". The my theme of much use in custom filed, so I created a function to simplify your calls:
/**
* Get custom field of the current page
* $type = string|int
*/
function my_getcustomfield($filedname, $page_current_id = NULL)
{
if($page_current_id==NULL)
$page_current_id = get_page_id();
$value = get_post_meta($page_current_id, $filedname, true);
return $value;
}
said this, I created the custom filed in my theme:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); if(!empty($video_code)) : ?>
<div class="video_code"><?php echo $video_code; ?></div>
Since the custom value is a URL, type: http://127.0.0.1:4001/?f=cadbe2676d5108523f931a68eedb3582, I need to extract just the ID. using this technique PHP Regex to get youtube video ID?, I modified my custom filed in this way:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); $url = $video_code; parse_str(parse_url($url, PHP_URL_QUERY )); if(isset($video_code[0])){ ?>
<div cacaolink="http://127.0.0.1:4001/?f=<?php echo $f; ?>"></div>
<?php } else { ?>
<?php echo 'Video n.d.'; ?></div>
<?php endif; } ?>
In this way everything works perfectly.
Now, the custom value can be those listed below:
<div cacaolink="http://127.0.0.1:4001/megavideo/megavideo.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/videobb/videobb.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/?f="></div>
**Finally here is the problem: How can I use an elseif according to the custom value?
any help is appreciated :)**
So what you need in short is extracting "video id" part from urls having one of 3 structures defined above. If I understood you right, you can just use the next code:
$video_id = preg_replace("/.*\?(videoid|f)=/", '', $customvalue);
Where $customvalue is the value extracted from your custom field with my_getcustomfield.
I am trying to pull in a styled sidebar specific to the category. I have the following code which works, but it is pulling in BOTH my new sidebar and the default. What am I doing wrong here?
From category.php
<?php get_sidebar();
if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');
} else {
include(TEMPLATEPATH . '/sidebar.php');
}
?>
<?php get_footer(); ?>
Because you're calling sidebar twice; do this:
<?php
if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');
} else {
include(TEMPLATEPATH . '/sidebar.php');
}
?>
There is a slight problem with the code being provided to you. But as Eimantas suggested, you could simply make a new file called category-39.php which will accomplish the job perfectly, if though for some reason you are still wanting to continue using your category.php file, then here is what you would need to do:
if ( is_category('39') ) {
get_sidebar('2');
} else {
get_sidebar();
}
?>
<?php get_footer(); ?>
The difference between this and the code that you posted is that I have removed
<?php get_sidebar(); ?>
Additionally I have changed in_category to is_category. The reason for this is because when looking at the category page itself using is_category will change on the category list, whereas in_category only looks at the current post and therefore will not change according except when looking at the single.php page.
Example:
in_category will change the sidebar for the following url www.mysite.com/category/stuff/myfirstpost
But it will not change the sidebar for this url www.mysite.com/category/stuff
Simply using is_category will fix this problem.
The next thing would be using
get_sidebar('2');
and
get_sidebar();
get_sidebar(); will run the appropriate wordpress functions related to the sidebar in addition to including sidebar.php.
get_sidebar('2'); on the other hand will run all the appropriate wordpresss functions related to the sidebar while also loading sidebar-2.php.
Hope this helps,
Remove this from your code:
get_sidebar();
otherwise you call this file "sidebar.php" twice...
if you look into wp documentation http://codex.wordpress.org/Function_Reference/get_sidebar
you can do it like that:
<?php
if ( in_category('39') ) :
get_sidebar('two'); //this will include sidebar-two.php
else :
get_sidebar();
endif;
?>
You should create separate template named category-39.php and do common design stuff. WP itself will notice that it should apply this template to category with id=39. No need for if else statements.