Wordpress custom pagination links to a blank page - php

this is my archive of posts: https://polnapol-tarnow.pl/aktualnosci/page/4/
There are only 4 pages but link to another (blank) page is showing up anyway ("Następna strona"). Is there a way to adjust conditions to stop rendering another "Next page" link if there is no another page with posts? I would be very grateful for any help.
Faulty code:
<?php if (!is_paged()) : ?>
<span><?php _e('Next page'); ?> ››</span>
<?php else : ?>
<span><?php _e('Next page'); ?> ››</span>
<?php endif; ?>
Full template:
<?php $b_subitlte = get_field('b_subitlte');
$b_title = get_field('b_title');
$b_desc = get_field('b_desc'); ?>
<section class="blog-home padding czarny">
<?php if ( function_exists('yoast_breadcrumb') ) { ?>
<div class="breadcrumbs">
<div class="container">
<?php yoast_breadcrumb('<p id="breadcrumbs">','</p>'); ?>
</div>
</div>
<?php } ?>
<div class="col-sm-offset-2 col-sm-8 text-center">
<h4 class="upper-title">aktualności</h4>
<h2 class="title">aktywni <br>
nie tylko w kuchni</h2>
<p>nieustannie badamy otaczający świat aby tworzyć nie tylko lepsze potrawy, ale i klimat naszego włoskiego lokalu. Inspirujemy się i piszemy o tym!</p>
</div>
<div class="container">
<div class="row">
<div class="col-sm-offset-2 col-sm-8 text-center">
<h4 class="upper-title"><?php echo $b_subitlte; ?></h4>
<h2 class="title black"><?php echo $b_title; ?></h2>
<p><?php echo $b_desc; ?></p>
</div>
</div>
<?php
$args = array(
'posts_per_page' => 5,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
);
$news = new WP_Query( $args );
if ( $news->have_posts() ) : ?>
<div class="blog-wrapper row">
<?php while ( $news->have_posts() ) : $news->the_post(); ?>
<?php $blog_short = get_field('blog_short'); ?>
<div class="col-md-4 col-xs-6">
<?php if ( has_post_thumbnail() ) : ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div style="background-image: url(<?php echo $url; ?>)" class="thumbnail-cover">
<?php the_post_thumbnail($post->ID);?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="custom-hover">
<span class="main-btn">zobacz post</span>
</a>
</div>
<?php endif; ?>
<p class="meta"><?php the_time('Y-m-d'); ?></p>
<h2 class="title">
<?php the_title(); ?>
</h2>
<p><?php echo $blog_short; ?></p>
</div>
<?php endwhile; ?>
</div>
<?php echo previous_posts_link(); ?>
<?php if (!is_paged()) : ?>
<span><?php _e('Nastepna strona'); ?> ››</span>
<?php else : ?>
<span><?php _e('Następna strona'); ?> ››</span>
<?php endif; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</section>
</div>

You should use the pagination of WordPress. Go to this link: WordPress pagination
You can use this template for your pagination :
<div class="paginate">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<<' ,
'next_text' => '>>' ,
) );
?>
</div>
And you can set a number of the post in one page by going WordPress panel > setting > reading > blog page show at most.

I managed to fix this issue by counting all published posts and dividing them by number of posts per page (in this case 5 posts):
$count_posts = wp_count_posts()-> publish / 5;
So my code was:
<?php echo previous_posts_link(); ?>
<?php if (!is_paged()):?>
<span><?php _e('Previous page'); ?> ››</span>
<?php elseif ($count_posts > $paged ):?>
<span><?php _e('Next page'); ?> ››</span>
<?php else : ?>
<?php endif; ?>

Related

Wrapping a div around the results of a word press loop but excluding the first 2

