Drupal - Display content of node - php

I've a problem with the content of one node.
There's a loop to display the excerpt of last news on a page.
the content of the loop is :
<h2 class="title"><?php print $title; ?></h2>
<?php if ($node->field_news_thumb[0] && $node->field_news_thumb[0]['filepath']): ?>
<img src="/<?php print $node->field_news_thumb[0]['filepath']; ?>" width="200" >
<?php endif; ?>
<?php print_r($node->content['body']['#value']); ?>
But there is just one news for which the title and thumb are well displayed, but the content is empty.
In the backoffice I've just one paragraph in content.
I found that if I add a new paragraph, the first one will be displayed on the front.
When there's just one paragraph, if I click on the title to get fullpage node. The content is well displayed with exactly the same method :
<?php print_r($node->content['body']['#value']); ?>
How can I display the full content of these nodes ?

Ok I find the solution.
I have replace
print_r($node->content['body']['#value']);
By :
$news = node_load(array('nid' => $node->nid));
print_r($news->body);
This give me the full content. I just have to shorten as I want.

Related

the_category pulling div out of normal flow

trying to use <?php the_category( ' ' ); ?> to put in my category from my wordpress post.
within the loop html
<a href='<?php the_permalink(); ?>'>
<section id="post-<?php the_ID(); ?>" class="..">
<div>
<?php the_category( ' ' ); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
</div>
</section>
</a>
Problem is that having the category php the section div's seems to get pulled out of the link (output has section outside link code.
Not having the category code it works perfectly.
You have an issue in your HTML: you have a <a> tag (inline element) with inside block type elements (like <div> and <section>).
Take a look at this page in order to properly understand the difference between inline and block elements.
While using the_category(), you are going to displays a link to the category or categories a post belongs to, so you are also placing a <a> tag inside another <a> tag.
Because you want just display the categories' names, you can use the following code
foreach((get_the_category()) as $category){
echo $category->name."<br>";
}
Review your formatting and everything will be work as expected.

Show exactly full content in WordPress without escape tags

I want to show limit latest post of WordPress in main page. I'm using WordPress APIs and functions to show that, but after that get_the_content() remove and escape all of seved tags and my posts don't show correctly such as editor tags.
This function escape HTML tags:
<ul style="list-style: none;margin:5px" class="latest-posts">
<!-- LOOP START -->
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- THIS DISPLAYS THE POST THUMBNAIL, The array allows the image to has a custom size but is always kept proportional -->
<li> <?php the_post_thumbnail( array(100,100) );?></li>
<!-- THIS DISPLAYS THE POST TITLE AS A LINK TO THE MAIN POST -->
<li>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a>
</li>
<!-- THIS DISPLAYS THE EXCERPT OF THE POST -->
<li>
<?php echo mb_substr(get_the_content(), 0, 1200).' ...'; ?>
</li>
<!-- READ MORE LINK -->
<li>more ...</li>
<?php endwhile;?>
<!-- LOOP FINNISH -->
</ul>
There are basically three possibilities how to output the excerpt of the post content.
with the excerpt() function, to output the content of the excerpt field in post editor, if it's not filled it will display the first 55 words of the post content ( use excerpt_length filter to control the length of the excerpt ).
to use the <!--more--> quicktag, the_content() tag will only show the excerpt up to the <!--more--> quicktag. That quicktag will not work in single pages templates.
to manually strip HTML from post content and to show custom excerpt. One way to do that is to use wp_filter_nohtml_kses() function:
echo mb_substr( wp_filter_nohtml_kses( get_the_content() ), 0, 1200 );
All methods above will strip the HTML from post content. To include HTML in excerpts use excerpt field in post editor, WordPress will preserve HTML you included in post editor. There are also some plugins like Advanced Excerpt that will create excerpt from content and preserve html, but I don't believe that there is a bullet proof way of doing that.

Display an image alt or title below the image and in h1 html tag

I've got an image carousel in a site. I want to display the image alt tag or title below the image when I do mover over. At the same time I want to display that same name in another h1 tag.
The code is in PHP and Magento implementation. I know it is a simple trick. But I tried all the codes using jQuery after, append and replacewith functions. These functions are working. But I couldn't display the alt or title name.
Php Code:
<ul id="mycarousel" class="jcarousel-skin-tango alt">
<?php
// products
if(!isset($CatID))
$CatID = 10; // Set to classic collection if empty;
$_category = Mage::getModel('catalog/category')->load($CatID);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('status', 1)//enabled
->addCategoryFilter($_category);
foreach($products as $index=>$prod):
$product = Mage::getModel('catalog/product')->load($prod->getId());
echo '<li style="border: none;">
<a class="thumbActImg1" href="'.$product->getProductUrl().'" data-rel="'.(string)Mage::helper('catalog/image')->init($product, 'small_image')->keepFrame(FALSE)->resize($_resize, $_resize).'" alt="'. $product->getName() .'">
<div class="thumbActImg2">
<img src="'.$product->getThumbnailUrl(80, 80).'" alt="'. $product->getName() .'" />
</div>
</a>
</li>';
endforeach;
?>
</ul>
jQuery code:
jQuery(this,'img.thumbActImg2').after('<div class="pname"><?php echo $product->getName()?></div>');
jQuery('#pro_name h1').replaceWith('<h1><?php echo $product->getName()?></h1>');
Right now, I used php echo in order to display name. But it displaying the current page product name instead of mover over image name.
Could anyone help me out please.
Hi,
the PHP-Code is evaluated once, when the site is generated. So the <h1><?php echo $product->getName()?></h1> is always the same text.
You have to rewrite/get the current information from the image, when it's cycled (if I understand you). So here is what may work for you:
//insert the code, where the carousel is triggered
//get the alt attribute from image and replace h1 content
jQuery('#pro_name h1').html(jQuery('img.thumbActImg2').attr('alt'));
I hope this is what you're looking for, the problem isn't descripted very briefly.
Greetings Mat

