I'm trying to make Drupal output the body of a particular content type in div tag with certain class name.
First, I tried to override node.tpl.php, but I discovered $content variable here already contains body along with additional custom cck fields. And what I'm trying to do is precisely to separate body from another fields.
After that I looked at content-field.tpl.php and found it's executed only for custom cck fields. So doesn't suit me too.
So.. how to theme a node body field?
Start by calling
print_r($node);
This is what you node object contains - and you can use any of its members directly instead of just printing the content. Analyse this and you'll know what to call exactly ;)
In case of the body, the unstyled content can be accessed with:
$node->content['body']['#value']
For other fields, you do:
$node->field_name[0]['view']
(where [0] is the index of the item in the array - useful for ImageField when you can upload many pictures).
For example, here's the content of my node-event.tpl.php, displaying details about an event:
<div class="event clear-block">
<?php
$class = (convert_datetime($node->field_event_date[0]['value']) < time()) ? 'past' : 'future';
echo "<h3 class='header'>When?</h3><p class='$class'>".$node->field_event_date[0]['view']."</p>";
echo "<h3 class='header'>Where?</h3><p>".$node->field_event_place[0]['view']."</p>";
echo "<h3 class='header'>What?</h3>".$node->content['body']['#value'];
echo "<h3 class='header'>How much?</h3><p>".$node->field_event_price[0]['view']."</p>";
echo "<h3 class='header'>How to participate?</h3>".$node->field_event_subscribe[0]['view'];
?>
</div>
Take a look at this: http://capellic.com/blog/theming-the-node-body-field
If you need to be very flexible, the best way for me is to go to this page :
admin/content/node-type/[ContentType]/display/basic
And set all your additional fields to "hidden".
Then, the $content variable will contain only the body field.
The drawback is that you need to display all the additional fields one by one. CCK provide an easy way to do that by providing a fully rendered variable for each field
print $field_FIELDNAME_rendered;
Using that, you can really customize easily the output for a content type.
Related
I feel like there is a really simple solution to this problem. However, after trying to solve it for about 3 hours unsuccessfully, I humbly come to you.
The Basics:
A custom post type, " band ", has been created, and has several
custom fields (which were created through the Advanced Custom
Fields
plugin.)
The Question:
How would I get and display the contents (specifically custom field data) of a specific band entry (using its ID or title or slug) inside of a regular post? (see diagram below)
(source: thisnewband.com)
.
Methods to Trigger the Display of the Custom Post Type:
We have to provide the ID/title/slug so that it knows what band post's content to display
Shortcode (such as [band id="21"] ) (added inside post content)
Custom Field (custom field name band-id where you can input the ID of the band)
What I've Tried and Why It Didn't Work:
Shortcode
`[band id="21"] inserted in post editor field
Used WP_Query to query post with type=band and ID="21".
Code located in functions.php
Result: It would echo static text but would not display any post-specific content (Band Name, etc.). Also would not pull post-specific custom field data.
(also tried query_post with no luck)
Custom Field
Entered ID (21) into custom field on post editor page.
Coded it directly into the post template:
Used WP_Query and had the ID in the array pull from the custom field.
Result: Nothing good happened.
Where I Keep Running Into Trouble:
It's hard to pull the custom field data from the custom post type while inside an actual post
The Optimal Solution:
Whether it's by using a shortcode, custom field, or even a new widget, it would be easiest if one could:
Create a PHP template with the code for just how the single Band content is supposed to display. (Including the loop). Example name: band-block.php
Use get_template_part('band-block'); to echo this code (either in Post Template or Shortcode via functions.php)
Thanks for your help! Let me know if you'd like to see any of my code.
I knew you have found a solution for your problem, but for others i will give them an other solution:
You can query by ID, no problem. Look at these: http://www.advancedcustomfields.com/resources/field-types/relationship/
But you have to check in the custom field "return format" the box "Post IDs". Then it works perfectly well.
Sorry for my bad english ;)
Cheers
With some amazing help from Hobo, I was able to come up with a solution!
The main problem was with trying to query by 'ID'. No matter what we tried, it just never worked well. (It could be the way that the Advanced Custom Fields stored the ID field contents.)
What did work:
Created a custom field for the post page in which to put the Band (custom post type) post name/slug into. (Custom field was named post-band-name and created with the Advanced Custom Fields plugin.)
Placed the query code in the Post Template. (See code below)
Done.
The Solution Code
Add this loop after the normal loop...
<?php /* Display all the author's posts from the custom post type ('band') */ ?>
<?php
$authorid = get_the_author_meta( ID, $userID );
$args4=array('author'=>$authorid,'post_type'=>'band', 'numberposts'=> -1);
$cquery4=new WP_Query($args4);
if($cquery4->have_posts()):
while($cquery4->have_posts()):
$cquery4->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<p><?php the_title();?></p>
</a>
<?
endwhile;
wp_reset_postdata();
endif;
?>
I need to create meta tags for the seo of my website.
I am using WHMCS and a custom template
Any page is compiled through the header.tpl, footer.php page1.tpl and page1.php pages
What I tried was to add
">
to header.tpl which does add the line of code to the "output source"
and I added
$pgDesc = 'Any custom description';
to the page1.php
Somehow the echo $pgDesc does not call the value correct
Any ideas ?
Just add your metatags to header.tpl. And that's all. You can get your title/desctiption/keywords strings from various sources: whmcs variables, translation strings, and even some db.
See http://docs.whmcs.com/Seo
i want to get the custom profile fields which i have made in user profile part,
i want to get that values and show on one simple page i have tried this one....
<?php print $profile['Personal Information']['profile_fname']['#value']; ?>
but its not working....
i want to show all the fields on that page....
thanks in advance,
Nitz
UPDATE:
i got the answer....
// load profile fields
profile_load_profile($account);
//print values -
//note the variable names after $account-> are the names given when you created the custom fields for the user profiles
<?php echo $account->profile_first_name;?>
<?php echo $account->profile_last_name;?>
I have a list of Staff Members in Drupal6.
I need to style a node such that the email field of a staff member displays as "Contact [First word of Full Name field]". Clicking it causes a mailto:// link to open. BTW, I know that's not a recommended procedure because a contact form or a captcha would be more effective, but my client desires it.
Yes, I'm using the CCK module and the CCK Email module too.
So, again, I have a list of staff members using a custom content type. I have an email field in there using CCK Email module. When I display the node of a staff member, it's just showing the email address. My client wants to make it say "Contact Jonathan" if the staff member is named "Jonathan McDaniels", and so on with each node of each staff member. When "Contact Jonathan" or "Contact Sara" is clicked, it should do the ordinary mailto:// hyperlink stuff.
Yet another way to do this is with PHP. You can create a file node.tpl.php in your theme folder, copying this over from the garland theme. At the top of it, however, add this call:
require_once('node_hooks.php');
Now create a file node_hooks.php in your themes folder. This gives you enormous power now over a given node. You should start to learn the $node variable by doing this in your node_hooks.php file:
<?php
print_r($node);
Refresh your node page and then do a View Source in your browser on it. This will show you the object and each array element inside $node.
In my case I had a node of type 'staff' because that's what I called it when creating it. I also had a special CCK field called CCK Email and used it to create a field named field_staff_email. This was storing a value like jonathan#test.com. So, because of this, I could add this into my node_hooks.php file to do the search and replace on the content so that I get "Contact Jonathan" instead of the email address:
<?php
if ($node->type == 'staff') {
adjustStaffContactField($node, $content);
}
function adjustStaffContactField(&$node,&$content) {
$asWords = explode(' ',$node->title);
$sContact = htmlentities(strip_tags($asWords[0]));
$sContact = trim($sContact);
$sContact = "Contact $sContact";
$sLink = $node->field_staff_email[0]['email'];
$sContact = "<a href='mailto:$sLink'>$sContact</a>";
$sLookingFor = "$sLink";
$content = str_replace($sLookingFor, $sContact, $content);
}
To resolve this problem purely inside of Drupal without your own PHP code, you will need these modules in Drupal 6:
CCK Module
CCK Link Module
CCK Token Module
Unfortunately the CCK Email Module won't solve this problem.
Once copied into sites/all/modules and activated, you can then take your custom content type for Staff Member and create a field Staff Email as a type of "Link". Then, set the Title of that link to Static Title and set the text to:
Contact [title]
In the Default Value, set Staff Email URL to:
mailto:test#test.com
And set the Help Text to explain the default format requirement. Note if they enter this wrong without the mailto:, then the link will end up being passed off as a node, which is incorrect. Perhaps someone can think of a hook to test for this on this field in the PHP (or Javascript on form load) and swap it on the fly if forgotten.
Now, for only using the first name instead of the full name, you have a couple choices. You can use field_staff_firstname and field_staff_lastname (custom text fields you create) and not use the node's title (aka [title] token). Another route is to hook the page in PHP or with Javascript on form load, looking for this node element, intercepting it, and showing only Contact + first name.
I had the same problem of trying to turn a cck field into a mailto link. I found this posting but have found another, and fairly simple way, of doing it. It is done by overriding the cck theme template: content-field.tpl.php. This is the template which outputs the field values, so you can edit this to update the value you want. Copy the template into your own theme folder (don't overwrite the original), I then replaced the line:
<?php print $item['view'] ?>
(This was line 42 in my template), with:
<?php
// new lines for email field to turn it into a mailto link
?>
<?php if ($field_name=='field_email') : ?>
<?php // print_r($item);?>
<?php print "<a href='mailto:" . $item['value'] . "'>". $item['value'] ."</a>"; ?>
<?php else : ?>
<?php
// original line
?>
<?php print $item['view'] ?>
<?php endif; ?>
<?php
// end edit
?>
In this example, my field was called "field_email"
Hope this helps.
Ben
I am theming a custom tpl.php file to style my forms.
I am printing my fields with something like this:
print drupal_render($form['field_sr_minutes'])
The problem is that on one element I need to get in this form is a taxonomy field, which is what i'm having a problem with. It will display with drupal_render($form), but I want to put it in a specific place, similarly to how I'm doing it with the normal node fields.
However, what I'm trying won't work:
Shows in HTML source as name="taxonomy[6]"
So, I'm trying things like
print drupal_render($form['taxonomy[6]'])
print drupal_render($form['taxonomy_6'])
print drupal_render($form['name_of_taxonomy'])
But, no go...
Any Ideas???
Taxonomy fields are all present under the name taxonomy in the node form. So you can just print that.