I have a loop for a custom post type. I'm bringing back a block of title, image and content for each post. I want to apply slick slider to the results to create a slick carousel, but I don;t want to include the first two results of the loop - so I'd need to create a parent div to the results but only start that div after the first two results.
I've trialed ways of querying the results on a loop count to apply a class to only the first two results, but this doesn't really achieve what I'm after.
<div class="wrapper_for_news_items">
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'news',
'order' => 'DESC'
));
if( $posts ): ?>
<?php $post = $posts[0]; $c=0; ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div class="treatment_block news_block <?php $c++; if($c == 1) { echo ' featured'; } elseif($c == 2) { echo ' featured'; } ?>">
<h2 class="block_title above"> <?php the_title( '' ); ?></h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below"> <?php the_title( '' ); ?></h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div> <!-- treatment news block wrapper -->
You could just create 2 loops.
Use the first for the featured output and the second for the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_two_posts );
if( $query_with_two_posts->have_posts ) :
while ( $query_with_two_posts->have_posts ) : $query_with_two_posts->the_posts; ?>
<div class="treatment_block news_block featured">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of 2 post initial news loop -->
</div>
<!-- treatment news block wrapper -->
<?php
// Start your second loop containing the slickslider content
?>
<div class="wrapper_for_news_carousel_items">
<?php
$args_with_all_posts = array(
'posts_per_page' => -1,
'offset' => 2 // Offset the 2 initial posts
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_all_posts );
if( $args_with_all_posts->have_posts ) :
while ( $args_with_all_posts->have_posts ) : $args_with_all_posts->the_posts; ?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news carousel items -->
Or you could count the posts in the loop and asign a wrapper before the third post and after the last post to create the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query = new WP_Query( $args_with_two_posts );
$counter = 1; // Set the counter
if( $query->have_posts ) :
while ( $query->have_posts ) : $query->the_posts;
if ( $count == 3 ) { echo '<div class="slick-slider">'; };
?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php
$counter++; // Add +1 every loop
if (($query->current_post +1) == ($query->post_count)) {
echo '</div>'; // This is the last post
}
endwhile;
?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news block wrapper -->

Use image as a Wordpress conditional

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

How do I structure this nested php loop for Wordpress?