What is the php code I need to add to this file in order to get an image to appear in this custom post type?

I am using a wordpress theme that has a custom post type called "link" which simply produces a post where the title links to an outside link you specify. It also includes some text in the body of the post. I would like to also be able to display an image. Right now it is not showing the image.
Here is the code:
elseif ( has_post_format( 'link' )) {
if(get_post_meta($post->ID, '_format_link_url', true)!=''){
$link = get_post_meta($post->ID, '_format_link_url', true);
}else{
$link = '';
}
?>
<h2 class="entry-title">
<a href="<?php echo $link; ?>" target="_blank" title="Follow this link">
<?php the_title(); ?>
</a>
</h2>
<p>
Is there anything I can add to make it display an image as well as text?
Thanks!
I can't see anywhere on this example where you are using an tag to insert an image. The img tag has two required attributes, src and alt. Src is the path to your image, whereas alt is the alternate text for the image. So:
<img src="images/myImage.png" alt="This Is My Image"/>
Hope that helps!

Different <li> class based off of category or tag in Wordpress

My goal is to have action icons in lists that correspond to how the list item is tagged or categorized.
For example, if I have a list item that is a webinar it should have a webinar icon next to it, the next item in the list might be a white paper, which should have a white paper icon next to it. The population of the icons should be controlled by how the list item is tagged or categorized.
I don't know where to start; any suggestions are appreciated. Thanks!
EDIT:
Thought it might be helpful if I show the list I'm wanting to modify - technically, the items that I want to modify are in the span class=meta" section, but I'm open to using whatever method worls:
<ul class="sub_nav">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active last">
<a href="#resource_center" title="Resources II">
Featured Resources
</a>
</li>
</ul>
<div id="resource_center">
<?php
$resources = get_posts("post_type=resource&posts_per_page=2&cat=31&tag=featured");
?>
<?php foreach ($resources as $key => $resource): setup_postdata($resource); ?>
<?php if ($key === 0): ?>
<?php endif ?>
<p><span class="meta"><?php echo apply_filters('get_the_date',mysql2date('m.d.Y', $resource->post_date), 'm.d.Y') ?></span>
<?php echo $resource->post_title ?> – <?php echo strip_tags(get_the_excerpt()) ?></p>
<?php endforeach; ?>
<span class="more">Read More</span>
</div>
Just name all your icons after the tags they correspond to and put them in the same folder on your server (let's say http://www.yoursite.com/tagicons)
In your loop, just iterate the meta tag inside an image tag
<img src="http://www.yoursite.com/tagicons/{$tag}.png" />
Paste the code you're using to iterate your list items if you need more help.
Cheers
-D
EDIT:
I se you're using wordpress.
Refer to http://codex.wordpress.org/Function_Reference/wp_get_post_tags
to see how to get the tags you're looking for.
If you are generating the list inside the WordPress loop you can add the category as a class to the list element. For example:
...loop started
$categories = get_the_category();
$ids = '';
foreach ($categories as $category) {
$ids .= ' ' . $category->slug;
}
echo '<li class="' . $ids '">This item</li>';
...more loop
Then utilize CSS to style the list block.
While I think both of these solutions would have worked, I decided to go with a third solution I discovered as I researched options to meet my use case. This one was ideal because I was able to seamlessly fit it into my existing code structure and because I have a relatively low number of resources that I need to add the featured image to.
I added the code below, which basically uses the post's featured image as a left-aligned thumbnail.
<?php if ( has_post_thumbnail()): ?>
<?php
$thumb_id = get_post_thumbnail_id($resource->id);
$args = array(
'p' => $thumb_id,
'post_type' => 'attachment'
);
$thumb_image = get_posts($args);
$thumb_caption = $thumb_image->post_excerpt;
?>
<?php if (!empty($thumb_caption)): ?>
<div class="caption"><?php echo $thumb_caption ?></div>
<?php endif ?>
<?php the_post_thumbnail('sidebar-thumb'); ?>
<?php endif; ?>
Followed by this code snipped to grab the image and put it by the list item:
<?php echo get_the_post_thumbnail($id, 'thumbnail', array()); ?>
Here's a screen shot of my test site list section after I added the code - it's exactly what I was looking for:
Thanks for the suggestions and help, it got me moving in the right direction!

Categories