For some reason Wordpress fails to show page content created in php (bootstrap).
This is done in custom/self-made template.
Can you please look at my code below, and see what I am doing wrong? I am using latest version of wordpress.
<?php get_header(); ?>
<div class="row">
<div class="col-xs-12 col-sm-8">
<div class="row text-center no-margin">
<?php
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $currentPage);
query_posts($args);
if( have_posts() ): $i = 0;
while( have_posts() ): the_post(); ?>
<?php
if($i==0): $column = 12; $class = '';
elseif($i > 0 && $i <= 2): $column = 6; $class = ' second-row-padding';
elseif($i > 2): $column = 4; $class = ' third-row-padding';
endif;
?>
<?php the_content(); ?>
<div class="col-xs-<?php echo $column; echo $class; ?> blog-item">
<?php if( has_post_thumbnail() ):
$urlImg = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
endif; ?>
<div class="blog-element" style="background-image: url(<?php echo $urlImg; ?>);">
<!--<?php the_title( sprintf('<h1 class="entry-title">', esc_url( get_permalink() ) ),'</h1>' ); ?> -->
<h1 class="entry-title"><?php the_title(); ?></h1>
<small><?php the_category(' '); ?></small>
</div>
</div>
<?php $i++; endwhile; ?>
<div class="col-xs-6 text-left">
<?php next_posts_link('« Older Posts'); ?>
</div>
<div class="col-xs-6 text-right">
<?php previous_posts_link('Newer Posts »'); ?>
</div>
<?php endif;
wp_reset_query();
?>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
$args = array('posts_per_page' => 3, 'paged' => $currentPage);
query_posts($args);
dont use query_posts anymore
$args = array('posts_per_page' => 3,'post_type'=>'post', 'paged' => $currentPage);
new WP_Query($args);
Related
I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
Here is the code I'm using, simplified of course:
How to do pagination for this?
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; endif; ?>
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
First of all, you must setup properly your query:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
And second, you need to add pagination links out of the loop
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
Whole code can looks like this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
Problem has been solved:
<?php
global $paged, $wp_query, $wp;
$args1 = array('cat' => '3, 7, 10, 12');
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=3&cat=3,7,10,12');
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
due to some reason I'm getting the search bar in duplicate.
I suspect I have a double function command somewhere...but I can not find it. Can someone look at the code with "fresh eyes" and try to find my mistake?
as wordpress keeps giving me 2 search bars instead of one
I am using Notepad++, Bootstrap v 3.0 and Wordpress 4.7.8
Here is the code
<?php get_header(); ?>
<div class="row">
<div class="col-xs-12 col-sm-8">
<div class="row text-center no-margin">
<?php
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3,'post_type'=>'post', 'paged' => $currentPage);
new WP_Query($args);
if( have_posts() ): $i = 0;
while( have_posts() ): the_post(); ?>
<?php
if($i==0): $column = 12; $class = '';
elseif($i > 0 && $i <= 2): $column = 6; $class = ' second-row-padding';
elseif($i > 2): $column = 4; $class = ' third-row-padding';
endif;
?>
<?php the_content(); ?>
<div class="col-xs-<?php echo $column; echo $class; ?> blog-item">
<?php if( has_post_thumbnail() ):
$urlImg = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
endif; ?>
<div class="blog-element" style="background-image: url(<?php echo $urlImg; ?>);">
<!--<?php the_title( sprintf('<h1 class="entry-title">', esc_url( get_permalink() ) ),'</h1>' ); ?> -->
<h1 class="entry-title"><?php the_title(); ?></h1>
<small><?php the_category(' '); ?></small>
</div>
</div>
<?php $i++; endwhile; ?>
<div class="col-xs-6 text-left">
<?php next_posts_link('« Older Posts'); ?>
</div>
<div class="col-xs-6 text-right">
<?php previous_posts_link('Newer Posts »'); ?>
</div>
<?php endif;
wp_reset_query();
?>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
Try this code.
I have disabled the //get_sidebar();
<?php get_header(); ?>
<div class="row">
<div class="col-xs-12 col-sm-8">
<div class="row text-center no-margin">
<?php
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3,'post_type'=>'post', 'paged' => $currentPage);
new WP_Query($args);
if( have_posts() ): $i = 0;
while( have_posts() ): the_post(); ?>
<?php
if($i==0): $column = 12; $class = '';
elseif($i > 0 && $i <= 2): $column = 6; $class = ' second-row-padding';
elseif($i > 2): $column = 4; $class = ' third-row-padding';
endif;
?>
<div class="col-xs-<?php echo $column; echo $class; ?> blog-item">
<?php if( has_post_thumbnail() ):
$urlImg = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
endif; ?>
<div class="blog-element" style="background-image: url(<?php echo $urlImg; ?>);">
<!--<?php the_title( sprintf('<h1 class="entry-title">', esc_url( get_permalink() ) ),'</h1>' ); ?> -->
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<small><?php the_category(' '); ?></small>
</div>
</div>
<?php $i++; endwhile; ?>
<div class="col-xs-6 text-left">
<?php next_posts_link('« Older Posts'); ?>
</div>
<div class="col-xs-6 text-right">
<?php previous_posts_link('Newer Posts »'); ?>
</div>
<?php endif;
wp_reset_query();
?>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<?php //get_sidebar(); ?>
</div>
</div>
I have two loop with orderby random(all work good), but I want to show same post for each loop,
example: I have post 1 to 10;
loop 1 show post 1 2 and 3;
loop 2 need to show same post 1 2 and 3, my code is:
<div class="col-md-12" data-wow-delay="0.2s">
<div class="carousel slide" data-ride="carousel" id="quote-carousel">
<!-- Bottom Carousel Indicators -->
<ol class="carousel-indicators">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$avatar_testimonials = get_field('avatar-testimonials');
?>
<li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>
<?php $i++; endwhile; ?>
</ol>
<!-- Carousel Slides / Quotes -->
<div class="carousel-inner text-center">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- Quote 1 -->
<div class="item <?php if ($i == 0) echo 'active'; ?>">
<blockquote>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<?php echo the_excerpt(); ?>
<small><?php echo the_title(); ?></small>
</div>
</div>
</blockquote>
</div>
<?php $i++; endwhile; wp_reset_postdata(); ?>
</div>
</div>
</div>
Try this, I think it will work for you.
<div class="col-md-12" data-wow-delay="0.2s">
<div class="carousel slide" data-ride="carousel" id="quote-carousel">
<!-- Bottom Carousel Indicators -->
<ol class="carousel-indicators">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$avatar_testimonials = get_field('avatar-testimonials');
?>
<li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>
<?php $i++; endwhile; ?>
</ol>
<!-- Carousel Slides / Quotes -->
<div class="carousel-inner text-center">
<?php
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- Quote 1 -->
<div class="item <?php if ($i == 0) echo 'active'; ?>">
<blockquote>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<?php echo the_excerpt(); ?>
<small><?php echo the_title(); ?></small>
</div>
</div>
</blockquote>
</div>
<?php $i++; endwhile; wp_reset_postdata(); ?>
</div>
</div>
</div>
hi I have a problem to put featured post without pagination and featured post with pagination in one page:
Here is the code the first one I use this:
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) : while (have_posts()) : the_post();
$postImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
?>
<div class="slider" style="background:url(<?php echo $postImage[0];?>) no-repeat center center">
<div class="slider-shadowbox"></div>
<a class="slider-hyperlink" href="<?php the_permalink(); ?>">
<div class="slider-title">
<div class="featured-post-author">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '100'); ?>
<?php the_author_posts_link(); ?>
</div>
<span class="slider-tagline"><?php the_title()?></span>
<em class="slider-author">By <?php the_author_posts_link(); ?></em>
<em class="slider-date"><i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></em>
</div>
</a>
</div>
<?php
endwhile;
endif;
?>
/and the second one I use this to create featured post with pagination, but somehow the pagination shows the numbers but the hyperlink for each number doesn't work properly./
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="featured-post">
<div class="zitem">
<?php the_post_thumbnail( array(360, 240));?>
</div>
<span class="featured-post-summary">
<h3><?php the_title(); ?></h3>
<?php echo get_excerpt(); ?>
</span>
<div class="featured-post-author-wrap">
<?php echo get_avatar( get_the_author_meta('user_email'), $size = '35'); ?>
<span class="featured-post-author">By <?php the_author_posts_link(); ?></span>
<span class="featured-post-date"><?php the_time('F j, Y'); ?><span>
</div>
</div>
<?php endwhile; ?>
<div class="featured-post-pagination pagination">
<div class="pagination-outer"><?php my_pagination(); ?></div>
</div>
<?php else : ?>
<strong>Sorry no post available.</strong>
<?php endif; ?>
I'm having trouble being able to insert an ad after every 3rd post in Wordpress. This is the code in the theme. I know I need a counter and an if statement.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
<a id="loadmore" href="#" data-loading="<?php _e( 'Loading ...', THB_THEME_NAME ); ?>" data-nomore="<?php _e( 'No More Posts to Show', THB_THEME_NAME ); ?>" data-count="5" data-action="thb_ajax_home"><?php _e( 'Load More', THB_THEME_NAME ); ?></a>
</section>
I could show you what I tried and failed at, just to show that I tried. Don't give me negative points for that please.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php $i = 1; if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if($i == 1) : ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<div class="clear"> </div>
<?php if ( $i == 3|| $i == 9 || $i == 15 ) : ?>
<?php if (function_exists ('adinserter')) echo adinserter (2); ?>
<div class="clear"> </div>
<?php endif; ?><?php endif; ?>
<?php $i++; ?>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
</section>
Thank you.
You're close. You'll want to increment your counter within the while loop (using $i++), and then use the modulus operator % to determine whether or not the counter is evenly divisible by 3:
<?php if ($query->have_posts()) : $i = 1; while ($query->have_posts()) : $query->the_post(); ?>
<!-- Article -->
<?php if ( $i % 3 == 0 ) : ?>
<!-- Advertisement Here -->
<?php endif; ?>
<?php $i++; endwhile; else: ?>
<!-- Display Notice -->
<?php endif; ?>