query_posts Exclude Directory, next_posts_link not work - wordpress

Can anybody look at the following codes? After I excluded the category 15, the next_posts_link does not work, nor does the page navigation plugin. When I click the page 2, 3,4, it only display the posts in page 1.
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; else: ?>
<?php next_posts_link() ?>
<?php endif; ?>

How about...
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; ?>
<?php next_posts_link() ?>
<?php endif; ?>
??

Try it like this:
<?php query_posts(array( 'cat' => -15, 'paged' => get_query_var('paged') ) ); ?>
This should work.

Related

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.

Single post template not working in WordPress

page-webinars.php:
<?php
/*
Template Name: Webinars
*/
?>
<?php
$loop = new WP_Query(array('post_type' => array('webinars')));
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
?>
<?php get_template_part('loop-webinars'); ?>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); ?>
loop-webinars.php:
<?php the_title(); ?>
single-webinars.php:
<h1><?php the_title(); ?></h1>
<div>
<?php
the_post();
the_content();
?>
</div>
Looks like everything's correct. Page displays necessary template, but single not working.
You forget to use WordPress loop.
Try with this code in your single-webinars.php file.
<?php
// Start the loop.
while ( have_posts() ) : the_post();
?>
<?php the_title('<h1>', '</h1>'); ?>
<div>
<?php the_content(); ?>
</div>
<?php // End of the loop.
endwhile;
?>
It had just to "reactivate" theme...

Post not displaying in wordpress theme

I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

How to show post of specific category?

Currently I am querying a post from my custom post type to my custom page template. This is the code I am using
<?php query_posts('post_type=testimonial&post_status=publish&posts_per_page=10&paged='.
get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
But I want to query a post from a specific category. For eg. my custom post type is "testimonial" it has 3 categories like category1, category2and category3 . I want to show only the post of category3 in my page template. How can I do that? thanks
Use this
<?php $posts = get_posts('category=3&orderby=rand&numberposts=5');
foreach($posts as $post) { ?>
<?php the_title(); ?>
<?php } ?>
Or
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
query_posts( $args );
Uncomment either one of the below as per you requirement.
If you want to query using the id of the category use the first one and replace 99 with your category id.
If you know the slug of your category use the second one with your category slug(not name).
<?php
//query_posts('cat=99&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged'));
//query_posts('category_name=your_category_slug&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
P.S: I recommend you to use wp_query() instead of query_post().

WordPress search results multiple loops

The following is my search.php:
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type == 'page') : ?>
Show Page results
<?php endif; ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type != 'page') : ?>
Show non-page results
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
No Results
<?php endif; ?>
The code allows search results under posts to be separated from those found in pages. If no search results are found for either posts or pages, the text "No Results" is displayed.
However, when results are found for pages but NOT posts (and vice versa), under the 'Post Search Results" there is nothing displayed. I would like the code tweaked so that if there are no results found under "Post Search Results" but there are results found under "Page Search Results," the text "No Results" is displayed underneath the "Post Search Results" header.
Thanks a lot everyone.
Give this a try, you can expand the array to include any post_type your site is using:
<?php
foreach (array('post','page') as $pt) :
$search_query = new WP_Query(array(
'post_type' => $pt,
's' => $s,
'posts_per_page' => 10,
'paged' => $paged
)
);
?>
<?php if ($pt == 'post') : ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php
if ($search_query->have_posts()) : while ($search_query->have_posts()) : $search_query->the_post();
if ($pt == 'post') :
?>
Post Results Code
<?php else : ?>
Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
No Results
<?php endif; ?>
<?php endforeach; ?>

Resources