Wordpress - List all posts (with proper_pagination) - wordpress

On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?
Thanks

You could create a new page template with this loop in it:
<?php
$paged = get_query_var('paged')? get_query_var('paged') : 1;
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
];
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>

For others who might be Googling this... If you have replaced the front page of your site with a static page, but still want your list of posts to appear under a separate link, you need to:
Create an empty page (and specify any URL/slug you like)
Under Settings > Reading, choose this new page as your "Posts page"
Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).

A bit more fancy solution based on #Gavins answer
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>

Related

Blog posts won't open on my custom WordPress theme

This could be a dumb question, but I just can't figure it out. I'm working on a custom WordPress theme I created by converting a html website. But when I use WP_Query, it doesn't load my blog posts. Yes, it displays a list of blog posts as it should with the loop, but when I click on each title or the 'read more' link of a post, it only changes the url in the address bar to the link of the post and then simply refreshes the hope page. It doesn't open I'm really tired. Here's the query loop in my index.php
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
//define args for query
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '30',
);
//The main query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt(); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div><!-- blog-main-content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Please what do I do
Based on your comment above, you are missing a template file for your posts page. In your current configuration, the posts loop will be displayed on all pages of your website since you only have an index.php file. You should create a single.php template to display your single blog posts.
Here is the WordPress template hierarchy for reference: https://developer.wordpress.org/themes/basics/template-hierarchy/

Wordpress highlight recent posts in front page

I'm trying to highlight the three most recent posts in the home page with bigger cover images and an excerpt so that if they're highlighted they don't appear on the smaller post list. Something like this, but with the most recent posts on top:
I've tried using multiple WP_Query with an offset but it doesn't seem to quite work (it's possible I'm not doing it right).
Any ideas? Thanks!
EDIT: Here's what I tried. The problem with this is that I get "undefined offset" errors.
<?php
/* Featured posts */
?>
<div class="featured-posts columns">
<?php
$featured_query = new WP_Query( array( 'posts_per_page' => 3) );
while ( $featured_query->have_posts() ) : the_post(); ?>
<div class="featured-post col-4">
<?php dge_post_thumbnail(); ?>
<div class="entry-meta">
<?php
dge_posted_on();
?>
</div><!-- .entry-meta -->
<h2>
<?php the_title(); ?>
</h2>
<p><?php the_excerpt(); ?></p>
<p>continue reading</p>
</div>
<?php endwhile; ?>
</div>
<?php
/* Start the Loop */
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
while ( $query->have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
/*get_template_part( 'template-parts/content', get_post_type() );*/
get_template_part( 'template-parts/preview-content');
endwhile;
?>
Try this script:
<?php
$featured_query = new WP_Query( array( 'posts_per_page' => 3,"orderby"=>"date","order"=>"DESC") );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<p>'.the_title().'</p>';
endwhile;
?>
<?php
echo '<hr/><h1>After offset set</h1> <br/>';
$featured_query = new WP_Query( array( 'posts_per_page' => 5,"orderby"=>"date","order"=>"DESC","offset"=>3) );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<p>'.the_title().'</p>';
endwhile;
?>
Response:

wordpress blog homepage customization

please don't give a negative feed like always and instead tell me the mistake.
I have a blog.
can i make the homepage display a summary of an article instead of the whole article?
if yes then where or how can i do it?
I have this pre-set in one of my WordPress sites. Here is what this looks like and it is in Appearance/Editor/Posts Page (home.php)
<div class="post-content">
<?php $content = get_the_content(); ?>
<?php echo wp_trim_words(strip_tags($content), 30); ?>
</div>
<a class="blog-post-read-more" href="<?php echo esc_url( get_the_permalink( get_the_ID() ) ); ?>"><?php echo esc_html( get_theme_mod( 'relia_blog_read_more', __( 'Read More', 'relia' ) ) ); ?></a>
</div>
So what this does is it will strip the words up to 30, wp_trim_words. And below that is the how to insert Read More.
Here are some links for you to check out:
https://codex.wordpress.org/Excerpt
https://codex.wordpress.org/Customizing_the_Read_More
Create one custom template that template you can assign your home page
/*
Template Name: Blog
*/
query_posts( array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) ) );
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format()); ?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
wp_reset_postdata();

Wordpress displays posts of category ID

Can't seem to find the right answer for what I thought would be trivial.
I have some categories arranged like this...
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.
This is what I am doing right now:
<?php
query_posts('cat=2&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).
Any suggestions?
Thanks in advance. (SO Community = Awesome Sauce).
try below code:
<?php
$current_cat_id = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
If you are using category.php, you can omit the query_posts and it will auto fill in the posts for you.
<?php
// Start the loop.
$categories = get_categories('child_of=1');//Parents category id
foreach ($categories as $cat) {
$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;//parents sub category name
$option .= '</a>';
echo $option;
query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; }?>
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => 1 );
// 1 is a cat id.
//
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
try
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );
Try this, this is better solution for this and you can also use it to show Related Post by a category id...
Just call the function that mentioned below by using this line. Put this in your template or page.php/single.php file.
Call by put this line: related_post_title('enter cat id here..');
Here is the Function and put this in function.php file.
Related posts function:
function related_post_title($cat_id){
$cat = $cat_id;
// Check if it is page only
if ( is_page() || is_single()) {
$args=array(
'cat' => $cat,
'order' => DESC,
'orderby' => rand,
'post__not_in' => array($post->ID),
'posts_per_page' => 9999,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ' <ul> ';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
If any query please let me know, i will help you...

Wordpress: single.php shows the newest post only

I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
It displays - no matter what post (&p=111 e.g.) you select - only the content of the newest post.
This is my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="page-title"><?php the_title() ?></h1>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="cover" />
<?php endif; ?>
<p class="page-text">
<?php the_content(); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
What could be wrong? I hope you've understood my problem. Thank you!
edit:
I recently updated the header file. When I delete this loop, it works fine:
<ul class="nav-dropdown">
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 5,
'exclude' => '1,2,3,4,5,6,8,9,10,11,12,13,14'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
$post_args = array(
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<li class="nav-dropdown"><?php the_title(); ?></li>
<?php
}
}
?>
</ul>
I'd change your variable names in the header as ones such as $post are reserved by Wordpress for handling single post pages.
I am not sure but please change the $post variable to any other variable and than try
may be your problem be solved.
Because $post is global variable of post.

Resources