Wordpress posts pagination - wordpress

I'am creating a website at Wordpress and in my website there exist a post type called "news". What I ask for is I'm showing 5 posts in a page but I couldn't show the "next page" button. My code is below, if anyone can help.
<div class="row10 offset1">
<div class="news">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'news', 'posts_per_page' => 5, 'paged' => $paged );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row-fluid">
<div class="well-small">
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_date("d.m.Y"); ?>
</div>
</div>
<?php
endwhile;
?>
</div>
<div class="pull-right">
<ul class="pager">
<li> <?php echo previous_posts_link();?></li>
<li><?php echo next_posts_link();?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>

I've solved my problem, but I don't know how it happened. =) I've added query_posts($args); and it worked properly.
<div class="span10 offset1">
<div class="">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'haber', 'posts_per_page' => 3, 'paged' => $paged );
$loop = new WP_Query( $args );
query_posts($args); <--- I added this line and it worked --->
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row-fluid">
<div class="well-small">
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_date("d.m.Y"); ?>
</div>
</div>
<?php
endwhile;
?>
</div>
<div class="pull-right">
<ul class="pager">
<li> <?php previous_posts_link("Geri"); ?></li>
<li> <?php next_posts_link("İleri"); ?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>

Related

Wp-query 'post__not_in' doesn't work when I want to exclude some posts

Here is my code and I'm trying to exclude some posts that have been added to the custom post type but it's not working for me, please if anyone can help me??
<div class="grid-x small-up-1 medium-up-3 large-up-3 gridi">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php
$args = array(
'post_type' => 'gallery',
'posts_per_page' => -1,
'post__not_in' => array (779, 1394, 774, 3278),
);
$loop = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="cell">
<div class="item-navigation">
<a href="<?php the_permalink(); ?>">
<div class="image-container">
<?php the_post_thumbnail(); ?>
<div class="description">
<h5 class="post-title"><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
<a class="read-more" href="<?php the_permalink(); ?>">Read More</a>
</div>
</div>
</a>
</div>
</div>
<?php endwhile; endif; ?>
</div>
This line..:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
... simply executes the global wp_query. The query you declare the line above as $loop isn't used.
Try:
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

Problem showing post for the term of the current page Wordpress

I'm trying to list the posts related to the current taxonomy page but it shows me all the posts instead. When I output the current term name is correct so really I don't know why this happens. I'm pasting here the code if anyone can put me in the right direction.
<?php
$term = get_queried_object();
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term->term_id
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Try with:
<?php
$term = get_queried_object()->term_id;
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Just use tax query.
<?php
$term = get_queried_object()->term_id;
$args = array(
'post_type' => 'properties-for-sale',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
);
$query = new WP_Query( $args ); ?>

How to get post into homepage?

Hi all I am a new of wordpress,I use theme wordpress foundation, When I post sth, in my homepage can not see post ,and I try to write code in my homepage to get post but still can not see post,How can I do ,and here is my code:
page.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<div id="page-gina">
<?php
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
</div>
</div>
<?php get_footer(); ?>
Help me please !!!
<?php
/*Template Name: homepage template
*/
get_header();
?>
<?php
$args = array(
'cat' => '3',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<?php get_footer(); ?>
Note: In dashboard,
i)Create page called Home,
ii)Same page right side template list shown assign homepage template,
ii)In Settings->Reading->Assign front page.

pagination within wp_query / wordpress

I have written this code for a custom loop in Wordpress, but I cannot seem to get pagination working.
<?php //Template Name: Acapellas ?>
<?php get_header(); ?>
<?php if (is_user_logged_in()) { ?>
<div id="main-content">
<div class="container">
<div class="row">
<div class="col-md-3">
<?php get_sidebar('primary-left'); ?>
</div>
<div class="col-md-9">
<div class="post-<?php the_ID(); ?> post-content">
<h1>Acapellas</h1>
<?php $loop = new WP_Query( array( 'post_type' => 'acapella', 'posts_per_page' => 100 ) ); ?>
<ul class="acapellas row">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php the_content(); ?>
<span class="download left"><?php get_attachment_icons($echo=true); ?></span>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php
} else {
wp_redirect( 'http://www.voclr.it/signup' ); exit;
}
?>
<?php get_footer(); ?>
Move your $paged variable outside the loop and include it as a parameter of WP_Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 100,
'paged' => $paged,
));

How to display from a post_type the first post and then to skip it in the next array

I'm trying to display posts from a post_type in a certain way: display the first post type withing a query then in the next query to skip that first post and show the second post and so on.
I've tried to do this with offset, the first post shows but the next one doesn't show. Is there a more elegant way to achive this?
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 2);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
for starters - its healthy to use the wp_reset_postdata when you have multiple wp_queries on one page.
in terms of your question. the first post type doesnt need an offset and your second one should offset 1 (minus the first)
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'posts_per_page' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="tableCell">
<?php
$args2 = array( 'post_type' => 'service', 'offset' => 1);
$loop2 = new WP_Query( $args2 );
while ( $loop2->have_posts() ) : $loop2->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>

Resources