I would like to get the image path of a field. I'm in a node and I need the Url of image in order to put it as a background-image in inline css.
I can't find the solution. Can you help me?
To get just the path of an image from it's URI:
file_create_url($node->field_image['und'][0]['uri']);
More information on file_create_url() can be found here: http://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7
To get the path of an image created using an image style from it's URI use:
image_style_url($style, $node->field_image['und'][0]['uri']);
More information on image_style_url() can be found here: http://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
I am afraid, none of the solutions above are correct. They don't follow Drupal standards.
// field_video_image is the name of the image field
// using field_get_items() you can get the field values (respecting multilingual setup)
$field_video_image = field_get_items('node', $node, 'field_video_image');
// after you have the values, you can get the image URL (you can use foreach here)
$image_url = file_create_url($field_video_image[0]['uri']);
More details here:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
I sometime use this:
$node = node_load($nid);
$img_url = $node->field_userimage['und'][0]['uri'];
<img src="<?php print image_style_url($style, $img_url) ?>" />
You also can use the Image URL Formatter module. It allows to change the field format in order to get only the URL:
Bonus: it works with Views as well:
to get image style url:
$field = field_get_items('node', $node, 'field_image');
$image_url = image_style_url('landingpage_slider', $field[0]['uri']);
Related
Theme
I have a content type with image and URL fields, I need to show image as banner on the path where URL field is matched with requested path using views.
I tried with
1-> adding "Alias" field as contextual filter in views.
2-> adding URL field
3-> I also tried with URL field with PHP Code in contextual filter:
if(drupal_is_front_page()) {
return '<front>';
}else{
return request_path();
}
3rd point works partially for only one path argument, like if current requested path is services/one and views contextual filter only takes first path component as you can see in attached image
However, I need to set contextual filter with whole path no matter how many components are requested.
How would I do that?
I have done this by embedding the views block in tlp and passing the URL field filter by code. See code below:
$path = request_path();
$query = 'SELECT field_url_url FROM {field_data_field_url}
WHERE bundle = :bundle AND entity_type = :entity_type AND field_url_url = :field_url_url';
$path = db_query($query, array(
':bundle'=>'page_banner',
':entity_type'=>'node',
':field_url_url'=> $path
))->fetchField();
if (!empty($path)):
print views_embed_view('page_banner','block', $path);
endif;
I hope this will help someone who needs to add contextual filter as request URL.
I'm working on a Drupal 7 website. I need custom layout for some pages. so I created page--customContentTypeName.tpl.php file and it addresses perfectly.
The problem is, I need to display some fields in page tpl. The code below works fine in node tpl, but page tpl :/
<?php print $content['field_images']['#items']['0']['filename']; ?>" />
How can I call custom fields into page tpl?
Appreciate helps!! thanks a lot!!
** SORTED **
with custom field editing... here is the tutorial video: http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54
For page.tpl.php
if you access the node directly you can use $node variable
$node['field_images']['und'][0]['filename']
else use $page variable.
$page['content']['system_main']['nodes'][1]['field_images']['#items'][0]['filename'];
but remember in a page variable you might have more than one node.
The structure changed in 7, the field is keyed by language first ("und" by default which means "undefined"), then you can follow this example:
// Array of Image values
$images = $node->field_images['und'];
//If you don't know the language, the value is stored in:
$node->language
// First image
$image = $images[0];
// If you need informations about the file itself (e.g. image resolution):
image_get_info( $image["filename"] );
// If you want to access the image, use the URI instead of the filename !
$public_filename = file_create_url( $image["uri"] );
// Either output the IMG tag directly,
$html = '<img src="'.$public_filename.'"/>';
// either use the built-in theme function.
$html = theme(
"image",
array(
"path" => $public_filename,
"title" => $image["title"]
)
);
Note the usage of the uri instead of the filename for embedding the image in a page because the File API in Drupal 7 is more abstracted (to make it easier to integrate with CDN services).
there are 2 useful modules in drupal for theme developers:
Devel and Theme_developer
Devel module provides a function called dsm() .
using dsm you can recognize that how elements are stored in different objects.
like nodes or ...
for example you can use this statement : dsm($node)
the structure of any nodes in the page will show up in the message box.
you can type the statements in your codes.
I'm looking for a plugin (or better yet, not a plugin) for wordpress that lets me generate standard content elements, or includes for posts and pages.
For example, my_content_1 could be:
buy it now for $23!!
Which could then be included in posts and pages using some kind of syntax (or whatever) like:
Welcome to my site, blah blah blah.. check out this product - %my_content_1%
Not looking for anything fancy, anything that does this sort of thing would be awesome.
The point of this being much like a regular php include I could have the same information updated in one place and applied over many pages/posts.
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/
However, other suggestions would be good as I'm not too confident in the quality of the code for that plugin.
Not sure about a plugin, but how about simply creating something yourself? If you created a PHP page and set up variables such as
$content->title = "This is a title"
$content->smallText = "Insert some short paragraph here"
And then just include it in your header? You could store it in your theme directory and then call it like so
<?php $themeFolder = get_bloginfo("template_url"); ?>
<?php include($themeFolder."/content.php") ?>
Would that be suitable?
How about creating a few files and link them in using shortcode?
ie: open your themes/functions.php file add this..
<?php
function wp_my_shortcodes($atts)
{
extract(shortcode_atts(array(
'type' => '', //author, rss, adverts
), $atts));
switch($type) {
case 'author' : $display = wp_display_author_info(); break;
case 'rssview' : $display = wp_display_rss_info(); break;
case 'adverts' : $display = wp_display_adverts(); break;
default : $display = wp_display_author_info(); break;
}
return $display ;
}
add_shortcode('mycontent', wp_my_shortcodes);
function wp_display_author_info()
{
include(TEMPLATEPATH.'/my_author_info.php');
}
function wp_display_rss_info()
{
include(TEMPLATEPATH.'/my_rss_info.php');
}
function wp_display_adverts()
{
include(TEMPLATEPATH.'/my_adverts.php');
}
?>
using shortcodes inside your posts you can then bring in which ever piece of content that you want.. in the example above I've created 3 pages in the template root folder called
my_author_info.php, my_rss_info.php, my_adverts.php all of which speak for themself..
my_author_info.php
this page could use the the_author_meta() to populate a div box with included author info,
my_rss_info.php
include your subscription box to let users subscribe to your blog
my_adverts.php
include 4x 125x125 adverts?
so in the post i could use
[mycontent type='author']
[mycontent type='rssview']
[mycontent type='adverts']
if no argument is added to the shortcode then the default view is shown, in this case..
[mycontent]
would return the authorview as default...
this would then include that file in the content...
just remember to create the included files :)
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/
On my Drupal site I've got a set of Primary Links. The ones that expand I'd like to make the parent not click able e.g
-home
-about
-history
-website
Only home, history, website should link to a page. If the user clicks on aboutnothing should happen. I've tried searching around the admin panels as well as leaving the field blank but it doesn't seem to be working. I'd assume I'd have to hardcode this? If so, how?
Try this module http://drupal.org/project/special_menu_items
Its probably the simplest way to achieve what you want.
If you can live with it, the easiest solution is to use js to disable clicks.
Adding a yourtheme_menu_item function in template.php seems to be the way to go for this. The documentation for the original function is at http://api.drupal.org/api/function/theme_menu_item
The function passes a $has_children variable and a $menu variable, so it should be pretty easy to adjust Primary Menu items with children as needed.
Some untested example code:
function yourtheme_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
// ... original theme code copy-pasted ...
if ($has_children) {
$modified_link_name = youtheme_write_menu_item_without_links($link);
return '<li class="'. $class .'">'. $modified_link_name ."</li>\n";
} else {
// From original function
return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
}
}
You just need to add in the path the phrase <nolink>.
"You just need to add in the path the phrase <nolink>"
i used this before and it worked, but for some reason it didnĀ“t work for a different site that i am using now.
So i tried to write # in the path and worked fine for me.
Hug everybody.
Does anybody know if there is a way of having an image upload field on a post so I can upload a specific image for that post.
Thanks
It's not currently possible (have been looking after it for months with no luck).
Are you trying to display that image as a thumbnail (while keeping it easy for the authors)?
The nicest approach is auto-detecting the first image in the post. Users don't need to deal with custom fields nor URLs:
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
// With TimThumb
<img src="/thumb.php?src=<?php echo catch_that_image() ?>&w=200&zc=1&q=200" alt="<?php the_title(); ?>"/>
Found it here: http://snipplr.com/view/23968/wordpress--get-first-image-of-post-without-custom-fields/
If that's not what you were looking for please give more details and we'll try to find a solution.
:)
The solution I used was the Flutter module, which let me create custom content types.
It's not an ideal solution but I'm currently just using a custom field called "thumbnail" and putting the location of an uploaded image in there. Then, when I display the post details somewhere I use the value from the custom field as the <img src=""> value.
To upload the image in the first place I just use the normal image upload tool in the editor and copy the uploaded name into the custom field as I'm writing the post.
Not very elegant but it works.