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" />'
; } ?>
Related
I'm not quite sure where I've gone wrong here. One of my template parts that I'm using for listing a bunch of posts of a certain category doesn't seem to be working.
Here is the code where I'm trying to get a template part named "content-offer.php" to display these posts, but it will not show any.
<?php query_posts('cat=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "4") { break; }
else { ?>
<?php get_template_part( 'content', 'offer', get_post_format() ); ?>
<?php $count++; } ?>
<?php endwhile;
endif;
wp_reset_query(); ?>
Here's what's actually within the template part file:
<div class="offer-box">
<div class="offer-left">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" height="100%" height="auto" />';
}
?>
</div>
<div class="offer-right">
<h2 class="offer-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<p class="offer-text">
<a href="<?php the_permalink(); ?>">
<?php the_excerpt(); ?>
</a>
</p>
<a href="<?php the_permalink(); ?>">
<div class="offer-cta">Claim now!</div>
</a>
</div>
Take note that all my files are in the root folder of the wordpress theme currently, so it's surely nothing to do with sub directories.
I have another area that displays posts with the same method just fine, so I'm confused as to why this one doesn't work.
If anyone can help me out and point where I've gone wrong it would be greatly appreciated.
So, it looks like I had the "content-offer.php" file in the root wordpress folder, not the theme root folder. Oops.
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();
I've created a plugin that sets up a custom post type ('Programmes'), taxonomy and a short code to display the custom posts. I'm trying to display a custom post meta field (Broadcast Date) via the shortcode. These are the methods I've tried so far:
This is the method I'd have preferred as it allows me to use it for multiple meta values:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeMeta = get_post_meta($post->ID,'_meta_content',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeMeta['broadcast-date'] ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
But that didn't work so I tried this instead:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeBroadcastDate = get_post_meta($post->ID,'broadcast-date',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeBroadcastDate ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
and that doesn't work either!
Any ideas? Thanks
I'm making an online magazine. Right now I'm trying to show the posts I'm posting. I've managed to do that, but what I want is that if a post is categorized in "Streetstyle", the loop will be completely different then if it's categorized in "Photography". I've mangaged to do this with one category, how can I do the same with other categories, and style it another way? I tried using the same code as <?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>, but then the theme just gets bugged and the posts shows up twice. Do you know of a better way to do this?
This is my code:
<?php query_posts('posts_per_page=9' . '&orderby=date');
while ( have_posts() ) : the_post(); ?>
<?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1>Fashion</h1>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php } else { ?>
<div <?php post_class('pin'); ?>>
<h1>
<?php the_title(); ?>
</h1>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content('Les mer <br><br>'); ?>
</div>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>
If you have a lot of categories I would recommend using switch, it will save you time and your code will look a lot cleaner.
http://php.net/manual/en/control-structures.switch.php
I've slimmed this down a bit, but this is probably more what you're after
<?php query_posts('posts_per_page=9' . '&orderby=date');
while(have_posts()) : the_post(); ?>
<?php if(is_category('Streetstyle') || in_category('Streetstyle')) : ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php if(has_post_thumbnail()) the_post_thumbnail(); ?>
<?php else : ?>
<div <?php post_class('pin'); ?>>
<h1><?php the_title(); ?></h1>
<?php
if(has_post_thumbnail()) the_post_thumbnail();
the_content('Les mer <br><br>');
?>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
How can I can I check if a post has a thumbnail and if does do something? If doesn't do something else. This is what I have:
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<?php the_title(); ?>
<?php
}else{
?>
<?php the_post_thumbnail(); ?>
<?php
}
?>
<?php endwhile; ?>
<?php endif; ?>
Any help will be appreciate it.
You already have this, in the line
if ( has_post_thumbnail() )
you are checking if the post has thumbnail, the problems is that you put wrong code in else statement, you have to put something like:
<?php if ( has_post_thumbnail() ) { ?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
HAVE THUMBNAIL DO SOMETHING
<?php
}else{
?>
DOESN'T HAVE THUMBNAIL : DO SOMETHING ELSE
<?php
}
?>
Try with these line of codes:
<?php if(has_post_thumbnail())
{
?>
<img src="<?php the_post_thumbnail_url(); ?>" id="contextual" class="contextual" alt="" />
<?php
}
else{
?>
<img src="<?php echo get_template_directory_uri(); ?>/design/images/i-default.jpg" id="contextual" class="contextual" alt="" />
<?php } ?>
To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme’s template files:
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
firstly CHECK your functions.php file for this
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
if its not in there, copy and paste that into your file..
Secondly Add this to your functions.php
this lets you return the Image src, and not just print the entire img tag
function get_the_post_thumbnail_url( $post_id = NULL ) {
global $id;
$post_id = ( NULL === $post_id ) ? $id : $post_id;
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
$src = $src[0];
return $src;
}
Then on your template page change your code to something like:
this was used as a background image
<?php if ( has_post_thumbnail() ) { ?>
<div id="slider" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID, 'large'); ?>); background-position: center center;">
</div>
<?php
}else{
?>
<img src="<?php bloginfo('template_directory');?>/images/blank.jpg" alt="" />
<?php
}
?>
this should produce a div with a background image applied to it,
If you want the Full img tag code to be printed simply use one of the following.
if (has_post_thumbnail()) {
?>
<?php the_post_thumbnail(); // just the image ?>
<?php the_post_thumbnail('thumbnail'); // just the thumbnail ?>
<?php the_post_thumbnail('medium'); // just the Medium Image ?>
<?php the_post_thumbnail('large'); // just the Medium Image ?>
<?php
// adding a 200x200 height and width along with a class to it.
the_post_thumbnail(array( 200,200 ), array( 'class' => 'alignleft' ));
?>
<?php
// Adding a few classes to the medium image
the_post_thumbnail('medium', array('class' => 'alignleft another_class'));
?>
<?php
}
Marty..