Pagination on WordPress posts within page - wordpress

I'm trying to do a custom archive for all posts, but I want it to look a little different than the category-specific archives. I've achieved this so far by placing the code below into a page on my site.
Is it possible to add pagination to something like this? I thought that 'paged' => $paged line might do it, but no such luck.
Here's my code: (I'm using a custom thumbnail size if you were wondering what that refers to.)
<?php
global $post;
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'paged' => $paged
);
$thumbnails = get_posts($args);
foreach ($thumbnails as $post)
{
setup_postdata($post);
?>
<div class="featuredarticle">
<h4 class="entry-title"><?php the_title(); ?></h4>
<div class="featuredimage">
<?php the_post_thumbnail('featured'); ?><br />
</div>
</div>
<p><?php the_excerpt(); ?></p>
<div class="entry-utility">
<span class="read-more">Read More</span>
</div>
<?php
}
?>

This code works very good within a page querying posts and use pagination.
<?php
/**
* Template Name: Page of Books
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_content();
endwhile; wp_reset_query();
?>
<div id="container">
<div id="content">
<?php
$type = 'book';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php
get_template_part( 'loop', 'index' );?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Related

Wordpress page shows content from the previous loop (wp_reset_postdata not working?)

I am rather new to Wordpress, and I can't really get what is going wrong in my example.
In my template I have page-standard.php which has a Slider in the Header working by means of the following code:
<div class="slider">
<div class="slider__wrapper">
<?php
$posts = get_posts( array(
'numberposts' => -1,
'category_name' => 'slidertop',
'orderby' => 'date',
'order' => 'ASC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
<!-- ! Here goes SLIDE content -->
... <!-- ! End of SLIDE content -->
<?php
}
wp_reset_postdata();
?>
And it works fine. But then I need to show contents of the page and make another wp loop on the same page this way:
<?php
/*
Template Name: Standard Page
*/
get_header();
?>
<section class="blog-posts-section">
<div class="container">
<?php
while( have_posts() ) : the_post();
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
?>
</div>
</section>
<?php
get_footer();
?>
content.php looks as follows:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<div class="entry-content">
<?php
the_content();
?>
</div>
</article>
So it should be showing the contents of the page which has the Page Standard template.
But instead it shows the contents from the SLIDER which it got in the very first wp loop. It seems like wp_reset_postdata() is not working properly. I used the same approach before, but just now it started to show this kind of behaviour.
I've tried using wp_reset_query() as well, but no effect.
Just use proper methods provided by Wordpress to run an extra query instead of $posts = get_posts. The code should look like that:
<?php
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
and then your standard query. It's all described in details in here:
https://developer.wordpress.org/reference/classes/wp_query/

Wordpress, inserting URL in a shortcode

I'm trying to display/insert a url into the shortcode below:
<?php echo do_shortcode('[sc_embed_player fileurl="http://www.example.com/wp-content/uploads/my-music/mysong.mp3"]'); ?>
I'm using a ACF field called "add_audio" which is a mp3 URL.
I used the default get field ( <?php the_field('field_name'); ?> )
code to call the the url.
I made a variable too and called ($variable) too.
But nothing worked!!
Here's the error i get
"Prase error:syntax error, unexpected 'add_audio'(T_STRING),
expecting,''or')'in
/app/public/wp-content/oxygen/component-framework/components/layouts/code-block.php(33):eval()'d
code on line 43"
I used Oxygenbuilder plugin and ACF plugins.
<?php
/*
* Easy Query Shortcode
* [easy_query container="div" template="template_906184" posts_per_page="20" post_type="audio_files"]
*/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('audio_files'),
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 20,
'paged' => $paged,
);
// WP_Query
$eq_query = new WP_Query( $args );
if ($eq_query->have_posts()) : // The Loop
$eq_count = 0;
?>
<div class="wp-easy-query paging-style-default grey">
<div class="wp-easy-query-posts">
<div>
<?php
while ($eq_query->have_posts()): $eq_query->the_post();
$eq_count++;
?>
<div id="div_block-17-25" class="ct-div-block audio-div">
<h1 id="headline-18-25" class="ct-headline">
<?php the_title(); ?></h1>
<div id="div_block-20-25" class="ct-div-block">
<?php echo do_shortcode('[sc_embed_player_template1 fileurl="<?php the_field('add_audio'); ?>"]'); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<?php include(EQ_PAGING); ?>
</div>
<?php endif; ?>
Simply Try Removing from your Code write it as follows:
<?php echo do_shortcode('[sc_embed_player_template1 fileurl="'.the_field('add_audio').'"]'); ?>

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:

How to show Previous and Next link in WordPress

Below is my example code that is dynamically get the posts.
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
The code above will works correctly but my question is, since this is not a default blog page or a category, how can I use the posts_nav_link() so that I can still access the rest of the pages? I tried to used it but it doesn't work unless if the current page is a category. Hope you guys can help me this.
If you giving you paging in your custom post type. then i think you can do very simple you have to use wordpress plugin like wp-pagenavi after then add your custom post type in this plugin in admin panel after then add
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
Without plugin you can use like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, 'cat' => '-10, -72&paged=' . $paged) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?> <?php the_title(); ?>
<span><?php the_time('d.m.y') ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination

Resources