wordpress custom post pagination show same post on all pages - php

Here is my code. Plesae helps me to fix this issue. Actually, I used a page template on WordPress. I try the many articles but do not get exactly what I want
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'case_studiess',
'post_status' => 'publish',
'posts_per_page' => 10
);
$wp_query = new WP_Query( $args );
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<article data-aos="fade-down" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if (has_post_thumbnail()) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $wp_query,
)
);
endif;
?>

Please use this query (I've added 'paged'=>$paged)
$args = array(
'post_type' => 'case_studiess',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged'=>$paged
);

Related

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

WordPress add a class for newest posts

Does sombody know how i can add a class to the newest posts in WordPress. And what i mean with the newest is the lasts posts that are added in the last minut?
My code now:
<?php
// query
$the_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'profiel',
'posts_per_page' => 48,
'paged' => $paged,
'meta_key' => 'online',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
if ( $the_query->have_posts() ) : ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" class="profiel">
<h3><?php the_title(); ?></h3>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
wp_reset_query();
the_posts_pagination( array(
'mid_size' => 10
) );
?>
Get the current time, remove 60 seconds compare that to the timestamp of your post. If your post timestamp is greater than the timestamp from a minute ago then it's new.
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" class="profiel <?= (get_the_time('U') > (time()-60)) ? 'new' : '' ?>">
<h3><?php the_title(); ?></h3>
</div>

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>

Pagination with Wordpress using "wp-query"

Hey all i'm running a query loop in a custom page.php using a custom post type. I would for it to paginated so i could have a specific amount of post per page. I was wondering if someone could help me out. I have added my code below:
<?php query_posts( array(
'post_type' => array( 'POSTTYPE' ),
'showposts' => -1 )
); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="">
<?php the_title(); ?>
<?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?>
View
</div>
<?php endwhile; ?>
Thanks!
The problem as wordpress.org documentation says:
Pagination won't work correctly, unless you set the 'paged' query var appropriately: adding the paged parameter
Example of usage:
<?php
$args = array(
'cat' => '5',
'post_type' => 'POSTTYPE',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
?>
<?php the_title(); ?>
<?php the_post_thumbnail( 'thumbnail' , array('class' => 'aligncenter project_post_thumbnail') ); ?>
View
<?php
endwhile;
?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php
wp_reset_query(); // Restore global post data
?>
Also you can check rvoodoo guide if you have more questions about it.

Only first post has content in loop

My Wordpress site has a problem:
In my blog-loop only the first post has content displayed; for the rest the_content(); never seemed to be called (checked with "inspect element").
Any probs in my code?
php/html:
<section id="blogPosts" class="clearfix">
<?php $myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
<div id="post-<?php the_ID(); ?>" class="post clearfix">
<div class="postHeader clearfix">
<a href="<?php the_permalink();?>">
<h2><?php the_title(); ?></h2>
</a>
<h4><?php the_date(); ?></h4>
</div>
<div class="postTags clearfix">
<ul class="tagContainer clearfix">
<?php the_tags( '<li><div class="tagInline">',
'</div></li><li><div class="tagInline">',
'</div></li>'); ?>
</ul>
</div>
<div class="blogContent">
<?php the_content();?>
</div>
<div class="editPost">
<?php edit_post_link('<h4>Redigera detta inlägg', '', '</h4>'); ?>
</div>
<?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
<?php //comments_template(); ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</section>
Check all other posts for having content in it..
Posts can be get by
<?php $posts_array = get_posts( $args ); ?>
Default Usage of args:
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'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 ); ?>
Access all post data
<?php
$args = array( 'posts_per_page' => -1 );
$allposts = get_posts( $args );
foreach ( $allposts as $post ) :
setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata(); ?>
This is the simple code to get all the posts. You can check the code by print the data ( print_r($post); ) to check what you are getting in the loop.
If you're talking about the page that has been assigned to Posts under Wordpress Reading Settings, you can get rid of most of your code and simplify it:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Just put whatever information you want between the while statement and you'll be good. See the Wordpress codex if you want to get more specific.

Categories