Here's the website I'm talking about. If you scroll, when you reach the bottom you'll notice a link to go on with the page navigation (Pagina successiva). If you click it you'll get the same posts as the ones in home page! And it goes on. If you visit website.com/page/6/ you still get the home page posts.
Here's the index.php. What's wrong with it? :o
P.S.: Wordpress latest version. No child theme. Custom one I created:
<?php get_header(); ?>
<?php if (is_home() && !is_paged()) { ?>
<!-- editoriale -->
<div id="editoriale">
<?php $my_query = new WP_Query('cat=10&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<h2><?php the_title(); ?></h2>
<div class="postinfo" style="margin-top:-10px">
di <?php the_author(); ?> -
pubblicato il <?php the_time('j F Y') ?> -
<?php comments_popup_link('nessun commento »', '1 commento »', '% commenti »'); ?>
<?php edit_post_link('modifica',' - [ ',' ]'); ?>
</div>
<div id="editoriale-cont" class="entry">
<?php the_content(' Leggi il resto »'); ?>
</div>
<div class="postinfo" style="text-align:right;margin-top:10px">
Tutti gli editoriali »
</div>
<?php edit_post_link('Modifica', ' <div class="edit">', '</div>'); ?>
<?php endwhile; ?>
</div>
<!-- fine editoriale -->
<div class="hotnews" id="bombacalendario">
<div style="padding:10px 0 15px 10px">
<?php $my_query = new WP_Query( 'page_id=18570' );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate2[] = $post->ID;?>
<h3><?php the_title(); ?></h3>
<div><?php the_content(); ?></div>
<?php edit_post_link('Modifica', ' <div class="edit">', '</div>'); ?>
<?php endwhile; ?>
</div>
</div>
<div style="margin-top:12px;padding-bottom:6px" id="cerca" class="hotnews">
<h3>Cerca nel sito</h3><p>
<form action="<?php echo get_option('home'); ?>/" id="searchform" method="get">
<p><input type="text" style="width:135px;margin-right:10px" value="" name="s" id="s"><input type="submit" value="Vai" id="searchsubmit"></p>
</form>
</div>
<div class="fine-blocco"></div>
<?php } ?>
<?php get_sidebar(); ?>
<!-- inizio contenuto -->
<div id="content">
<!-- post del blog -->
<?php query_posts( 'cat=-7' );
if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="postinfo">
di <?php the_author(); ?> -
pubblicato il <?php the_time('j F Y') ?><?php edit_post_link('modifica',' - [ ',' ]'); ?> -
<?php comments_popup_link('nessun commento »', '1 commento »', '% commenti »'); ?>
</div>
<div class="entry">
<?php the_content('[Continua »]'); ?>
</div>
</div><br style="clear:both">
<!-- fine post -->
<?php endwhile; else: ?>
<p><?php 'Spiacente, nessun risultato.'; ?></p>
<?php endif;?>
<p class="navigation">
<?php posts_nav_link(' - ', '« Pagina Precedente', 'Pagina Successiva »'); ?>
</p>
</div>
<!-- fine contenuto -->
<?php get_footer(); ?>
As you can see visiting the site, in the home page there's an hardcoded page (which remains in every next page, as it should, and then a "featured" post which shouldn't appear in next page). Problem is everything is screwed when you go to page 2, 3, etc :D
The reason the pagination is not working is that you have not added the correct parameters to the query_posts function.
You have this:
query_posts( 'cat=-7' );
But you need to add the paged parameter too. You may also want to add posts_per_page
See the Wordpress codex: http://codex.wordpress.org/Function_Reference/query_posts#Pagination
Also see this for getting the paged parameter: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
Here is an example:
$args = array(
'cat' => '-7',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
);
query_posts($args);
Related
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; ?>
I created a page template for join all posts from a specific post_type in one page, everything is working except the pagination. It show the right number of posts for page but, in this case, I have 8 posts and it only show 5. I did some changes but without success. Any ideas?
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div class="container">
<main id="main" class="container" role="main">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('paged' => get_query_var('paged'), 'posts_per_page'=>5, 'post_type'=>'our-work', 'order' => 'ASC'))
?>
<br><br><br>
<!--header-->
<div class="page-header">
<img src="www.wtk.com/img/3428245.png">
</div>
<div class="inform">
Display text here
</div>
<br>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-title">
<h3><?php the_title(); ?></a></h3>
</div>
<div class="news-box">
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Read more →
</div>
<br><br>
<?php endwhile;?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_footer(); ?>
I can see in your code you have 'posts_per_page' => 5. That would explain why you are seeing only 5 posts on the page.
I have WordPress onepage and I displayed posts from specific category in one of page.
When I click to link with permalink href, I am redirecting to home page with added /post-name/ to url, but no to post page. I have index.php and single.php.
I have this index.php:
<?php
query_posts(array(
'post_type' => 'page',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order'
));
$tpl_parts = array(
'5' => 'about',
'7' => 'team',
'76' => 'tech',
'81' => 'services',
'101' => 'contact',
);
?>
<?php get_header('home'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if(array_key_exists($post->ID, $tpl_parts)) : ?>
<?php get_template_part('template-parts/'. $tpl_parts[$post->ID], 'template'); ?>
<?php else: ?>
<section id="<?php echo $post->post_name; ?>">
<div class="container">
<div class="row">
<?php the_content(); ?>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; else : ?>
<?php endif; ?>
<?php get_footer(); ?>
This code show all pages in index.php by template part, it's working.
When I added to services page, a few posts like this:
<section id="services" class="services-section">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-left">Services</h2>
</div>
</div>
<?php $inner_query = new WP_Query( 'category_name=services' ); ?>
<?php if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
<div class="row box-service">
<div class="col-sm-6 col-xs-12">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<div class="col-sm-6">
<h3><?php the_title(); ?></h3>
<p class="intro"><?php echo the_field('short_caption'); ?></p>
More
</div>
</div>
<?php endwhile; else: endif; wp_reset_postdata(); ?>
</div>
</section>
This code isn't working, becouse when I want to click on link and go to post, website is refreshing with url localhost/mywebsite/name-of-post/ but I want to redirect to post page. I have a single.php file:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endwhile; else: endif; ?>
<?php get_footer(); ?>
What's wrong ? How can I fix that ? My theme ignore files like page.php or single.php
Thanks for help.
Are You trying to use Wp_Query to single.php too ?
And maybe try to change posts_per_page in top index to number of your pages instead of -1.
you can try this for display single post page.
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
</h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
Ok, after a few hours spending on this problem, I tried to find why page.php and single.php is ignored.
Polylang plugin - after turned it off. Everything working fine.
Thanks everybody for help me.
Not sure if this pertains specifically to Advanced Custom Fields, but I'll keep this short. I'm relatively new to PHP and want to know the best way of approaching this task. Also, this is a WordPress (4.1.1) environment.
I'm sending a query for all post types with an associated category type to pull in and display. If the query can find posts, it produces an <article> block with additional html for each result. Not to complicated right?
Now, I want to create a pagination element if/when the query produces greater than 8 results.
Environment with the front-end of the query results can be found:
http://test-hdwg.pantheon.io/news/
Template being utilized for this page (news.php) contains the following:
<?php /* Template Name: News */ ?>
<?php get_header(); ?>
<main id="primary" class="content-area">
<div class="jumbotron">
<div class="row">
<div class="intro">
<p><?php the_field('news_heading') ?></p>
</div>
</div>
</div>
<div class="news">
<div class="row news-articles">
<div class="column-wrapper">
<div class="small-4 columns news-left-column">
<div class="select-dropdown">
<button href="#" data-dropdown="dropdown-items" aria-controls="dropdown-items" aria-expanded="false" class="dropdown">Select a news category</button>
<ul id="dropdown-items" data-dropdown-content class="f-dropdown" aria-hidden="true" tabindex="-1">
<?php $args = array(
'exclude' => '',
'title_li' => __( '' ),
'show_option_none' => __( '<li>No categories</li>' ),
'taxonomy' => 'category',
'child_of' => 2,
'current_category' => 0
); ?>
<?php wp_list_categories($args); ?>
<li class="cat-item cat-item-all-news">All News
</ul>
</div>
<div class="news-block">
<span class="news-block-heading">Email Newsletters</span>
<div class="news-inner-block">
<?php if( have_rows('newsletters_list') ): ?>
<?php while( have_rows('newsletters_list') ): the_row(); ?>
<span class="year"><?php the_sub_field('newsletter_year'); ?></span>
<?php if( have_rows('newsletter_resource') ): ?>
<ul>
<?php while( have_rows('newsletter_resource') ): the_row(); ?>
<li><?php the_sub_field('newsletter_title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="small-8 columns news-right-column">
<?php query_posts( 'post_type=post&cat=' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article class="large-6 columns ui-article-block">
<h3><?php echo get_the_date( 'd M Y' ); ?></h3>
<hr/>
<h2><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></h2>
<h1><?php the_title(); ?></h1>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 175) . "..."; // set a character cut-off at 175 characters
?>
</p>
Read More
</article>
<?php endwhile; ?>
<?php else : ?>
<div class="large-8 columns">
<p class="lead">Well this is embarrassing. There are currently no news articles.</p>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>
</div>
</main>
<?php get_footer(); ?>
Just to be clear, this is not an issue, but more-so a request from more experience developers on how to approach a task utilizing Advanced Custom Field elements (if that matters).
Any help would be greatly appreciated!
EDIT
This edit is after clarification from OP on nature of the question.
The tutorial here, explains how to handle numeric navigation in posts at a beginner level.
Our blog page is supposed to look something like this: http://livedemo00.template-help.com/wordpress_33821/?page_id=174
However it ends up looking like this - completely empty!
This is despite the fact that the page theme is set correctly and the template file contains the following code:
<?php
/**
* Template Name: Blog
*/
get_header(); ?>
<div class="box clearfix color2">
<div id="content" class="three_fourth">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<h2><?php the_title(); ?></h2>
<div class="post-meta">
<div class="fleft">Posted in: <?php the_category(', ') ?> | <time datetime="<?php the_time('Y-m-d\TH:i'); ?>"><?php the_time('F j, Y'); ?> at <?php the_time() ?></time> , by <?php the_author_posts_link() ?></div>
<div class="fright"><?php comments_popup_link('No comments', 'One comment', '% comments', 'comments-link', 'Comments are closed'); ?></div>
</div><!--.post-meta-->
</header>
<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>'; ?>
<div class="post-content">
<div class="excerpt"><?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,50);?>Read more</div>
</div>
</article>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav class="oldernewer">
<div class="older">
<?php next_posts_link('« Older Entries') ?>
</div><!--.older-->
<div class="newer">
<?php previous_posts_link('Newer Entries »') ?>
</div><!--.newer-->
</nav><!--.oldernewer-->
<?php endif; ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div><!--#content-->
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
Unfortunately this theme doesn't have any documentation so I'm forced to find my own solution.
Actually I already faced this situation in WordPress.
Its not fault of your code. Might be WordPress Bug.
Try to change your page name once.
Like
<?php
/**
* Template Name: Blog_new
*/
?>
And login again and check if it is showing new custom page option or not.