I'm attempting to build a category.php page for a custom Wordpress theme. Ideally, the page would find all category posts, then grab the sticky posts from that category, and if there's at least two, it would display them at the top (it's fine if these same posts repeat below).
This is the page I have built so far. It can gather all of the results without error, but the nested loop which tries to find the most recent two stickies fails. I receive the error:
"Warning: Attempt to assign property of non-object in C:\dev\xampp\htdocs\wordpress\wp-content\themes\ampersand\category.php on line 22"
How do I fix this loop? (I've included a github gist in case that's easier to read, category page for Wordpress theme in development)
<?php // Category Page Content Start ?>
<div id="content">
<main id="main" class="category-page" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Periodical">
<h1><?php single_cat_title(); ?></h1>
<?php // Find all posts for the queried category ?>
<?php $catID = get_queried_object_id();
$args = array( 'numberposts' => -1, 'posts_per_page' => 20,
'cat' => $catID, 'post-type' => 'post' );
$query = new WP_query( $args ); $count = 0;
if ($query -> have_posts()) : ?>
<section class="featured">
<?php // Find the most recent sticky posts for this category; if at least 2, post them seperately ?>
<?php $sticky = get_option( 'sticky_posts' );
$findsticky = array( 'post__in' => $sticky, 'numberposts' => 2 );
$stickyposts = get_posts( $findsticky ); if ($stickyposts -> count = 2) :
foreach ($stickyposts as $post) : $post -> the_post(); ?>
<article class="featured-article" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endforeach; endif; ?>
</section>
<section class="all-results">
<?php while ($query -> have_posts()) : $query -> the_post(); $count++; ?>
<article class="<?php echo 'article-'.$count; ?>" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endwhile; ?>
</section>
<div class="pagination-bottom">
<?php if( get_next_posts_link() ) :
previous_posts_link( 'Older Posts' );
endif; ?>
<?php if( get_next_posts_link() ) :
next_posts_link( 'Newer Posts', 0 );
endif; ?>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_postdata(); ?>
</main>
</div>

wordpress: show posts in two columns

I have a theme that shows the latest four posts in one column. I want to convert this to two posts in two columns.
I made two divs next to each other and put the first in descending order and the other in ascending order. Then I set it to show only 2 posts.
But now it shows 2 posts in the left div and all four posts in the right div:
I don't understand why it is doing this. Here is the code:
<section class="container">
<div class="left-half">
<article>
<!-- =========================
SECTION: LATEST NEWS
============================== -->
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'post', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'ASC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$parallax_one_latest_news_title = get_theme_mod('parallax_one_latest_news_title',esc_html__('Latest news','parallax-one'));
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<div class="section-overlay-layer">
<div align="center" class="container">
<div class="row">
<!-- TIMELINE HEADING / TEXT -->
<?php
if(!empty($parallax_one_latest_news_title)){
echo '<div class="col-md-12 timeline-text text-left"><h2 class="text-left dark-text">'.esc_attr($parallax_one_latest_news_title).'</h2><div class="colored-line-left"></div></div>';
} elseif ( isset( $wp_customize ) ) {
echo '<div class="col-md-12 timeline-text text-left paralax_one_only_customizer"><h2 class="text-left dark-text "></h2><div class="colored-line-left "></div></div>';
}
?>
<div class="parallax-slider-whole-wrap">
<!--<div class="controls-wrap">
<button class="control_next icon icon-arrow-carrot-down"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Down','parallax-one')?></span></button>
<button class="control_prev fade-btn icon icon-arrow-carrot-up"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Up','parallax-one')?></span></button>
</div>-->
<!-- TIMLEINE SCROLLER -->
<div itemscope itemtype="http://schema.org/Blog" id="parallax_slider" class="col-md-6 timeline-section">
<ul class="vertical-timeline" id="timeline-scroll">
<?php
$i_latest_posts= 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$i_latest_posts++;
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 1){
echo '<li>';
}
} else {
echo '<li>';
}
?>
<div itemscope itemprop="blogPosts" itemtype="http://schema.org/BlogPosting" id="post-<?php the_ID(); ?>" class="timeline-box-wrap" title="<?php printf( esc_html__( 'Latest News: %s', 'parallax-one' ), get_the_title() ) ?>">
<div itemscope itemprop="image" class="icon-container white-text">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail('parallax-one-post-thumbnail-latest-news');
else: ?>
<img src="<?php echo parallax_get_file('/images/no-thumbnail-latest-news.jpg'); ?>" width="150" height="150" alt="<?php the_title(); ?>">
<?php
endif;
?>
</a>
</div>
<div class="info">
<header class="entry-header">
<h1 itemprop="headline" class="entry-title"><br><br><br>
<?php the_title(); ?>
</h1>
<!-- .entry-meta -->
</header>
<div itemprop="description" class="entry-content entry-summary">
<?php the_excerpt(); ?>
<?php printf( esc_html__( 'Bekijk de fotos %s', 'textdomain' ), '<span class="screen-reader-text"> '.get_the_title().'</span>' ); ?>
</div>
</div>
</div>
<?php
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 0){
echo '</li>';
}
} else {
echo '</li>';
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div><!-- .parallax-slider-whole-wrap -->
</div>
</div>
</div>
</section>
<?php
}
} ?>
</article>
</div>
<!--rechts-->
<div class="right-half">
<article>
<!-- =========================
SECTION: LATEST NEWS
============================== -->
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'post', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'DESC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$parallax_one_latest_news_title = get_theme_mod('parallax_one_latest_news_title',esc_html__('Latest news','parallax-one'));
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<div class="section-overlay-layer">
<div align="center" class="container">
<div class="row">
<!-- TIMELINE HEADING / TEXT -->
<br>
<br>
<br>
<div class="parallax-slider-whole-wrap">
<div class="controls-wrap">
<button class="control_next icon icon-arrow-carrot-down"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Down','parallax-one')?></span></button>
<button class="control_prev fade-btn icon icon-arrow-carrot-up"><span class="screen-reader-text"><?php esc_attr_e('Post slider navigation: Up','parallax-one')?></span></button>
</div>
<!-- TIMLEINE SCROLLER -->
<div itemscope itemtype="http://schema.org/Blog" id="parallax_slider" class="col-md-6 timeline-section">
<ul class="vertical-timeline" id="timeline-scroll">
<?php
$i_latest_posts= 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$i_latest_posts++;
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 1){
echo '<li>';
}
} else {
echo '<li>';
}
?>
<div itemscope itemprop="blogPosts" itemtype="http://schema.org/BlogPosting" id="post-<?php the_ID(); ?>" class="timeline-box-wrap" title="<?php printf( esc_html__( 'Latest News: %s', 'parallax-one' ), get_the_title() ) ?>">
<div itemscope itemprop="image" class="icon-container white-text">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail('parallax-one-post-thumbnail-latest-news');
else: ?>
<img src="<?php echo parallax_get_file('/images/no-thumbnail-latest-news.jpg'); ?>" width="150" height="150" alt="<?php the_title(); ?>">
<?php
endif;
?>
</a>
</div>
<div class="info">
<header class="entry-header">
<h1 itemprop="headline" class="entry-title"><br><br><br>
<?php the_title(); ?>
</h1>
<!-- .entry-meta -->
</header>
<div itemprop="description" class="entry-content entry-summary">
<?php the_excerpt(); ?>
<?php printf( esc_html__( 'Bekijk de fotos %s', 'textdomain' ), '<span class="screen-reader-text"> '.get_the_title().'</span>' ); ?>
</div>
</div>
</div>
<?php
if ( !wp_is_mobile() ){
if($i_latest_posts % 2 == 0){
echo '</li>';
}
} else {
echo '</li>';
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div><!-- .parallax-slider-whole-wrap -->
</div>
</div>
</div>
</section>
<?php
}
} ?>
</article>
</div>
</section>
If I understood correctly what you want to achieve - to divide several posts into two columns, I can not understand exactly how you want to achieve this into code. If you rely on this condition $the_query->current_post % 2 == 1 to filter the posts, then in your code it only filter printing of the li element, but then the cycle continues and show the post itself, ie what you achieve with this code is to place two posts (div.timeline-box-wrap) elements in one li.If you want to use this method of separation, you should change the code a little (I will simplify it, but the main is, that you must stop current loop if your condition pass).
You don't need to query DB two times with the same query - this is performance issue, so you can use the same result and loop over two times.
You can use $the_query->current_post to get current post and filter.
<section class="container">
<div class="left-half">
<article>
<?php
$parallax_number_of_posts = get_option('posts_per_page');
$args = array( 'post_type' => 'product', 'posts_per_page' => $parallax_number_of_posts, 'order' => 'ASC','ignore_sticky_posts' => true );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<ul class="vertical-timeline" id="timeline-scroll">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if($the_query->current_post % 2 == 1)
continue; ?>
<li><?php the_title() ?></li>
<?php endwhile; ?>
</ul>
</section>
<?php
}
} ?>
</article>
</div>
<div class="right-half">
<article>
<?php
$the_query->rewind_posts();
if ( $the_query->have_posts() ) {
if($parallax_number_of_posts > 0) {
?>
<section class="brief timeline" id="latestnews" role="region" aria-label="<?php esc_html_e('Latest blog posts','parallax-one'); ?>">
<ul class="vertical-timeline" id="timeline-scroll">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if($the_query->current_post % 2 == 0)
continue; ?>
<li><?php the_title() ?></li>
<?php endwhile; ?>
</ul>
</section>
<?php
}
} ?>
</article>
</div>
</section>
P.S. You can simplify more, if you set 2 variables: $left and $right and use only one loop to set one or other with html, and then print their values.

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

Categories