WP_Query, custom fields and no pagination - wordpress

I have this code:
<?php $q = new WP_Query(array(
'post_type' => 'oferty'
));
?>
<?php while ($q -> have_posts()) : $q -> the_post(); ?>
<!-- .post | id: <? echo $post->ID; ?> -->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<h1><?php the_title(); ?></h1>
<small><?php echo get_field('bank'); ?></small>
<?php the_content(); ?>
</div>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php if($thumb) : ?>
<div class="bankimg" style="background-image: url('<?php echo $thumb[0];?>')"></div>
<?php endif; ?>
<div class="clear"></div>
</article>
<!-- /.post | id: <? echo $post->ID; ?> -->
<?php endwhile; wp_reset_query(); ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
It uses wp_query to list all the posts from post type oferty. I set in WordPress options limit of posts to 2 and it works - but no pagination shows up. I tried WP PageNavi, WP Pagination and normal WordPress' prev/next linsk.

Take a look at the pagination parameters when querying for custom post types:
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
Specifically, try this:
$q = new WP_Query(array(
'post_type' => 'oferty',
'posts_per_page' => 2
));

Related

Not working Custom template in WPML Plugin Wordpress

I have Created custom template in WordPress its working for English with WPML plugin, but when I change language to Dutch language in custom template so it's not change Dutch language for custom template.
Please check the screenshot:
https://prnt.sc/tibvMuUL3jIh (English Language)
https://prnt.sc/EqzfIWRL5Jop (Dutch Language)
<?php $args = array(
'post_type' => 'stories',
'posts_per_page' => '3',
);
$query = new WP_Query($args); ?>
<?php if ($query->have_posts() ) : ?>
<?php
while ($query->have_posts() ) :
$query->the_post();
$test = get_post_meta(get_the_ID());
$img_atts = wp_get_attachment_image_url($test['profile_image'][0]);
?>
<div class="content_story">
<div class="story-col">
<div class="story_main">
<?php if(!empty($img_atts)) { ?>
<img src="<?php echo $img_atts; ?>">
<?php } else { ?>
<img src="https://ourforeverstories.com/wp-content/uploads/2022/04/search.png">
<?php }?>
<h3><?php echo $test['firstname'][0].' '.$test['lastname'][0]; ?></h3>
<p><?php echo $test['date_of_birth'][0]?></p>
<p><?php echo $test['Place_of_birth'][0]?></p>
</div>
</div>
</div>
<?php endwhile; ?>

Wordpress custom loop pagination - always same content

I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.

Wordpress - display certain categories on homepage

I have been trying to alter my current code, but it makes no sense to me (it doesn't do what I was told it does). All I want to do is only display posts with category 13 on my homepage and I want to be able to add multiple categories to a post.
Post 1 (Category 13 and 1) = displayed on homepage
Post 2 (Category 13 and 4 and 5) = displayed on homepage
Post 3 (Category 6 and 1) = not visible on homepage
Post 4 (Category 2) = not visible on homepage
This is my current code to only show category 13 on my homepage, if another category is added to the post it won't be displayed at all.
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
query_posts("cat=-6,-4,-1,-11");
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => 13,
'posts_per_page' => 5,
'paged' => $paged);
query_posts($args);
?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You're looking way too hard at this code. You don't need to exclude anything, you just need to include cat 13, which will include everything that is in cat 13; even if it is in other categories as well. Just run a regular WP_Query() like so:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
// The Query
$the_query = new WP_Query("cat=13, paged=".get_query_var('paged'));
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
(had to add a few charachters or I could not edit, sorry :P)
Try this...
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&cat=13'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Your stuff goes here...
<?php endwhile; ?>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Try this code
<?php
global $post;
if (is_home()) {
$args = array( 'numberposts' => 5, 'category' => 13 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content( 'Continue reading <span class="meta-nav">»</span>); ?>
</div>
<?php endforeach;
}
?>
This will display the title and content. If you want additional information like date, author, category, tag etc, you may have to use other template tags. Also if you want to style these differently, then you have to use appropriate CSS.

Wordpress blog isn't showing posts. Does adding a custom portfolio post field effect this?

Now I've done this a couple other times with no problem, I have a main page using home.php and the blog as another page using "blog-home.php" blog template with all the right code to grab the posts but it's not displaying. The only difference is I've added a custom portfolio post field to the functions, would this be effecting it or could it be something else? I can access a post from the home page under latest post putting the code below but that's it.
<?php query_posts("post_per_page=1"); the_post(); ?>
<p><?php the_excerpt(); ?></p>
<?php wp_reset_query(); ?></div>
*UPDATE: I've tried another code but now it is only displaying the blog page as a post. *
<?php
/*
Template Name: Blog Home
*/
?>
<?php get_header(); ?>
<div id="contentwrapper">
<?php query_posts( array ( 'category_name' => 'Blog', 'posts_per_page' => 5 ) ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="blogentry">
<h4><?php the_title(); ?> </h4>
<?php the_content(); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_footer(); ?>
Maybe if you use
$posts = get_posts(array('numberposts' => 1));
global $post;
$post = $posts[0];
the_excerpt();
instead
query_posts();
is never a good idea change the global query if get_posts dosn't work for you try with WP_Query()

Use WP_Query, but keep pagination

I would like to have my page show the 2 most recent posts, so I'm using WP_Query and setting posts_per_page to 2, which works great, but it kills the pagination. Here is my code. How do I alter it to show two most recent posts and keep pagination?
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2 ));?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div id="ind_post">
<h2 class="post-title"><?php the_title(); ?></h2>
<div id="entry">
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<p><?php the_excerpt(); ?></p>
</div>
<h4 class="more-post">continue reading…</h4>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>No results found</h2>
<?php endif; ?>
<?php wp_reset_query(); ?>
This should do it
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2, 'paged=' . get_query_var( 'page' ) ));?>

Resources