Use image as a Wordpress conditional - php

I'm trying to use a image as a conditional to show different post types. I have one post with images and another one without images. So I built a If else statement and a while loop that calls the post. At the end I have a page number code. But the loop is infinite, I'm not finding where to close it. Some one can help me?
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '3',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="_w-image container-novidades">
<?php if( get_field('imagem_do_evento') ): ?>
<div class="w-row">
<div class="column w-col w-col-4"><img class="<?php the_field('imagem_do_evento');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('imagem_do_evento');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('imagem_do_evento');?></p>
</div>
</div>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<?php endif; ?>

Try to put the endwhile and edn if in the same row like this
<?php endwhile; wp_reset_query(); endif; ?>

I Figured it Out. It was a div opening outside the if statement. And I wasn't closing all if else properly. Check out the final code.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="texto-longo">
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
<div class="row-2 w-row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'cat' => '4',
);
$wp_query = new WP_Query( $args );
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php if( get_field('foto_novidades') ): ?>
<div class="_w-image container-novidades">
<div class="w-row">
<div class="column w-col w-col-4"><img class="img-novidades" src="<?php the_field('foto_novidades');?>">
</div>
<div class="colomn-text-novidades w-col w-col-8">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
</div>
</div>
<?php else:?>
<div class="container-novidades">
<h1 class="txt-blue"><?php the_title();?></h1>
<h3 class="txt-blue"><?php the_field('sub_titulo_novidades');?></h3>
<p><?php get_the_date('d/m/Y'); ?></p>
<p><?php the_field('texto_novidades');?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="navegacao-paginas">
<div class="paginacao twisted w-inline-block">
<div class="seta-text"><?php echo get_previous_posts_link( '⟶' ); // display newer posts link ?></div>
</div>
<div class="paginacao w-inline-block">
<div class="seta-text"><?php echo get_next_posts_link( '⟶', $wp_query->max_num_pages ); // display older posts link ?></div>
</div>
</nav>
<?php } ?>
</div>
<?php wp_reset_query(); else : ?>
<p>Ainda não temos novidades :(</p>
<?php endif; ?>

Related

Querying only the latest sticky post

So I'm trying to pull only the latest sticky post from WordPress and only 1. I am using 'showpost=1' but for some reason if I have two posts tagged as sticky both show up?
<h2>Breaking News</h2>
<?php
query_posts('posts_per_page=1');
if (have_posts()) {
while (have_posts()) : the_post();
if ( is_sticky() ) : ?>
<div class="small-12 medium-6 large-4 columns">
<div class="img-holder">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('sticky-hp-thumbnails');
}
?>
<?php if( get_field('sticky_sub_heading') ): ?>
<div class="tag">
<p>
<?php the_field('sticky_sub_heading'); ?>
</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<h3><a href"<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h3>
<?php if( get_field('sticky_date') ): ?>
<p class="sticky-date">
<?php the_field('sticky_date'); ?>
</p>
<?php endif; ?>
<p>
<?php the_field('sticky_summary'); ?>
</p>
Read More </div>
<?php endif;
endwhile;
}
wp_reset_query();
?>
Where am I going wrong in the above code?
Use posts_per_page instead of "showposts"
i.e.
query_posts( 'posts_per_page=1' );
Source:
https://codex.wordpress.org/Function_Reference/query_posts
Updated: Adding WP_QUERY code to fetch latest sticky post:
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<h3><?php echo get_the_title(); ?></h3></a>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
The easiest solution to solve (workaround) your problem is to remove the while loop. This way you'll print only one.
Of course, this is sub-optimal since other pages may be fetched and unused; you could try using posts_per_page=1 or post_limits=1 to see if solve the issue.

WP-pagination and excluding category from page

I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);

Advanced custom fields, php variables and displaying content

I have an advanced custom field group, and inside the group and inside the group are a few fields and one select field in where the user selects a category. I want to output onto the page based on what category has been chosen.
Here is the code I was trying:
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=Specialist_post&showposts=' . $limit . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
if(have_posts()) :
while(have_posts()) :
the_post();
if(get_field('category') == "1402")
{ ?>
<div class="row wheelsspecial">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h3><?php the_field('company_name'); ?></h3>
<p><?php the_field('brief_description'); ?></p>
<p><?php the_field('contact_number'); ?></p>
<?php the_field('website_address'); ?><br />
<?php the_field('email'); ?>
<?php } else if (get_field('category') == "1403") {?>
<div class="row tuningspecial">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h3><?php the_field('company_name'); ?></h3>
<p><?php the_field('brief_description'); ?></p>
<p><?php the_field('contact_number'); ?></p>
<?php the_field('website_address'); ?><br />
<?php the_field('email'); ?>
<?php } ?>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
This now outputs nothing, if i echo the field category its returning 1403, so i would think it at least outputs the else if, maybe this whole code design is wrong. Essentially i want to print different into different divs based on the category selected.
Did it this way, not the ideal solution, works for now, just needs a code review
<div class="row wheelsspecial">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'Specialist_post',
'meta_key' => 'category',
'meta_value' => '1402'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_field('company_name'); ?></h3>
<p><?php the_field('brief_description'); ?></p>
<p><?php the_field('contact_number'); ?></p>
<?php the_field('website_address'); ?><br />
<?php the_field('email'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
<div class="row tuningspecial">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'Specialist_post',
'meta_key' => 'category',
'meta_value' => '1403'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_field('company_name'); ?></h3>
<p><?php the_field('brief_description'); ?></p>
<p><?php the_field('contact_number'); ?></p>
<?php the_field('website_address'); ?><br />
<?php the_field('email'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
Thanks all

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!

Categories