I've made a custom metabox that if selected makes the post thumbnail the background of the site.
Now I need that this post thumbnail has a link to the post.
< ?php query_posts ('showposts=5$cat=2');
if (have_posts()) : ?>
if ( has_post_thumbnail() && get_post_meta($post->ID, 'dbt_checkbox', true) ) {
the_post_thumbnail('background');
}
else {}
?>
<?php endwhile; endif; ?>
Wordpress documentation provides an example for this exact situation
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
However, I kind of hate opening and closing php tags so lightly. I'll try something to improve this answer.
Edit: got it. Please try this:
if ( has_post_thumbnail() && get_post_meta($post->ID, 'dbt_checkbox', true) ) {
echo '<a href="' . get_permalink( $post->ID ) . '" >';
echo get_the_post_thumbnail( $post->ID, 'background' );
echo '</a>';
}
Related
I have a custom post type archive page that lists the categories associated with that post type. I would like to include the thumbnail of the latest post in each category as the thumbnail for the said category.
So far I have managed to create the list of the categories, but my attempt at including the latest post thumbnail has resulted in the latest post for the post type as a whole being returned for all categories.
This is how I currently have it below:
<?php if ( $terms && !is_wp_error( $terms ) ) :
?>
<ol class="lister__list">
<?php foreach ( $terms as $term ) { ?>
<li class="lister__item item">
<article class="lister__article">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>" class="item__link" title="Permalink to <?php the_title(); ?>" rel="bookmark">
<!--image-->
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumb' );?>
<?php if ( has_post_thumbnail() ) { ?>
<img src="<?php echo $thumb[0]; ?>" width="<?php echo $thumb[1];?>" height="<?php echo $thumb[2];?>" />
<?php } else { ?>
nope
<?php }?>
<h2 class="item__title"><?php echo $term->name; ?></h2>
</a>
</li>
<?php } ?>
</ol>
<?php endif;?>
any help much appreciated.
I'm trying to use a Wordpress slider and change the link on the thumbnail to the category URL instead of the permalink (while still pulling the thumbnail from the post). I started with a slider that used a permalink and the post thumbnail like so:
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php echo the_permalink(); ?>" class="listing-link">
<?php
if ( true == $thumbnail ) {
// if the post has a feature image, show it.
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title">
<?php the_title(); ?>
</div>
</a>
</li>
Using Wordpress Codex and tips from this related post, I've gotten as far as to change the title beneath the thumbnail correctly, but I can't seem to find the correct combination on the <a href="" piece to call the thumbnail's category URL. Is there a way to correctly do this? Here's where I'm at at this point:
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php $categories = get_categories(); foreach ($categories as $cat) { $category_link = get_category_link($cat->cat_ID); } echo esc_url( $category_link ); ?>" class="home-category-link">
<?php
if ( true == $thumbnail ) {
// if the post has a feature image, show it.
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title">
<?php the_category(); ?>
</div>
</a>
</li>
This is the closest I've gotten, but the URL is now pulling a category URL unrelated to the post. Not sure what I'm missing but any help is appreciated. Thanks!
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" />'
; } ?>
I'm having a problem with my Wordpress theme. My next_posts_link() only reloads the same page. Here's my code.
<div id="main">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="utdrag">
<h2><?php the_title(); ?></h2>
<div class="crop"><a href="<?php the_permalink(); ?>">
<?php
if ( get_the_post_thumbnail($post_id) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="';
echo catch_that_image();
echo '" alt="Image unavailable" class="crop" />';
echo '</a>';
}
?></a>
</div>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else: endif; ?>
<?php next_posts_link();?><?php previous_posts_link();?>
</div>
I'm using a static page as my front page. As you can see it's only displaying posts that have the same category as the page title: query_posts('category_name='.get_the_title().'&post_status=publish,future');
This is something I want to keep. So does anyone know why it just reloads? Why it isn't changing to the next page?
I figured it out! I'm posting it here in case someone else is having the same problem!
I added this code at the top:
<?php if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}
elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}
else { $paged = 1; }
?>
And replaced this:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
with this:
<?php query_posts('catery_name='.get_the_title().'&showposts=2'.'&paged='.$paged); ?>
For more information, check out this link:
http://wordpress.org/support/topic/front-page-wp-query-pagination-issue-repeating-posts-next_posts_link-help
These two functions does not work on static pages.
From the documentation for wp_next_post_link():
This function does not work with static pages.
However, take a look at this article. It talks about how to create a static front page with dynamic content.
I am using query_post to show a list of recent post.
And I want to give special style and html markup to the first post.
This is my current code:
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=query_posts($args);
if ($posts) {
echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
while ( have_posts() ) : the_post();
if( $wp_query->current_post == 0 ) :?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
<?php else : ?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
<?php endif;
endwhile;
} // if ($posts
} // foreach($categories
All you need to do is just wrap your snippet into if statement and call the_post function before while loop. By calling the_post function you will fetch first record from your queue. It should be like this:
if ( have_posts() ) :
the_post();
if ( has_post_thumbnail()) :
?><a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('post-thumb'); ?>
</a><?php
endif;
?><h5>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h5><?php
while ( have_posts() ) :
the_post();
if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="thumb" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail('post-thumb'); ?></a>
<?php endif; ?>
<h5> <?php the_title(); ?></h5>
endwhile;
endif;
Depending on what you want to style [the whole post, the title, whatever], you're going to want to identify the first post, and then conditionally output a class or whatever HTML you want for that post. The "loop" is the part of your code between the while and the endwhile. So right before the loop, put:
$is_first_post = TRUE;
then inside the while loop, before whatever it is you're trying to add styles to, put:
if ($is_first_post == TRUE){
echo (" .... THIS WOULD BE ADDED TO THE FIRST POST ONLY ....");
$is_first_post = FALSE; //this flags the next post as not being first
}