Echo the_post_thumbnail in wordpress - wordpress

I have been trying to echo the post thumbnail for worpress using the string and the function but it doesnt work.
Here is the template i am trying to show it.
if ($favorite_post_ids):
$favorite_post_ids = array_reverse($favorite_post_ids);
foreach ($favorite_post_ids as $post_id) {
$p = get_post($post_id);
echo "<li>";
echo "<a href='".get_permalink($post_id)."' title='". $p->post_title ."'>" . $p->post_title . "</a> ";
echo '<img src="'.get_the_post_thumbnail(array(55,55)).'" />';
wpfp_remove_favorite_link($post_id);
echo "</li>";

First parameter of get_the_post_thumbnail() is the post ID, not the thumbnail size

Related

How do I add alt tags to images in the Wordpress theme file?

I have the following code that inserts an image spinner while a woocommerce product information loads. However, it doesn't pick up the alt tags for the images. Does anyone know how to grab that information from the media file?
// Insert the spinner HTML
add_action( 'woocommerce_single_product_summary', 'woocommerce_websiteexample_product_loader', 60 );
if ( ! function_exists( 'woocommerce_websiteexample_product_loader' ) ) {
function woocommerce_websiteexample_product_loader() {
$base_url = "/wp-content/themes/websiteexample/assets/images/";
$image_list = array(
"image1.png",
"image2.png",
"image3.png",
"image4.png"
);
// wc_get_template( 'single-product/share.php' );
echo "<div class='deal-loader-container'>";
echo "<div class='table full'>";
echo "<div class='table-cell vertical-align'>";
echo "<div class='relative'>";
echo "<h2>Please wait...</h2>";
echo "<div class='deal-loader'></div>";
foreach ($image_list as $image)
{
echo "<img src='" . $base_url . $image . "'>";
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}

Same link returns everytime by using multiple excerpts

On the main page in Wordpress I use multiple excerpts of different pages. The problem is though that it returns everytime the same excerpt link. It seems like the last link (of 3) on the page is used everytime.
The code (of course I changed the number of the id everytime ;-) :
<?php
$post_id = 35; // post id
$queried_post = get_post($post_id);
$my_excerpt = get_excerpt_by_id_long($queried_post); //$post_id is the post id of the desired post
echo '<a href="' . get_permalink( $queried_post ) . '" title="' . $queried_post->post_title . '">';
echo '<h3><strong>';
echo $queried_post->post_title;
echo '</strong></h3>';
echo '</a>';
echo $my_excerpt;
?>
This is in functions.php
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
$the_excerpt = '<p>' . $the_excerpt . '<a class="leesmeer" href="'.get_permalink($post_id).'">lees verder...</a></p>';
return $the_excerpt;
}
never mind, I found the solution: forgot to use the $post_id in the .get_permalink()...Edited the code in the above functions.php

checking custom fields like images and ignore them if they are empty

I have five custom fields for loading images but all of them are not required. I mean the user can upload a random number of images from 1 to 5. I am stuck in a simple lack of concept here. What I am missing? Here is my code
$img = array();
$post = get_page_by_title( $pgnr,OBJECT,'post');
$id= $post->ID;
$custom_fields = get_post_custom($id);
$images = Array("image1","image2","image3","image4","image5");
foreach($images as $image){
if(isset($custom_fields[$image]) && (!empty($custom_fields[$image]))) {
$img[] = $custom_fields[$image];
}
}
echo '<div id="showcase" class="showcase">';
foreach ( $img as $value )
{
echo '<div class="showcase-slide">';
echo '<div class="showcase-content">';
echo'<img alt="image" src="'. wp_get_attachment_url( $value ).'" width="600" height="500"/>';
echo '</div>';
echo '<div class="showcase-thumbnail">';
echo '<img alt="thumb" src="'.wp_get_attachment_url( $value ).'" width="140" /> ';
echo '</div>';
}
echo '</div>';
}
Perhaps the array $img[] needs an index between the brackets that only increments each time values are assigned into the array.

Add a pages featured image to a foreach loop

I've got a foreach loop that is displaying a list of subpages Title, Content and URL. I can't seem to display the featured image though! Here's what I have:
<?php
$pageChildren = get_pages('sort_column=menu_order&hierarchical=0&child_of='.$post->ID);
if ( $pageChildren ) {
foreach ( $pageChildren as $pageChild ) {
echo '<div style="position:relative;float:left;width:100%;margin:0 0 20px 0;border-bottom:1px dashed #cdcdcd;padding:0 0 20px 0">';
//FEATURED IMAGE HERE
echo '<a class="newstitle" href="' . get_permalink($pageChild->ID) . '">' . $pageChild->post_title.'</a><br /><br />';
if (!empty($pageChild->post_content)){
echo '<p>'.$pageChild->post_content.'</p> ';
echo '<a class="readmore" href="' . get_permalink($pageChild->ID) . '" style="float:left">Read More »</a>';
}
echo '</div>';
}
}
?>
you must use
<?php echo get_the_post_thumbnail($pageChild->ID ); ?>
to get the post thumbnail as well page

Wordpress - output custom field as ul list

I have a custom field who's content I would like to output as a ul list.
The custom field contains words that are separated with spaces.
I'm trying to use this code here but it's not working.
<?php
$list_items = get_post_meta($post->ID, 'idid');
if($list_items){
$list_items = explode(" ", $list_items) {
echo '<ul>';
foreach($list_items as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
}
?>
1- add ; before explode function, and remove accolades.
2- declare a second variable different than $list_items where to
put result of explode.
3- second parameter of get_post_meta() should be the slug of your custom field (in your case is it idid?), add also true parameter.
Your code will look like:
<?php
$list_items = get_post_meta($post->ID, 'idid', true);
if($list_items){
$list_items2 = explode(" ", $list_items);
echo '<ul>';
foreach($list_items2 as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
?>

Resources