Pagination in Wordpress home.php - php

Pagination isn't working in a Wordpress blog. I've tracked the specific file responsible, to home.php.
The original blog page displays posts fine (10 at a time), however as mentioned pagination isn't working.
<?php
/**
Category Page
*/
get_header(); ?>
<div class="container-fluid blogs post-section" id="blogs">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-9 col-lg-8">
<?php echo category_description( $category_id ); ?>
<div class="category-posts">
<?php
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => "10",
"orderby" => "date",
"order" => "DESC"
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-12">
<div class="inner-post">
<?php if ( has_post_thumbnail() ) { ?><div class="thumbnail col-sm-4" style="background:none!important;"><?php the_post_thumbnail(); ?></div><?php } ?>
<div class="abso custom_abso col-sm-8"><div class="inner-box">
<div class="date">
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('M');
$archive_day = get_the_time('d');
?>
<span class="month"><?php echo $archive_month; ?></span> <span class="day"><?php echo $archive_day; ?>,</span><span class="year"><?php echo $archive_year; ?></span>
</div>
<h2 style="margin-top:0;"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<p><a href="<?php the_permalink(); ?>" class="readmores">Read More <i class="fa fa-caret-right" aria-hidden="true"></i>
</a></p>
</div>
<?php endwhile; endif;
?></div>
<div class="pagination text-center">
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_sidebar(); ?>
</div></div></div>
<?php get_footer(); ?>
Based on this Wordpress documentation, I've updated the top of the file, to look like the following:
<div class="category-posts">
<?php $args = array(
'posts_per_page' => 10,
'offset' => 0,
'cat' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$posts_array = get_posts( $args ); ?>
<div class="col-sm-12">
This has worked, in that the pagination buttons work, however, only one post is displayed per page:
Any advice on how to display 10 posts per page?

Please add the following parameter 'paged'. It sets pagination query variable. The pagination is works as per this parameter.
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
to the $args.

Related

Pagination wordpress does not display

I have coded my theme from base and I have a problem displaying pagination in my page.
I have a foreach over my posts that is working properly and it is showing 9 posts in every page, but I could not display paginations below that to switch between page.
It would be great if someone can help me with it.
<div class="row">
<?php
$args = array(
'posts_per_page' => 9,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?= get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach (get_the_category($post->ID) as $category) {
echo '' . $category->name . '';
} ?> |
<span>
مدت زمان مطالعه
<?= get_field('duration'); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php endforeach; ?>
<?= previous_posts_link()?>
</div>
It does not display with any of these built in functions
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
The functions you mention:
<?= paginate_links() ?>
<?= the_posts_pagination() ?>
<?= get_the_posts_pagination() ?>
All require that the global $wp_query variable contains the necessary fields to generate the pagination (namely found_posts, paged, and posts_per_page).
Since you're using get_posts, the fields in $wp_query will never be set.
Instead, you can overwrite the $wp_query variable with a WP_Query instance which means the fields will be set. It is good practice to then restore $wp_query after your loop in case you have any data later in the page or in the footer that relies on it.
Example:
global $wp_query;
$args = array(
'posts_per_page' => 9,
// 'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
// this one is important, it lets the query object know what page you're on.
'paged' => get_query_var( 'paged' ),
// are you sure you want to suppress filters ? make sure you know what this does.
'suppress_filters' => true,
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div class="col-md-4 col-sm-12 mt-3">
<div class="bg-gray h-100 blog-cards">
<a href="<?php echo get_the_permalink(); ?>" title="more">
<div>
<div class="archive-img">
<?php echo get_the_post_thumbnail(); ?>
</div>
<h3 class="upper mb-0 px-3 pt-3 pb-0 text-dark"> <?php echo get_the_title(); ?></h3>
<div class="p-3 pt-0">
<?php the_excerpt(); ?>
<?php
global $post;
foreach ( get_the_category( get_the_ID() ) as $category ) {
echo '' . $category->name . '';
}
?>
|
<span>
مدت زمان مطالعه
<?php echo get_field( 'duration' ); ?>
دقیقه
</span>
</div>
</div>
</a>
</div>
</div>
<?php
endwhile;
the_posts_pagination();
// restore the original $wp_query.
wp_reset_postdata();

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

Displaying a specific category in a WP loop

On my WordPress website, I created a page in which I display 9 posts of the category "interviews". Then there is a button ("Charger plus d'interviews") to click if you want to see more posts of the category.
There is a problem in the loop, because when I click on it, it displays posts from all the other categories.
What shall I do to display only the "interviews" ?
<section class="blocs" id="home_article_derniers">
<div class="iso-container">
<?php
$post_home_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'interview',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'paged' => 2
));
if($post_home_query->have_posts()){
while ( $post_home_query->have_posts() ) {
$post_home_query->the_post();
get_template_part('loop-post', 'actus');
}
wp_reset_postdata();
} ?>
</div>
</section>
<div class="separator">
<a class="btn btn-default more-article" data-paged="1" data-post-not-in="<?php echo implode(',', $array_post_not_in) ?>">Charger plus d'interviews »</a>
?>
</div>
This is the code of the loop :
<?php $color = wami_get_first_category_color(get_the_ID()); ?>
<article data-id="<?php the_ID(); ?>" class="post post-actu iso-item bloc-infinity <?php echo (isset($phone) ? $phone : '' ) ?> ">
<a href="<?php the_permalink(); ?>">
<div class="post-header">
<?php
if(has_post_thumbnail()):
the_post_thumbnail('actu-hp-small');
endif;
?>
<div class="overlay">
<div class="img"></div>
<div class="background" style="background-color:<?php echo $color; ?>">
</div>
</div>
<div class="post-header-title">
<?php wami_the_first_category(get_the_ID(), false, $color, true); ?>
</div>
</div>
<div class="post-body">
<?php
the_title( '<h2>', '</h2>' );
/*the_excerpt();*/
?>
</div>
</a>
</article>
Here's the function
function wami_load_more_posts(){
if ( isset($_REQUEST) ) {
$paged = $_REQUEST['paged'];
$post_not_in = explode(',', $_REQUEST['post_not_in']);
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => $post_not_in,
'post_status' => 'publish',
'paged' => $paged
);
if(isset($_REQUEST['category_in'])){
$args['category__in'] = $_REQUEST['category_in'];
}
$homeblog_query = new WP_Query($args);
if( $homeblog_query->have_posts() ):
while($homeblog_query->have_posts()):
$homeblog_query->the_post();
get_template_part('loop-post', 'actus');
endwhile;
endif;
wp_reset_postdata();
}
die();
}
add_action('wp_ajax_wami_load_more_posts', 'wami_load_more_posts');
add_action('wp_ajax_nopriv_wami_load_more_posts', 'wami_load_more_posts');

