Using the below code I have successfully listed WordPress page titles. the pages have a Featured Image. when I run the below code the image shows up as the Image Unavailable one instead of the actual correct image.
Have I missed something out? The Title displays correctly.
PHP
<?php query_posts(array('showposts' => 30, 'post_parent' => $post->ID, 'post_type' => 'page'));
while (have_posts()) { the_post();
if(has_post_thumbnail()) { ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('medium');?>
</div>
<?php } else { ?>
<div class="entry-thumbnail">
<img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
</div>
<?php } ?>
<?php the_title();
}
wp_reset_query(); // Restore global post data
}?>
has_post_thumbnail() sometimes fails as codex says here http://codex.wordpress.org/Function_Reference/has_post_thumbnail
can you try the following
if ( '' != get_the_post_thumbnail() ) { ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('medium');?>
</div>
<?php } else { ?>
<div class="entry-thumbnail">
<img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
</div>
<?php } ?>
<?php $this_page_id=$wp_query->post->ID; ?>
<?php query_posts(array('showposts' => 20, 'post_parent' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($this_page_id, 'thumbnail') ); ?>
<img src="<?php echo $url ?>" />
<h2><?php the_title(); ?></h2>
<?php } ?>
Related
I am trying to setup my blog homepage to show the featured image and the title, so far everything is coming out fine except for some reason my code to add in the image is having issues even trying to follow what many other posts here have seemed to answer. Here is my code:
<div class="blogContainer">
<?php
while(have_posts()) {
the_post();?>
<?php $thumb = get_the_post_thumbnail_url(); ?>
<div class="blogItems" style="background-image: url('<?php echo $thumb;?>')">
<h4><?php the_title();?></h4>
</div>
<?php } ?>
I've tried a couple small variations such as this:
<?php
while(have_posts()) {
the_post();?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div class="blogItems" style="background-image: url('<?php echo $url; ?>')">
<h4><?php the_title();?></h4>
</div>
<?php } ?>
And this
<?php
while(have_posts()) {
the_post();?>
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="blogItems" style="background-image: url('<?php echo $backgroundImg[0]; ?>');">
<h4><?php the_title();?></h4>
</div>
<?php } ?>
And all come out with the same issue of having the url just come up empty in the inspect tab. If someone has an answer it'd be much appreciated!
Please try below code:
<?php
$args = array(
'post_type' => 'post', //change with your post type
'posts_per_page' => -1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$thumb = get_the_post_thumbnail_url(); ?>
<div class="blogItems" style="background-image: url('<?php echo $thumb;?>');">
<h4><?php the_title();?></h4>
</div>
<?php endwhile; endif; ?>
Try adding get_the_ID() to get_the_post_thumbnail_url() in the first example:
<div class="blogContainer">
<?php
while(have_posts()) {
the_post();?>
<?php $thumb = get_the_post_thumbnail_url(get_the_ID()); ?>
<div class="blogItems" style="background-image: url('<?php echo $thumb;?>')">
<h4><?php the_title();?></h4>
</div>
<?php } ?>
So this is my code it shows all the titles of the child pages but i also want all the feutured images of the child pages. And i have no clue how to accomplish this. I have incldue page.php and my function.php script. I know alot of people allready ask this type of quistion but i can't seem to figure it out.
Page.php
<?php
get_header();
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post page">
<?php
if ( has_children() OR $post->post_parent > 0 ) { ?>
<nav class="site-nav children-links clearfix">
<span class="parent-link"><?php echo get_the_title(get_top_ancestor_id()); ?> </span>
<ul>
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php wp_list_pages($args); ?>
</ul>
</nav>
<?php } ?>
</article>
<?php endwhile;
else :
echo '<p> No content found</p>';
endif;
get_footer();
?>
Function.php
function get_top_ancestor_id() {
global $post;
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post->ID;
}
// Does page have children?
function has_children() {
global $post;
$pages = get_pages('child_of=' . $post->ID);
return count($pages);
}
So i found the answer finally for people that also strungle:
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php $our_pages = get_pages($args); ?>
<?php if (!empty($our_pages)): ?>
<?php foreach ($our_pages as $key => $page_item): ?>
<div class="col-*-* product-object">
<a class="product-article" href="<?php echo esc_url(get_permalink($page_item->ID)); ?>">
<div class="product-image" style="background: url(<?php echo get_the_post_thumbnail_url($page_item->ID,'product-image');?>); ">
<h2 class="product-h2"><?php echo $page_item->post_title ; ?></h2>
</div>
</a>
</div>
<?php endforeach ?>
<?php endif ?>
</article>
</div>
<?php
The image is set to return a URL in the Custom Fields Plugin however it is just coming up with img src Unknown, everything else works on the page except this... code is as follows: Thanks in advance for any help!
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'artists',
);
$query = new WP_Query( $args );
?>
<section class="row artists">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-3">
<a href="<?php if( get_post_field('artist_website') ) { ?>
<?php echo $artist_website; ?>
<?php } else { the_permalink(); } ?>">
<img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
<p><?php echo the_title() ;?></p>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
The problem is that get_post_field('artist_feature_image') only return the image.
You need to display it with "echo"
<img src="<?php echo get_post_field('artist_feature_image'); ?>" />
And don't do echo the_title() ;, because the_title() is already doing an echo on get_the_title();
function the_title() {
echo get_the_title();
}
So if you want to display it you just have to do: the_title();
Here's the page template code I'm working with, defined into sections...
<?php
//* Template Name: Partnerships Archive
?>
<!-- Header =========================================== -->
<?php get_header(); ?>
<!-- Homepage Header Video =========================================== -->
<div class="subpage-video-sca">
<!-- VIDEO -->
<div class="subpage-desktop-vid-sca">
<video title="<?php the_field('seo_video_title'); ?>" autoplay loop muted playsinline>
<source src="<?php the_field('vimeo_link'); ?>" type="video/mp4">
</video>
</div>
<!-- GRAD-OVERLAY -->
<div class="subpage-overlay-image-sca">
</div>
<!-- OVERLAID TEXT -->
<div class="subpage-text-over-video-sca">
<div>
<h1><?php the_field('sub_page_title'); ?></h1>
<p><?php the_field('paragraph'); ?></p>
</div>
</div>
</div>
<!-- Breadcrumbs =========================================== -->
<?php echo do_shortcode("[breadcrumbs]"); ?>
<!-- Main Content =========================================== -->
<div class="wrap">
<?php
$args = array(
'post_type' => 'partnerships',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a class="one-third partnership-block" href="<?php the_permalink(); ?>">
<img src="<?php the_field('logo'); ?>" alt="<?php the_title();?> graphic">
</a>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
</div><div class="row">
<?php endif; ?>
<?php endwhile; endif; ?>
</div>
<!-- Testimonials =========================================== -->
<div class="wrap">
<div class="page-section-headers pt-testimonials-container txt-right">
<h1>WHAT <span class="red">OUR PARTNERS</span><br>HAVE TO SAY</h1>
</div>
<?php if( have_rows('testimonial') ): ?>
<div class="testimonial-slider-container slick-slider">
<?php while( have_rows('testimonial') ): the_row();
// vars
$text = get_sub_field('testimonial_text');
$client = get_sub_field('client_name');
$company = get_sub_field('client_company');
?>
<div class="testimonial-slider-single">
<p><?php echo $text; ?></p>
<h2><?php echo $client; ?></h2>
<h3><?php echo $company; ?><h3>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
The code in the main content area is pulling through custom post types I have.
The code in the Testimonials area is pulling through data from an ACF Repeater field for this page specifically.
They both work independently but when I have them both on the page at the same time, the testimonials doesn't pull through.
That said, Ive just noticed that when I put the testimonials first and the main content after, they both work!!
Can anyone help? Is there something I haven't closed properly in the Main content or something? I don't get it...
It's wordpress, using Genesis Framework, latest versions of both. The page is here: http://staging.seedcreativeacademy.co.uk/partnerships/
add wp_reset_postdata(),after while :
<div class="wrap">
<?php
$args = array(
'post_type' => 'partnerships',
'orderby' => 'title',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a class="one-third partnership-block" href="<?php the_permalink(); ?>">
<img src="<?php the_field('logo'); ?>" alt="<?php the_title();?> graphic">
</a>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>
</div><div class="row">
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
got another question to ask:
I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.
Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):
<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="gallery_image_container">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
What am I doing wrong here?
This code works for me. Yours is virtually identical.
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />
<?php endif; ?>
Note: I also cut and pasted your code and tested on my WP install, and it worked fine.
Or... try this alternative to the if condition:
<?php if ( get_the_post_thumbnail( get_the_ID() ) ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>