Getting two different fields from a thumbnail generated by Drupal´s Image Cache and the original image - drupal

I have an image field, which imagecache automatically creates a thumbnail for.
I am creating my own node view, in which I would like to show both the full size image and the thumbnai, but drupal treats them as a single field (one or the other will be shown depending on which option you choose in "Display Views").
How can I make ImageCache treat the original image and the thumbnail, as two separate fields, or two different keys in the field array?

What module did you use for image attaching to node? imagefield, image, ...?
However, one way is theming node (if you use CCK and imagefield modules):
Create node-{YOURNODETYPENAME}.tpl.php (you can take source from node.tpl.php) in your theme folder.
And add there this code (image field named as image, also you should disable output of this field in settings of fields of this content type, or remove one of print... line):
<?php
$img = current($node->field_image);
$alt = $img['data']['description'] ? $img['data']['description'] : $title;
...
print theme('imagecache', 'YOURIMAGECACHENAME', $node->img['filepath'], $alt);
...
print theme('image', $node->img['filepath'], $alt);
...
?>

Related

WordPress Page Templates for Custom Post Type

I have a custom post type, "Store Pages" for instance, which is an almost identical duplicate of the default Wordpress "Page" post type.
Like the "page" post type, I would like to create page templates (not post-type templates) and be able to select them from the "Template" drop-down within the "Page attributes" box in the page editor.
I have created several templates, but the drop-down menu does not appear; I am assuming this is because a custom post types does not allow support for this.
Is there a way I can create page templates for a custom post type without using "single-{post-type-name}.php" and having a dozen queries to load up different template files?
I have double checked the comments for the templates are correct as they appear when I create a new page (post type, "Page").
Help would be greatly appreciated.
starting 4.7 you can use the new feature described here https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
// … your code here
If I understood correctly you want a Select template dropdown for your custom post type.
You can do this easily through Advanced Custom Fields, here's a quick guide how to get through.
After installing the plugin you can access the Custom Field section, if you open it and click Add new it bring you to the field editor. Name it whatever you want, it's just for administrative display.
On Field type choose "Select", this will allow you to construct a select box on your backend, keep in mind the value of "Field name" you will need this later on the code.
Scrolling down you can add the values of the select box in the following format: "key value : Textual label" just assume for now you want 2 templates, one for audio posts and one for video posts.
If you keep scrolling down you can see the display rule for this field group, now you will have "post" and "page" by default, when you add different content types you will have the additional content types here to select, just go ahead and pick yours.
And, ta-da. If you go on the custom content type edit window you will find your new fresh select box here waiting for you.
The code integration now is very simple, just go onto your single-{post-type-name}.php template and pull the custom field data into your loop. Then you can use this to use get_template_part() to pull your custom templates.
<?php $template_type = get_field('template'); // This must match with the field name value ?>
<?php if (isset($template_type) && !empty($template_type)): ?>
<?php get_template_part( 'store', $template_type ); ?>
<?php else: ?>
// You should have a fallback for the all the existing posts without template set or if you create a new post without a template.
<?php endif; ?>
In this specific example the pulled template files will be in the format of store-{key-value-of-the-selectbox}.php, of course you can readapt that for you best convenience.

How to show the image of custom content type in drupal page.tpl.php?

I just created a custom content type in drupal7 and I want to manage the image of that content in my template. <?php print $content; ?> displays all the field of that content. But I just want to show the image only.How can I do so?
You must somehow get node id of node that contains that picture (hard-code it or something). Then use node_load() function to load whole node - image field will be one of available fields in loaded node object. Use print_r or something to check out available fields, so you can construct exact code, but it should be something like this:
$node = node_load($nid);
$picture_uri = $node->field_myimagefield['und'][0]['uri'];
// To get image path in specific image style you created:
$picture_style_path = image_style_url('style_name', picture_uri);
// To get original image path
$picture_original_path = file_create_url($picture_uri);
Then you can directly print picture path into img html tag...
But, are you sure that you are using right template file? If you want template just for your content type you should use node, not page template (which is wrapper for node template).

Add HTML to node title in Drupal module, not in theme layer

I want to add some functionality to my Drupal 6 module: I want to insert an image next to certain node titles (it involves a span and an img tag). I thought I could do this by just implementing mymodule_preprocess_node() and modifying the title. However, Drupal strips out all tags to avoid XSS attacks.
I've seen many solutions that involve the theme layer (most commonly http://drupal.org/node/28537), but I want to do this in my module, not in the theme. I don't want to modify any .tpl.php files or template.php. Can anyone give me tips on how to do this?
You mention that you've tried preprocess_node(), and are correct that, if you are storing the img tag as part of the node title, Drupal will indeed strip that out, as it runs $node->title through check_plain in template_preprocess_node().
My suggestion would be to store the actual image as an image field (using some combination of the imagefield module and imagecache for sizing), set the display of that field to be hidden on the CCK display tab for the given content type, and then attach the image to be part of the $title variable in your module's preprocess function. This solution would also allow you to display that image next to the title in any views you may need to create.
By 'certain node titles' - do you mean all nodes titles from certain node types?
If so, you can likely style the node using only CSS. By default all nodes will have a class that corresponds to the node type. Using CSS background images, you can add an image to the node title.
Your module can call drupal_add_css and add in any required CSS into the page. This way, it is theme independent.
I think the easier way is with javascript / Jquery.
You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.
You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:
<?php
drupal_add_js("var mymodule_imagelink = " . drupal_to_js($imagelink) . ";", 'inline');
drupal_add_js("my_js_file.js");
?>
then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...
Find text string using jQuery?
http://api.jquery.com/replaceWith/

