How to add post link to thumbnail in wordpress? - php

I am using the following loop in my index.php to show the posts with a thumbnail:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
How can I add a permalink to the thumbnail? When the user clicks on it he should be directed to the post. At the moment, nothing is happening.
Thanks for your answers.
EDIT: I added the whole loop because the answer of Deepti produces an error. Maybe someone could help me with that.

<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>

Related

How do i only display posts with a certain category?

So I'm building a website for my new business.
An I want to only run the following code if the post have a category of 'certain-category'.
How do I do this? I tried a number of things. but they do not work..``
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
Use has_category()
if(has_category('certain-category', get_the_ID())):
// Do something
endif;
WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown e.g. Title, thumbnail, excerpt etc

How to target a specific query from functions.php in Wordpress

I'm trying to use offset on a specific WP_Query on a page that has a total of 3 Loops with the code described here.
For now, this works but I can only target all three loops. What (I think) I have to do is change this part:
//Before anything else, make sure this is the right query...
if ( ! $query->is_posts_page ) {
return;
}
To something like this:
//Before anything else, make sure this is the right query...
if ( ! $query->$THELOOPIWANTTOTARGET ) {
return;
}
Is there a way to do this?
The query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$mainloop = new WP_Query(array('posts_per_page' => 3, 'paged' => $paged));
?>
<div class="full padding center isotope">
<?php if ( $mainloop->have_posts() ) : while ( $mainloop->have_posts() ) : $mainloop->the_post(); ?>
<article class="post">
<?php the_post_thumbnail(); ?>
<div class="textwrap">
<h2><?php the_title(); ?></h2>
<?php if ( get_field('bron')): ?>
<?php if ( get_field('bron')): ?><a href="<?php the_field('bron_link'); ?>" target="_blank" class="source"><?php endif; ?>
<?php the_field('bron'); ?>
<?php if ( get_field('bron')): ?></a><?php endif; ?>
<?php endif; ?>
<?php the_excerpt(); ?>
</div>
<div class="readmore">
<div class="fa fa-share share" id="<?php the_ID(); ?>">
<div class="share_block"><div class="addthis_sharing_toolbox" data-url="<?php the_permalink(); ?>"></div></div>
</div>
Lees verder
</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>

if have posts display div else display no posts statment

I have no idea why this code is not working. I am trying to create a conditional statement where if this custom post type has posts then display customer-section div. if post does not exist then print no post statement. I did everything I thought I am suppose to but I must be doing something silly wrong because I can still see the customer-section div even though there are no posts.
here is what I have:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
What you are currently doing is checking if your initial have_posts() (the default query) condition is true and if this is a page template it is always true. Actually (for page templates) you don't need that check at all, since WordPress will return 404 if the page is not found.
You need a check for posts for your custom query:
<?php the_post(); ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile;
else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>
</div>
</div>
I believe the issue is your syntax with the two loops.
You're missing the if statement before the while condition
You're trying to load your $loop loop inside the standard page or post loop.
Try this instead:
<?php
$case_studies = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) );
if ( $case_studies->have_posts() ) : ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $case_studies->have_posts() ) : $case_studies->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<footer>
<?php if (get_the_tags()) { ?>
<p><?php the_tags(); ?></p>
<?php } ?>
</footer>
</div>
<hr />
</div>
<?php endwhile; ?>
</div>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_query(); ?>
this is what worked for me. You don't need to end the while loop if it's inside the WP_Query. All you have to do is close the if statement for the main div you want to show, add the while for the repeating section and if it doesn't work, I'd try deleting the conditions you have set on your footer.
<?php $loop = new WP_Query( array( 'post_type' => 'case_study', 'posts_per_page' => 9 ) ); if($loop->have_posts()): ?>
<div class="customer-section case-study">
<div class="case-study-container">
<h2>Case Studies</h2>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div <?php post_class(); ?>>
<?php if ( has_post_thumbnail() ): ?>
<div class="press-featured-image">
<?php the_post_thumbnail('', array('class' => 'th')); ?>
</div>
<?php endif; ?>
<div class="blog-post">
<h3><?php the_title(); ?></h3>
<div class="entry-summery">
<?php the_excerpt(); ?>
</div>
<footer>
<?php $tag = get_the_tags(); if (!$tag) { } else { ?><p><?php the_tags(); ?></p><?php } ?>
</footer>
</div>
<hr />
</div>
<?php } wp_reset_postdata(); ?>
</div>
</div>
<?php endif; ?>

pagination custom post type with multiple WP_Query loop

after adding the function of pagination to Functions.php and recall it in template-product-listing.php
there is nothing shown in result.
I have a big problom with this...
could you find and resolve the problem?
thnx
<article class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="single-head" class="sixteen columns">
<h1><?php the_title(); ?></h1>
<?php if (has_excerpt()) { the_excerpt(); } ?>
</div>
<div class="row">
<nav id="portfolio-filters" class="sixteen columns">
<?php _e('Show All', 'ci_theme'); ?>
<?php
$args = array(
'hide_empty' => 0
);
$skills = get_terms('product-category', $args);
?>
<?php foreach ( $skills as $skill ) : ?>
<?php echo $skill->name; ?>
<?php endforeach; ?>
</nav><!-- /portfolio-filters -->
</div>
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query('post_type=product&posts_per_page=4'); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
<?php $item_skills = wp_get_object_terms($post->ID, 'product-category'); ?>
<article class="<?php ci_e_setting('product_columns'); ?> columns <?php foreach ( $item_skills as $item_skill ) : echo $item_skill->slug.' '; endforeach; ?> columns portfolio-item">
<a href="<?php echo get_permalink(); ?>" title="<?php echo esc_attr(get_the_title()); ?>" class="fb">
<?php the_post_thumbnail('ci_portfolio_slider', array('class'=>'scale-with-grid')); ?>
</a>
<div class="portfolio-desc">
<h3><?php the_title(); ?></h3>
<p class="desc"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?>...</p>
</div>
</article><!-- /portfolio-item -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- /portfolio-items -->
<?php get_template_part('part', 'call_to_action'); ?>
<?php endwhile; endif; ?>
<div class="pagination">
<?php wp_pagination(); ?>
</div>
</article>
if your sure there is a post_type called products and there is posts in it...
try:
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query(array('post_type'=>'product', 'posts_per_page'=> 4); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
You know you are starting a new wp_query for every post in have_posts() ? you might want to rethink what you are trying to achieve!

Wordpress: Use different loops

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(); ?>

Categories