I've created custom theme look for one of my fields in views, and added field field_field_services_text_link[0]['rendered']['#markup'];?>. I tried to do same with my another image field, but it doesnt works. I used field_field_services_image[0]['rendered']]['uri']; ?>, but something wrong in this code. Any ideas how to solve it?
To troubleshoot this, perhaps the following would help. Paste it in your theme file instead of what you have above
<?php print_r($field_field_services_image); ?>
the print_r function will show you the structure of the array ($field_field_services_image) you're trying to work with and can show you whether the structure is exactly as you'd expect. Perhaps there's a problem somewhere in the [0]['rendered']... portion, and you can check that. It may be empty somewhere, etc...
You could always see whats being output with a var dump or even better, the devel module.
Related
I have a business goal forcing me to try to change the global wordpress query after the URL has been determined, but before the templates start outputting variables in the context of the original post. I need to be able to use a plugin to check some meta values on the original post, and then change the query to represent another post object to display different data without changing the url.
I've tried using setup_postdata() what seems like everywhere.
(tried including wp_reset_query();)
global $post;
$post = get_post(145, OBJECT );
setup_postdata($post);
However, the template is still outputting the original query.
I'm open to other solutions. Thanks in advance.
add_action('wp_loaded', function(){
query_posts(array('p'=>145,'post_type' =>'any'));
});
This worked out fine. It can be added just about anywhere. However, it messes up page templates, and displays pages as if they're single.php!!! If I can get around that, then I'll be in good shape. Help?
EDIT: Got it working. I have to check and use p for posts, and page_id for pages. So long as those are set, the templates will follow correctly. Otherwise it was trying to apply the standard post template to pages.
I wanted to override page.tpl.php for specific content type.
I have tried these thing, nothing works for me.
page--article.tpl.php
page--node--article.tpl.php
page--node--type--article.tpl.php
page--node-type--article.tpl.php
page--type--article.tpl.php
But when I targetted specific node by number i.e. page--node--8.tpl.php it worked fine.
I think page--article.tpl.php should have worked, but I dont know why its not working.
Please tell me if I am naming it wrong. and how can I debug such things. I have heard I can use Devel module, but know nothing about it. A slight hint in right direction will be appreciated.
Thanks in advance
You have to make sure that your template can handle this ... I got a code snippet that worked for me here:
http://drupal.org/node/1089656#comment-4426790
<?php
function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}
?>
Have you remembered to clear the cache (under Administer > Site configuration > Performance) so that Drupal is 'aware' of your new file & uses this rather than the default.
See here on Clearing cached data
Also, you may need to add a preprocess hook (I haven't used D7 myself, just 5/6 — think this has changed slightly.) This post on Drupal Stack Exchange seems to give more details
I just ran into the same problem.
node--recipe.tpl.php
not
page--recipe.tpl.php
this is, of course, with a content type called 'recipe'.
ALWAYS CLEAR THE CACHE...
Go to /admin/config/development/performance and click the clear cache button. It should be the first thing you do if you've made alterations to the structure of the backend and they're not showing as expected.
Otherwise you almost had it right
page--content-type-name.tpl.php
will allow you to theme the content type template.
I know this is an old question but if others arrive here, the way I override content types in drupal 7 is like this
node--contenttype.tpl
You will need to overwrite the specific node template for the content type if it is anything different than a Basic Page. Please check this out:
http://api.drupal.org/node/19080
Under this page copy/paste the items that are in the node.tpl.php page and overwrite it on how you want. You can access the node specifically by $node
Your template for articles would be:
node--article.tpl.php
Once this is created make sure you clear cache to ensure this works.
So depending on what you are trying to accomplish, there might be some CSS tricks that could help you out.
For instance, to override any CSS rules for a specific node type just remember that your pages for that node type will have a body class of "node-type-[TYPE]" (ie: .node-type-article)
You can then override the CSS as follows:
body.node-type-article #selector {}
Step 1.create a content type for example: my content type is "fullwidthpage"
Step 2. copy page.tpl.php and rename to page--fullwidthpage.tpl in template folder.
Step 3. Add this code to template.php
function <strong>YOUR_THEME_NAME_HERE</strong>_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
// If the content type's machine name is "my_machine_name" the file
// name will be "page--my-machine-name.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
}
}
Don't forgot to change YOUR_THEME_NAME_HERE
I am working in worpress front page.
I want to add a plugin to the page at a specific location manually but adding the code to the page myself.
I basically want to include a plugin in a certain page on a certain location. So I'm create a div...
<div id="plugin-holder">
**Plugin-will-appear-here-with-this-code**
</div>
Don't anyone know how this is done please?
Thanks
If you're wanting a plugin to appear somewhere, you'll be looking for "shortcode" functionality.
This is actually surprisingly easy to code, check out the examples in the Codex under Shortcode API - ie:
function bartag_func( $atts ) {
// ... do more things here ...
return "text to replace shortcode";
}
add_shortcode( 'bartag', 'bartag_func' );
Once you've called these functions you can use [bartag] in code and it will run your function and replace the shortcode with the generated text your function returns.
If you're adding shortcode functionality to your site, it generally makes most sense to code a really simple plugin and put it in that. The reason why this works best is that, over time, it's really easy to forget and upgrade a theme by mistake (or even change to a new theme) and thus break your site by losing your custom code in your former functions.php. Surprisingly, this is pretty easy to achieve and only requires some specially formatted comments at the top of your plugin file and a little common sense in coding - there are many tutorials and "how to"s around!
Here's a useful shortcode tutorial: http://www.reallyeffective.co.uk/archives/2009/06/22/how-to-code-your-own-wordpress-shortcode-plugin-tutorial-part-1/
You should add the relevant plugin code to functions.php.
I suspect you'll want to use some conditional tags, like is_home() to pinpoint your location. But maybe not, depending on what you are trying to do,
Also, if you're trying to to insert from a pre-existing plug-in, make sure you remove the register_activation_hook or activate_pluginname action.
If your plugin supports a sidebar widget you can simply "widgitize" the div tag that you wish to insert the plugin into.. Google the term and you are gonna find many resources.
I asked this question over in the actual tutorial, but not sure I'll get an answer anytime soon as it's almost 2 months old... so I'll take a gander here...
Tutorial is here: Build a WordPress Plugin to Add Author Biographies to your Posts
To sum up the tutorial and what the problem is, The tutorial adds an Author Bio on to the end of the content like so (the short version):
function x($content) {
return $content . "Author Bio";
}
add_action('the_content','x');
The Problem:
When someone uses:
$z = apply_filters('the_content', 'some content here');
echo $z;
The Author Bio will end up applied to $z and if $z is echoed out in the middle of some page… the Author Bio would be in the middle of some page… correct? (it's correct because I've tested it...)
Is there a better way to apply something to the end/under/below the_content hook? other than add_action(‘the_content’, ‘some_function’) because this to me seems evil...
or is apply_filters(‘the_content’, ‘some content here’) not the norm or something developers shouldn't be using inside their WordPress templates…? (which seems pretty much the norm, at least upon Google-ing formatting "the_content" outside the loop)...
Using apply_filters('the_content','some content here'), while it may not be 'the norm' (I don't know. I haven't seen it before, but if I needed formatted text, that's what I'd do), is a perfectly valid use of filters to get some text formatted like the content. Unfortunately, there's no better way to append something to content from a plugin. That is just the way these things work.
However, there's a (less than optimal) way of circumventing this. As part of the setup/install process for your plugin, have the user insert a custom function call, action, or filter into their theme. I know several plugins that do this, so it's not all that uncommon. Something like this:
do_action('my_super_awesome_bio_hook');
Would allow you to hook in without worrying about adding a bio to unexpected (and unintended) content. Even better would be inserting a filter:
echo apply_filters('my_super_awesome_bio_filter_hook','');
That would allow your plugin to modify the bio, but also allow the one using the plugin to override it if necessary (like on pages where they're just using excerpts, like search results, etc.).
Hope this helped.
Also, one minor addendum: you should be using add_filter, not add_action to append the Author Bio. add_action still works, but it's a filter you want to be using.
I bumped into a similar issue with a widget I'm dev'ing. I just found this:
http://codex.wordpress.org/Function_Reference/wpautop
Which I'm now going to use instead of add_filters('the_content'). I want the WYSIWYG formatting but I don't want things append to my content because it's not traditional content anymore.
In my template.php i have a function_name($form) with some basic html markup and a drupal_render($form['mail']) and drupal_render($form) that returns $output;
I'd like to include this small snippet & form in my page.tpl.php. Everything else in page.tpl.php is printed out as a single variable, so I'd like to do that for the $output of the function above.
What is the best way to do this?
In theory you could append the output of the function to the $content variable.
However, It sounds like you have something which may be better off as a block, rather than something that exists in your template.php.
You can quite easily create a module with a single block (which would be your function). You could add this where ever you wanted and easily turn off and on (and all the other block admin goodness) without having to change the code.
The other thing you could try out is the webform module. It's a nice module that has a large feature set, saving you the trouble of having to create a module. You can have a webform block which you can stick on that particular page (restrict to page).
I also see that your form is called mail. Webform also allows you to email form submissions. In addition, it provides a nice interface to see what users have submitted.