Imagecache for non image CCKs?

I'm creating a view with images, videos, audio and documents (books) and I like to shown a picture of each of them in a carousel.
Everything works nicely with images and videos as far as we added a image-thumbnail (image filefield) to their CCK but we like to show a default image for audio and documents without changing the original CCK. Is it possible with imagecache (may be with imagecache_custom_code or views_custom_php) or we need to look for a different approach?
Thanks for your help,
m.
You are able to do this from two places.
First, you can go into Display Fields part of the CCK content type settings and change its default display in the teaser and node view. You will be given all the current imagecache presets as sizing options for display.
Second, you can also change the output as part of Views. Adding the image as a Field to show will give you the same set of preset options.
I'm going to say something very similar to Kevin above but I think it's the solution your looking for. To understand the problem:
you have 3 content types, two have thumbnails that you set individually
for the third you would like to have a default image displayed but not have to set it
I think you should still use a cck field.
Add a cck field for the image
Set a default image for that cck field (http://screencast.com/t/YzA0OWY4Mz)
Set your imagecache from the display tab
Hide the cck field from node edit for using http://drupal.org/project/formfilter, http://drupal.org/project/cck_field_perms, or hook_form
I might be mis-understanding your issue though.
Thanks for your both for your help.
I think you should still use a cck field.
You took a good picture of the scenario, but this is exactly the point of what I was trying to avoid. :-)
I don't want to use a cck field for books and I don't want to hide it with hook_form_alter.
I think I finally found a solution that didn't require adding extra CCK-fields to my "non-image" content types (audio and text).
There is a module called "Views custom field" (drupal.org/project/views_customfield) that allows you to add a PHP-processed-field to your view, so you can filter by content type and apply imagecache or imageapi code
$node=node_load($data->nid);
$img_path=drupal_get_path("module", "gt_medias") . "/images";
switch ($node->type) {
case "gt_video":
$img_path = $node->field_gt_thumbnail[0][filepath];
break;
case "book":
$img_path .= "/bookDefault.jpg";
break;
case "gt_audio":
$img_path .="/audioDefault.png";
break;
case "pingv_image":
$img_path = $node->field_pingv_image[0][filepath];
break;
}
$imgcache_url = imagecache_create_url('gt_medias_video_346', $img_path);
$output .= '<img src="'. $imgcache_url .'" ' />';
return ($output);
Where "gt_medias_video_346" is the name of my imagecache preset.
Once again, thanks you both for your help,
m.

How would you recommend adding an image as a custom field in WordPress?

I need to add an image to each page in WordPress.
I don't wish to insert it using the WYSIWYG editor, I just need the url as a custom field, which I later use in the template.
I tried using the CFI plugin (Custom Field Images), and I hacked my way into getting it to work with the rest of my plugins, but then I moved the site to the production server and CFI just didn't work for some reason.
I can't seem to find any other plugin that just lets you pick an image from the library and add it as a custom field.
I've downgraded to the point where I'm willing to manually enter all the URLs into each and every page. but before I do, I thought I'd check here.
Can anyone tell me what's the easiest, best way to add images as custom fields in WordPress (2.7.1 if it matters)?
In our WordPress template, each post generally only has one image 'attached', which is displayed outside the posts' (textual) content.
I simply upload the file using the edit posts' media uploader, never insert it into the post like JoshJordan above, then retrieve the image using a bit of code in the right place in my template file.
This would also work if you're using more than one image in your post, eg. in your post content. As long as you keep the image used as the 'main' post image as the first image (remember you can re-order images in your posts' image library by dragging them up and down), you're easily able to call it anywhere in your template file by using something like this:
<?php
$img_size = 'thumbnail'; // use thumbnail, medium, large, original
$img_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/gif') and post_type = 'attachment'");
$img_array = wp_get_attachment_image_src($img_id,$img_size,false);
echo '<img src="'.$img_array[0].'"' title="'.get_the_title().'" />';
?>
No need for copying and pasting image urls.
The template I have uses a manually-entered custom field for the splash image of each post. When I'm done writing my article, I upload an image, copy its URL from the upload tool, never insert it into my post, and then paste that URL into the "Image" custom field. Simple as pie and takes only seconds. Insignificant compared to the amount of time it takes me to write an article.
You can use the custom key value fields on posts as well. let's say you always give your images the key 'thumb'. you can then use this code to output them in your post as a thumbnail:
<?php
$values = get_post_custom_values("thumb");
echo “<img src=\”$values[0]\” class=\”thumb\”></a>”; ?>
Consider using Flutter it's a bit tricky to figure out at first, and has many really useful featured, including EIP (edit in place), and image handling.
After installing the plugin create a new "Write Panel", you'll figure it out from there. The plugin provides you with a rather intuitive GUI, which includes an image uploader. The template tags are very easy to use, I believe it's something like
<?php echo get_image('name_of_field'); ?>
I just had to build a site for a client that needed the same feature, I ended up using Flutter.

Resources