How to have featured image link to larger version in Wordpress? - wordpress

How do I link a post's featured image to its full size version on a category page?
Here is my loop on my category.php:
<h1>Professional Portfolio Gallery - <?php single_cat_title(); ?></h1>
<p><?php echo category_description(); ?></p>
<?php if (have_posts()) : ?>
<ul class="portfolio">
<?php while (have_posts()) : the_post(); ?>
<li>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<?php the_excerpt(); ?></li>
<?php endwhile; ?>
</ul>
<?php wp_paging(); ?>
<?php else : ?>
<h1>Not Found</h1>
<p><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
Any help would be appreciated!

To link Post Thumbnails to the full size version you should be able to use the following:
<?php
if ( has_post_thumbnail()) {
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $full_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('thumbnail');
echo '</a>';
}
?>
You can find out more about this redirect here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
Hope that helps!

Related

Why does the_permalink() method return a preview link?

I've got the code below on an "archive.php" template file. The job is to list all archived posts on the page by year. Although the code does that, it appears that the value being returned by the_permalink() is a preview link. Therefore the post can only be accessed when the user is logged in to the back-end of the website. I'd expected that the method would return the actual (permanent) permalink instead... But maybe I'm missing something?
The "archived" status is generated via the edit-flow plugin.
<?php
/*
Template Name: Archive Page Template
*/
?>
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
<?php
$yearly = new WP_Query(array('posts_per_page' => -1, 'post_status'=>'archived'));
$prev_year = null;
if( $yearly->have_posts() ) : while( $yearly->have_posts() ) : $yearly->the_post();
$this_year = get_the_date('Y');
if ($prev_year != $this_year) {
if (!is_null($prev_year)) {
echo '</ul>';
}
echo '<h3>' . $this_year . '</h3>';
echo '<ul>';
}
echo '<li>';?>
<?php the_title(); ?>
<?php
echo '</li>';
$prev_year = $this_year;
endwhile;
echo '</ul>';
else :?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_postdata();
?>
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Theme is called TrillingTheme and didn't include any template for archive page natively. Wordpress' version is 5.4.2

Wordpress post object getting page title rather than object title

I have an ACF repeater, which repeats post objects. I am changing the postdata to the post object rather than the page so that i can get the title and thumbnail image. This works perfectly for the first one, however the subsequent objects pull the correct thumbnail but the title is pulled from the page title.
Heres the code:
<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>
<?php $post_object = get_sub_field('service'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post ); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php the_permalink(); ?>" class="service">
<div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_field('read_more_text'); ?></p>
</div>
</a>
<?php unset($post_object, $post); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
Any help would be greatly appreciated!
Ok, so i removed the <?php unset($post_object, $post); ?> and it works now. Im concerned that this will give me trouble later down the line with variables however.

Displaying post titles from specific category in WordPress

I would like to display a few post titles from a specific category on homepage. First one have to be with a small thumbnail and excerpt and rest of them just title. Below this section I would like to have a link clicking on which will show all the posts under this category.
like this http://i.stack.imgur.com/N5jUA.jpg
As arslaan ejaz said, you can use wp_query. But I think that answer is not enough for your question. You want to show first post with thumbnail and others with titles only right?. It can be done by php count. Here is what I am using on my site. check below code, it will show first post with thumbnail, title and excerpt, other three posts with title only from category ID 1.
<div class="main-div">
<div class="thumbnaildiv">
<?php $count=1; $query = new WP_Query('showposts=4&cat=1&offset=0'); if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php if($count==1){ ?>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</h2>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<p><?php the_excerpt(); ?> ...</p>
</div>
</div><!--div with thumbnail, title, and excerpt end-->
<div style="clear:both"></div>
<div class="without-thumb">
<ul>
<?php } if($count>1){ ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</li>
<?php } if($count==4){ ?>
</ul>
<?php } ?>
<?php $count++; endwhile; else: endif; wp_reset_postdata(); ?>
</div><!--div without thumbnail end-->
</div><!--main div end-->
The div's I have used is for information purpose only. You can change and style it as desired.
Use WP-Query:
<?php
$args = array('cat'=>1);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '<li>' . the_permalink() . '</li>';
echo '<li>' . the_excerpt() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
It will list all posts of category 1, with title, permalink, and excerpt
More info: wp_query
I can recommend you to add 'Elementor' plugin. With this plugin, you can add 'read more' and split your text. If you add 'read more' in the beginning of your text then there will be shown only title and under the title a 'read more' link.
Here is the Screenshot

Group Posts by Category in Monthly Archive

I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>

the_post_thumbnail not working

I'm not super familiar with wordpress, but I've been working on getting my layout written as a theme so I can use the wordpress platform. For some reason I can't get the_post_thumbnail function working. I already added
add_theme_support( 'post-thumbnails' );
to my functions.php file and yes I did set a featured image and include the_post_thumbnail(); in the loop.
What am I doing wrong? I went as far as to go through the actual function and found that it is having problems with the $image variable in the wp_get_attachment_image() function while referencing the wp_get_attachment_image_src() function. $image is empty, when I'm guessing it shouldn't be. It get's the post_thumbnail id just fine though so I know it knows there's an image set. It just won't display the darn thing.
I'm writing myself a custom theme from scratch so functions.php only has add_theme_support( 'post-thumbnails' ); in it right now if you're curious.
Edit:
Here's my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-entry clearfix" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" >
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img class="home-thumb trans-border" src="' . catch_first_image() . '" width="200px" height="150px" title="' . the_title() . '" />';
}
?>
</a>
<div class="home-post">
<div class="home-meta">Posted <?php the_time('M j, Y'); ?> - <?php the_category(' , ') ?> - <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<h2 class="post-title">
<?php the_title(); ?>
<a class="read" href="<?php the_permalink(); ?>" title="Read More">Read More</a>
</h2>
<div class="home-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo paginate_links() ?>
<?php else : ?>
<h2>Nothing Found</h2>
<?php endif; ?>
Continued:
So I went and found a theme with featured image support and copied that part of the loop exactly and still nothing:
<?php if(has_post_thumbnail()) {
echo '<span class="thumbnail">';the_post_thumbnail(array(100,100)); echo '</span>';
} else {
$image = evlget_first_image();
if ($image):
echo '<span class="thumbnail"><img src="'.$image.'" alt="';the_title();echo'" /></span>';
endif;
} ?>
So what the heck could it be? I'm so confused...
This function will echo itself.
How to solve: Remove the echo in front of the function.
check this
Are your images locally stored on your server/local machine?
If not, the_post_thumbnail() won't work as it can't retrieve an image based on a URL.

Resources