Add a pages featured image to a foreach loop - wordpress

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

Related

Different Styling for Last Thumbnail in Advance Custom Fields Pro Gallery

I've created an image gallery using Advance Custom Fields pro with lighbox and limited it to show only 4 thumbnails, and that's working perfectly. When someone click on any thumbnail, the gallery opens up in lightbox and shows all the images in it.
Now I want to use different css styling for the 4th thumbnail but don't know how to do that.
Here's my code:
<?php
$images = get_field('menu_gallery');
$image_1 = $images[0];
if( $images ) { ?>
<ul class="gal-grid">
<?php
$i = 0;
foreach( $images as $image ) {
if ($i >= 5) {
$content = '<li class="gal-grid-zom">';
$content = '<a class="gallery_image" href="'. $image['url'] .'"></a>';
$content .= '</li>';
} else {
$content = '<li class="gal-grid">';
$content = '<li class="gal-grid"><a class="gallery_image" href="'. $image['url'] .'">';
$content .= '<img class="gallery-zom" src="'. $image['sizes']['thumbnail'] .'" alt="'. $image['alt'] .'" />';
$content .= '</a>';
$i++;
}
$content .= '</li>';
if ( function_exists('slb_activate') ) {
$content = slb_activate($content);
}
echo $content;
?>
<?php } ?>
<?php } ?> </ul>
Can someone help me regarding this issue?
You can add the $key inside your foreach loop :
foreach( $images as $key => $image ) {
if( $key === 3 ) {
// it will show the specific <li> for the 4th iteration.
<li class="fourth-grid">The Image</li>
} else {
<li class="gal-grid">The image</li>
}
}

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>";
}
}

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.

Echo the_post_thumbnail in 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

Styling category names in wordpress

I am trying to style category names with different colours.
Here is my PHP:
<?php
foreach((get_the_category()) as $category) {
if (is_category('5') ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';}
else {
echo '' . $category->cat_name . ' ';}
}
?>
It is not returning errors, but it is not working either...
What am I doing wrong?
Here is the page I'm working on:
http://216.172.178.12/~saracimi/eng/
and the snippet of code is relative to the rectangular boxes at the center of the page.
Thanks a bunch!
is_category will return true when you are on that category archive. you need to check the category id so try:
foreach((get_the_category()) as $category) {
if ($category->cat_ID == 5 ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';
} else {
echo '' . $category->cat_name . ' ';
}
}

Resources