I'm using stanleywp theme for WordPress to display some post, actually I've inserted 20 posts, but it shows only 10posts.
This is what I did:
Portfolio -> Portfolio categories -> I added "brands" category
Portfolio -> add new -> and I've added 20 posts and set "brands" category
when I go to .../portfolio-category/brands/ I should see all my 20 posts, but I see only 10.
How can I solve that?
This is taxonomy-portfolio_cats.php:
<?php
/**
* #package WordPress
*/
?>
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<div class="container pt">
<div class="row mt">
<div class="col-lg-6 col-lg-offset-3 centered">
<h1><?php echo single_term_title(); ?></h1>
<hr>
<?php if(category_description()) { ?>
<?php echo category_description( ); ?>
<?php } ?>
</div>
</div>
<div class="row mt centered">
<?php $cont = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-lg-4">
<?php if ( has_post_thumbnail()) : ?>
<a class="zoom green" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<?php if(bi_get_data('project_title', '5')) {?>
<p><?php the_title(); ?></p>
<?php } ?>
</div> <!-- /col -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
I think it's due to the default pagination (10 posts per page): you could increase the value on Settings > Reading > Blog pages show at most, or insert the pagination after <?php wp_reset_query(); ?>, something like:
<?php
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'your-textdomain' ),
'next_text' => __( 'Next page', 'your-textdomain' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'your-textdomain' ) . ' </span>',
) );
?>
Or if you want to change the number of posts per page only for that taxonomy archive you can use the pre_get_posts action:
function portfolio_cats_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( is_tax( 'portfolio_cats' ) ) {
// Display all posts in one page
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'portfolio_cats_posts_per_page', 1 );
Related
Pagination template part includes common pagination function with style. The template part works for archive.php (it's for "single", you know default wp file) but doesn't work for custom post type.
Why not? How to solve it?
<?php get_header(); ?>
<main role="main">
<!-- section -->
<?php get_template_part( 'breadcrumb' );?>
<!-- Inner Pages Main Section -->
<section class="ulockd-service-details">
<div class="container">
<div class="col-md-12">
<div class="row">
<?php
/**
* Setup query to show the ‘services’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
if (have_posts()): while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php //if (have_posts()): while (have_posts()) : the_post(); ?>
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
?>
<div class="col-md-12 ulockd-mrgn1210">
<div class="ulockd-project-sm-thumb">
<img class="img-responsive img-whp" src="<?php printf( '%s', esc_url($image_src[0]) ); ?>" alt="">
</div>
</div>
<?php
}
?>
<div class="col-md-12 ulockd-mrgn1210">
<article class="ulockd-pd-content">
<div class="ulockd-bp-date">
<ul class="list-inline">
<li class="ulockd-bp-date-innner">On <span class="text-thm2"><?php the_time('j'); ?></span> / <?php the_time('F Y') ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-nurse-head text-thm1"></span> <?php the_author_posts_link(); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-chat text-thm1"></span> <?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></li>
<li class="ulockd-bp-comment"><span class="flaticon-black-check-box text-thm1"></span> <?php the_category(); ?></li>
</ul>
</div>
<h3><?php the_title(); ?> </h3>
<p class="project-dp-one"><?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?></p>
<a class="btn btn-lg ulockd-btn-thm2" href="<?php the_permalink(); ?>"> Read More</a>
</article>
</div>
<?php get_template_part('pagination'); ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>
</div></div></div></section>
<?php get_footer(); ?>
</main>
First of all, you don't need to include that template inside while loop. That's wrong.
Then, if you want to have an archive page for your team post type, you need to provide 'has_archive' => true within register_post_type() function args.
Also, consider changing archive page default slug if needed. If you do so, you need to open Settings > Permalinks for resetting your permalinks structure.
Then you could either use standard archive.php for the whole team archive page and standard template-parts/content.php for one post inside the loop or rewrite either of those by creating archive-team.php or content-team.php. And the_posts_pagination() function will work on proper archive page (either archive.php or archive-team.php).
I would try two things.
Add the argument paged to your wp_query args :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
);
Put your pagination template outside the loop :
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
I'm having a problem with displaying all posts in my archive page using the have_posts loop. When I try to return the title and the content, it returns the template page's title and content instead of the posts' content.
What I did to try and fix it is I ran a WP_Query post loop which grabs all the posts of post type = post and it displays the content just fine. The only problem is I want a dynamic archive page so when users select a tag - it will take them to an archive page with all the posts containing that tag. Since I am running a specific WP_Query, all my archive pages display all the posts with the post-type = post.
My guess is that I have to run a simple have_posts loop instead of running WP_Query in order for my archive page to dynamic.
How I set up my archive page: I have a file called archive.php which I gave the template name of Archives and created a page for it on wp-admin and titled it Blog.
<?php
/**
* The template for displaying archive pages
* Template Name: Archives
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Seventeen
* #since 1.0
* #version 1.0
*/
get_header(); ?>
<div class="d-flex flex-wrap archive-wrap">
<div class="content-area">
<main id="main" class="site-main d-flex justify-content-center flex-wrap" role="main">
<?php // Show the selected frontpage content.
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="archive-header col-10 d-flex justify-content-center align-items-center flex-column">
<?php
the_title( '<h1 class="archive-title">', '</h1>' );
the_content( '<h1 class="archive-title">', '</h1>');
?>
</div><!-- .page-header -->
<?php
get_template_part( 'template-parts/post/content', 'archive' );
endwhile;
the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
) );
endif; ?>
<div class="col-10 col-md-3 offset-lg-1 sidebar-4-container">
<?php dynamic_sidebar( 'sidebar-4' ); ?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
<?php get_footer();
Inside my template content archive file I have the following:
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post',
'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="col-12 article-container">
<div class="article-image-div" style="background-image: url('<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?> ');"></div>
<h3><?php the_title(); ?></h3>
<h5>by <?php echo get_the_author(); ?></h5>
<?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>
<hr align="left">
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Try to use Query_posts or get_posts instead of
https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/functions/query_posts/
I have http://spainthephilippines.com/ where I use Rift Theme. Im having problem with showing the sidebar on single post page. On my index page the sidebar is showing, on the 404 page the sidebar is showing. It is working anywhere I use it just not in signle post page. I was looking 2 hours for answer but I cant find it. The theme is on version 1.0, so is it possible that to be the problem? Then why the sidebar shows on most pages? Anyone can help? Tell me if you need any code.
<?php global $theme; get_header(); ?>
<div id="main">
<?php $theme->hook('main_before'); ?>
<div id="content">
<?php $theme->hook('content_before'); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
/**
* Find the post formatting for the single post (full post view) in the post-single.php file
*/
get_template_part('post', 'single');
endwhile;
else :
get_template_part('post', 'noresults');
endif;
?>
<?php $theme->hook('content_after'); ?>
</div><!-- #content -->
<?php get_sidebars(); ?>
<?php $theme->hook('main_after'); ?>
</div><!-- #main -->
EDIT
Here is the sidebar:
<?php global $theme; ?>
<div id="sidebar-primary">
<?php
if(!dynamic_sidebar('sidebar_primary')) {
/**
* The primary sidebar widget area. Manage the widgets from: wp-admin -> Appearance -> Widgets
*/
$theme->hook('sidebar_primary');
}
$theme->hook("sidebar_primary_after");
?>
And also the post-single.php:
<?php global $theme; ?>
<div <?php post_class('post post-single clearfix'); ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><?php the_title(); ?></h2>
<div class="postmeta-primary">
<span class="meta_date"><?php echo get_the_date(); ?></span>
<span class="meta_categories"><?php the_category(', '); ?></span>
<?php if(comments_open( get_the_ID() )) {
?> <span class="meta_comments"><?php comments_popup_link( __( 'No comments', 'themater' ), __( '1 Comment', 'themater' ), __( '% Comments', 'themater' ) ); ?></span><?php
}
if(is_user_logged_in()) {
?> <span class="meta_edit"><?php edit_post_link(); ?></span><?php
} ?>
</div>
<div class="entry clearfix">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail(
array($theme->get_option('featured_image_width_single'), $theme->get_option('featured_image_height_single')),
array("class" => $theme->get_option('featured_image_position_single') . " featured_image")
);
}
?>
<?php
the_content('');
wp_link_pages( array( 'before' => '<p><strong>' . __( 'Pages:', 'themater' ) . '</strong>', 'after' => '</p>' ) );
?>
<?php if(get_the_tags()) {
?><div class="postmeta-secondary"><span class="meta_tags"><?php the_tags('', ', ', ''); ?></span></div><?php
}
?>
<!-- Post ID <?php the_ID(); ?> wtf-->
<?php
if(comments_open( get_the_ID() )) {
comments_template('', true);
}
?>
In my wordpress I have many posts, every page I am showing 5 posts.
Bottom of my page I have next and prev button. When I click one the next button it will go to /page/2/ link but this page title is showing Page not found. And it's not showing other posts in page 2.
My next and prev code :
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
My index.php code :
<div class="center-content">
<ul>
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else {
?>
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
You do have a few issues here
NEVER EVER use query_posts. It is slow, reruns queries, breaks and fails silently with pagination and worse of all, it breaks the main query object. If you break the main query object, you break page functions. So, please never use query_posts. Mke if it never existed
You have replaced the main query loop with a custom query, which you must not do. If you need to show a different amount of posts on a particular page ( not on page templates and a static front page though as this will not work there ), then use pre_get_posts. You need to go and read how helpful that action is and how to use it
Remove this line
<?php query_posts('posts_per_page=5'); ?>
Then add the following in your functions file
add_action( 'pre_get_posts', function ( $query )
{
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
});
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
Just add below code: I added 'paged' variable and set in next_posts_link() and previous_posts_link()..please see below code:
<div class="center-content">
<ul>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
global $wp_query;
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else { echo 'test';
?>
<div class="prev-next-btn">
<?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<div class="prev-next-btn">
<?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
I'm currently developing my own wordpress theme and so far so good. The problem is I'm using a permalink and a category. The category is inside the permalink. The following code is what I'm using (content.php):
<?php if ( has_tag ('half_half') ) : ?>
<div class="gridHalves">
<article class="project" >
<a class="projectLink" href="<?php the_permalink(); ?>">
<div class="projectwrapper">
<div class="projectOverlayInset">
<div class="projectOverlay">
<div class="projectInfo">
<h4 class="categorie"><?php the_category(', '); ?></h4>
<h2 class="onderwerp"><?php the_title(); ?></h2>
<p class="writer">Written by <strong><?php the_author(); ?></strong></p>
</div>
</div>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</div>
</a>
</article>
</div>
<?php endif; ?>
The following is happening when I leave the_category in there:
If i leave the_category tag out everything is working fine. I've already checked the function in wp-includes of the_category but couldn't find anything about the permalink. So any suggestions? My loop is the following:
<?php get_header(); ?>
<main id="projectengrid" class="gridmenu-closed">
<section class="gridRightSmall">
<?php if ( have_posts() ) : ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</section>
</main><!-- .site-main -->
<?php get_footer(); ?>
I left the post navigation in there because I want to use it later on.
Hope someone knows the solution to this! Seems like I'm having some nested loops in there!
Kind regards,
Wouter
If you need any additional info please let me know!