How do I show the first two posts only?

I am quit new to WordPress theme development. I am trying to show only the first two recent posts on my homepage but whatever I do the code always loops through the whole posts. Any help is appreciated.
index.php:
<?php get_header(); ?>
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args );
?>
<?php if(have_posts()) :
if(have_posts()) : the_post(); ?>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<h2><?php the_title() ?></h2>
<?php the_post_thumbnail('banner-image') ?>
</div>
<div class="col-md-4">
<h2><?php the_title() ?></h2>
<?php the_post_thumbnail('banner-image') ?>
</div>
<div class="col-md-4">
<div class="pull-right marTop20">
<h4>Most recent posts</h4>
<?php $recent_posts = wp_get_recent_posts($args); ?>
<?php foreach($recent_posts as $recent){ ?>
<ul class="list-unstyled">
<?php echo '<li class="list-item">' . $recent["post_title"].' </li> '; ?>
</ul>
<?php } ?>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<p><?php echo get_the_excerpt(); ?>
Read more</p>
</div>
<div class="col-md-4">
<p><?php echo get_the_excerpt(); ?>
Read more</p>
</div>
</div>
</div>
</div>
<?php
endif;
endif;
?>
<?php get_footer(); ?>
Use this
$args = array (
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'posts_per_page' => 2,
'orderby' => 'post_date',
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
/* Your Code */
endwhile;
endif ;
You have to loop over the wp_get_recent_posts return value:
foreach($recent_posts as $recent_post) {
// stuff
}
Also, set in args the numberpost to 2.
Change Line 3 to:
'numberposts' => 2,
Here is the full documentation for the function: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

Why can't I pass a custom field variable to 'numberposts' variable in Wordpress array?

I'm going crazy here - I can't figure out why this doesn't work!
I have a wordpress template with multiple queries to show different post types.
I am able to do this no problem:
$showfeatposts = "1";
$args = array(
'numberposts' => $showfeatposts,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'cat' => '58',
'post_status' => 'publish',
'suppress_filters' => true
);
Which passses the value of "1" into the 'numberposts' variable.
What I NEED to do is instead replace that value of "1" with a value passed from a custom field in the admin panel so that the administrator can enter the number of posts they want to show.
When I change my code instead to this:
$showarticleposts = the_field('articles-posts-to-show');
$args = array(
'numberposts' => $showarticleposts,
'orderby' => "post_date",
'order' => 'DESC',
'post_type' => 'post',
'cat' => '5, -58',
'post_status' => 'publish',
'suppress_filters' => true
);
I get all posts returned, as if the value being entered is '-1'
I've verified that the actual value in the custom field is '2' by enchoing it on the page.
What am I doing wrong? Surely this should be possible?
For reference: Here's the page.
ETA: I've also tried this method from an ACF tutorial - still doesn't work:
$args = array(
'numberposts' => get_field('showarticleposts'),
'orderby' => "post_date",
'order' => 'DESC',
'post_type' => 'post',
'cat' => '5, -58',
'post_status' => 'publish',
'suppress_filters' => true
ETA2: In response to the answer below - I have also tried this option:
$showarticleposts = get_field('showarticleposts');
$args = array(
'numberposts' => $showarticleposts,
'orderby' => "post_date",
'order' => 'DESC',
'post_type' => 'post',
'cat' => '5, -58',
'post_status' => 'publish',
'suppress_filters' => true
);
And if I put this code on the page - it does echo the number '2':
<?php echo get_field('showarticleposts'); ?>
Per request - here is the full page of code - I have tried to clean it up as much as possible - you will undoubtably notice the weird coding for the reports section, I am taking this over from someone who used Types to create the custom fields and custom post types. But I am using ACF custom fields for my attempt to add the ability to choose how many of each post-type to show on the homepage:
section id="content" role="main" class="clearfix animated">
<?php
/**
* If Featured Image is uploaded set it as a background
* and change page title color to white
**/
if ( has_post_thumbnail() ) {
$page_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'big-size' );
$page_bg_image = 'style="background-image:url(' . $page_image_url[0] . ');"';
$title_with_bg = 'title-with-bg';
} else {
$title_with_bg = 'wrapper title-with-sep';
} ?>
<!--<header class="entry-header page-header">
<div class="page-title <?php echo isset( $title_with_bg ) ? $title_with_bg : ''; ?>" <?php echo isset( $page_bg_image ) ? $page_bg_image : ''; ?>>
<div class="wrapper">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
</div>
</header>-->
<div class="wrapper">
<div class="grids">
<div class="grid-8 column-1">
<?php
// Enable/Disable sidebar based on the field selection
if ( ! get_field( 'page_sidebar' ) || get_field( 'page_sidebar' ) == 'page_sidebar_on' ):
?>
<?php endif; ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
?>
<div class="page-content">
<?php the_content(); ?>
</div>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $showfeatposts = "1";
$args = array(
'numberposts' => $showfeatposts,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'cat' => '58',
'post_status' => 'publish',
'suppress_filters' => true
);
$featposts = get_posts( $args );
foreach( $featposts as $post ) : //setup_postdata($ppost);
setup_postdata( $post ); ?>
<div class="col-md-12 recent feat" id="recent">
<figure class="entry-image inview">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'rectangle-size-large' ); ?></a>
<?php } ?>
</figure>
<header class="entry-header">
<div class="entry-category">
<?php the_author_posts_link(); ?> / <?php the_time('F jS, Y'); ?> </div>
<h2 class="entry-title" itemprop="headline">
<?php echo $post->post_title; ?>
</h2>
</header>
<div class="entry-content">
<?php echo apply_filters("the_content", $post->the_excerpt) ; ?>
</div>
<div class="entry-category">
<span class="posted-on">By
<span class="author vcard"><?php the_author_posts_link(); ?></span></span>
</div>
</div>
<?php
$pnum++;
endforeach; ?>
</article>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-12 homehdr">
<header class="section-header">
<div class="title-with-sep">
<h2 class="title">Recent Articles</h2>
</div>
</header>
</div>
<?php $showarticleposts = "4";
$args = array(
'numberposts' => $showarticleposts,
'orderby' => "post_date",
'order' => 'DESC',
'post_type' => 'post',
'cat' => '5, -58',
'post_status' => 'publish',
'suppress_filters' => true
);
$artposts = get_posts( $args );
foreach( $artposts as $post ) : //setup_postdata($ppost);
setup_postdata( $post ); ?>
<div class="col-md-6 recent arts" id="arts">
<figure class="entry-image inview">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'rectangle-size' ); ?>
</a>
<?php } ?></figure>
<header class="entry-header">
<div class="entry-category">
<?php the_author_posts_link(); ?> / <?php the_time('F jS, Y'); ?></div>
<h2 class="entry-title" itemprop="headline">
<?php echo $post->post_title; ?>
</h2>
</header>
<div class="entry-content">
<?php if ($post->post_excerpt) the_excerpt(); else { ?>
<?php $content = apply_filters("the_content", $post->post_content);
$content = strip_tags($content);
echo substr($content, 0, 100); }
?></div>
<div class="entry-category">
<span class="posted-on">By
<span class="author vcard"><?php the_author_posts_link(); ?></span></span>
</div>
</div>
<?php
$pnum++;
endforeach; ?>
</article>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-12 homehdr">
<header class="section-header">
<div class="title-with-sep">
<h2 class="title">Recent Videos</h2>
</div>
</header>
</div>
<?php $showvideoposts = "2";
$args = array(
'numberposts' => $showvideoposts,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'cat' => '3, -58',
'post_status' => 'publish',
'suppress_filters' => true
);
$vidposts = get_posts( $args );
foreach( $vidposts as $post ) : //setup_postdata($ppost);
setup_postdata( $post ); ?>
<div class="col-md-6 recent vids">
<?php $video_embed = wp_oembed_get( get_post_meta( $post->ID, 'add_video_url', true ) ); echo '<figure class="video-wrapper">' .$video_embed. '</figure>'; ?>
<header class="entry-header">
<div class="entry-category">
<?php the_author_posts_link(); ?> / <?php the_time('F jS, Y'); ?> </div>
<h2 class="entry-title" itemprop="headline">
<?php echo $post->post_title; ?>
</h2>
</header>
<div class="entry-content">
<?php echo $post->the_content; ?>
</div>
<div class="entry-category">
<span class="posted-on">By
<span class="author vcard"><?php the_author_posts_link(); ?></span></span>
</div>
</div>
<?php
$pnum++;
endforeach; ?>
</article>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-12 homehdr">
<header class="section-header">
<div class="title-with-sep">
<h2 class="title">Recent Notes</h2>
</div>
</header>
</div>
<?php
$shownoteposts = "2";
$args = array(
'numberposts' => $shownoteposts,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'cat' => '42',
'post_status' => 'publish',
'suppress_filters' => true
);
$noteposts = get_posts( $args );
foreach( $noteposts as $post ) : //setup_postdata($ppost);
setup_postdata( $post ); ?>
<div class="col-md-6 recent notes">
<header class="entry-header">
<div class="entry-category">
<?php the_author_posts_link(); ?> / <?php the_time('F jS, Y'); ?>
</div>
<h2 class="entry-title" itemprop="headline">
<?php echo $post->post_title; ?>
</h2>
</header>
<div class="entry-content">
<?php echo apply_filters("the_excerpt", $post->the_excerpt) ; ?>
</div>
<div class="entry-category">
<span class="posted-on">By
<span class="author vcard"><?php the_author_posts_link(); ?></span></span>
</div>
</div>
<?php
$pnum++;
endforeach; ?>
</article>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-md-12 homehdr">
<header class="section-header">
<div class="title-with-sep">
<h2 class="title">Recent Reports</h2>
</div>
</header>
</div>
<?php
$showreports = get_field('showreports');
$args = array(
'numberposts' => $showreports,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'reports',
'post_status' => 'publish',
'suppress_filters' => true
);
$reptposts = get_posts( $args );
foreach( $reptposts as $post ) : //setup_postdata($ppost);
setup_postdata( $post ); ?>
<div class="col-md-6 recent rpts">
<h2 class="report-title" itemprop="headline">
<a href="<?php echo get_permalink($post->ID); ?>">
<?php echo(types_render_field("first-name", array('raw' => true))); echo(" ");
echo(types_render_field("last-name", array('raw' => true))); ?></a>
</h2>
<p><strong>Posted by:</strong> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" class="url fn n" rel="author" itemprop="url">
<span itemprop="name"><?php the_author_meta('display_name'); ?>
</span></a> on <?php
$publish_date = '<time class="entry-date updated" datetime="' . get_the_time( 'c' ) . '" itemprop="datePublished">' . get_the_time( get_option( 'date_format' ) ) . '</time>'; echo $publish_date; ?><br />
<strong>Dates Seen:</strong>
<?php,$dates_seen = types_render_field("dates-seen", array('raw' => true)); echo($dates_seen); ?>
<br />
<strong>Affiliate:</strong>
<?php echo(types_render_field("milb", array('raw' => true)));
<br />
<strong>MLB Team</strong>
<?php echo(types_render_field("mlb-club", array('raw' => true))); ?>
</p>
</div>
<?php
$pnum++;
endforeach; ?>
</article>
<?php endwhile; endif; ?>
<?php
// Enable/Disable comments
if ( $ti_option['site_page_comments'] == 1 ) {
comments_template();
} ?>
<?php
// Enable/Disable sidebar based on the field selection
if ( ! get_field( 'page_sidebar' ) || get_field( 'page_sidebar' ) == 'page_sidebar_on' ): ?>
</div>
<?php get_sidebar(); ?>
</div><!-- .grids -->
<?php endif; ?>
</div>
</section><!-- #content -->
<?php get_footer(); ?>
the_field(), by definition, will echo out the value of the field onto the page and NOT store it inside a variable... instead you want to do this:
$showarticleposts = get_field('articles-posts-to-show');
$args = array(
'numberposts' => $showarticleposts,
'orderby' => "post_date",
'order' => 'DESC',
'post_type' => 'post',
'cat' => '5, -58',
'post_status' => 'publish',
'suppress_filters' => true
);
To make sure it's getting what you want, do: var_dump( $showarticleposts ); and see if a 2, as you claim it should be, is being dumped on the page. Your third example "should" work, but you are using a different "field" name in each of your examples, so it's hard to tell if 'showarticleposts' or 'articles-posts-to-show' is the actual field name.
UPDATE
Since looking at your code... I have noticed that you are using setup_postdata( $post );. While this is a great thing to use, it's changing your global $post variable, and therefore when you call get_field() it is using the $post->ID of the "changed" $post variable, which is not what you want. You want the original $post->ID of the page you are currently viewing. So, simply add wp_reset_postdata(); after every custom loop you've created and it should fix the issue.
UPDATE 2
Since you mention it is STILL not working, the only other thing I can think of is to set a variable to the original page id at the very top of the page...
$current_page_id = get_the_ID();
Then when you call get_field(), include that id:
$showarticleposts = get_field( 'articles-posts-to-show', $current_page_id );

Categories