I have a custom taxonomy (support) and a custom post type (question) both related.
In my taxonomy-support.php template file, I use the following query:
<?php
$current_category = get_term_by('id', get_queried_object()->term_id, 'support');
$questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
And the loop
<?php if ($questions->have_posts()): ?>
<ul>
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
<div class="clearfix">
<div class="pull-left">
<?php previous_posts_link('← ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
</div>
<div class="pull-right">
<?php next_posts_link(__('Next', 'my-theme') . ' →', $questions->max_num_pages); ?>
</div>
</div>
<?php endif; ?>
As you can see I defined 2 posts per page.
When I visit the page, it shows me 2 posts then I go to page 2, it still works but when I go to page 3 or up, it shows 404.
Any idea?
I'm using WordPress 3.8.2 with no plugin installed.
Thanks
I was happy to find your post as I was fighting for the exact same issue (less happy to find no answer yet ;) ).
Anyway I finally found something interesting here: https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy
This made the trick for me.
I reckon you were into the situation where the value for your global posts_per_page option (as set up in the admin) was greater than the one for your taxonomy, which made the system screw up.
Well, not that much when you think about it: during its lifecycle, having the global posts_per_page option as is, the request does not validate the "paginated" url, thus never even load your taxonomy-support.php template file, where you tell it that the posts_per_page should be different.
You then must tell it earlier, during the intialization process. The 'pre_get_posts' action is the one.
Hope it helps someone!
Related
I'm using wordpress to create my own website. On the front page I want to dispaly the two latest blog posts and the two latest news article.
An example of text to display is like that:
<div class="blog_bloc">
<span class="Title_post">Title</span>
<p class="text_post">blog post text text text text text</p>
<button id="read" >
Read more
</button><br><br>
<span class="Title_post">Title</span>
<p class="text_post">blog post text text text text text</p>
<button id="read" >
Read more
</button>
</div>
I found some plugins in wordpress for creating blog page and news page.Now I want to display the latest posts or articles from the blog page or news page to put it automatically in the front page.Is there any plugin to add in my template to scrap the latest articles and put them into the front page automatically?
To get the latest posts use this php function:
<?php
$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
This will return an array which you then can loop through to display the two latest posts.
An example script would look like this:
<?php
$args = array(
'numberposts' => 2,
);
$recent_posts = wp_get_recent_posts( $args );
foreach ($recent_posts as $post ) {
?>
<div class="blog_bloc">
<span class="Title_post"><?php echo $post["post_title"]; ?></span>
<button id="read" >
Read more
</button>
</div>
<?php
}
wp_reset_query();
?>
It will normally show five posts in homepage or anywhere.
// Define our WP Query Parameters
<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
// Start our WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Display the Post Title with Hyperlink
<?php the_title(); ?>
// Display the Post Excerpt
<?php the_excerpt(__('(more…)')); ?>
// Repeat the process and reset once it hits the limit
<?php
endwhile;
wp_reset_postdata();
?>
Use your own css.
I've figured a way to enable adding categories to a PAGE (not a post). And I was just wondering if there was a way to display PAGES in a post loop, this is my code:
<?php query_posts('cat=540'); ?>
<div class="blog_module">
<?php if(has_post_thumbnail()) {
the_post_thumbnail(array(150,150));
} else {
echo '<img class="alignleft" src="'.get_bloginfo("template_url").'/images/empty_150_150_thumb.gif" width="150" height="150" />';
}
?>
<div class="entry">
<h3 class="blog_header"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>
</div>
</div>
However, this displays:
Which isn't what I want, it ONLY DISPLAYS POSTS and not the pages I have assigned to Category ID 540.
Please could someone help with a loop that would display pages that have been assigned to a category.
Thank you in advance.
Sure its easy. I guess the following should work (untested):
query_posts(array('cat'=>540,'post_type'=>page));
You can also use WP_Query which is more flexible than query_posts:
$q = new WP_Query(array('cat'=>540,'post_type'=>page));
while($q->have_posts()): $q->the_post();
// your post here
endwhile;
This should be even more flexible:
$args = array(
'post_type' => 'page',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 540
),
),
);
$query = new WP_Query( $args );
If you need posts AND pages try this:
'post_type' => 'any'
All information you need you can find on these links: https://developer.wordpress.org/reference/functions/query_posts/
https://codex.wordpress.org/Class_Reference/WP_Query
I need your help.
I would like to bring up the last (number) from my template classified ads. Here's the code:
<?php
$the_query = new WP_Query( array(
'showposts' => 10,
'post_type' => 'ad',
'tax_query' => array(
array(
'taxonomy' => 'ad_category',
'field' => 'id',
'terms' => array('8')
)
)
));
if($the_query->have_posts()):
?>
<div class="feat-ad column col12"><!-- featured ad -->
<h3 class="widget-title">Category</h3>
<ul id="first-carousel" class="first-and-second-carousel">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php colabs_image('width=150&height=150&play=true');?>
<?php the_title();?>
</li>
<?php endwhile; ?>
</ul>
</div><!-- end featured ad -->
<?php endif;?>
I tried with showpost, but nothing appears in one ad ... this is not even the last announcement.
Use the posts_per_page parameter instead of showposts
posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1
http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
I want to show all the posts of my wordpress blog on a single blog page, for some reason the solutions that I am finding in Google are 5-6 years old, I was wondering which is the best and easiest way to do it ?
Thanks
I think this will be the best one in my case ;)
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'orderby' => 'DESC'
);
$home_query = new WP_Query($args);
if($home_query->have_posts()): while($home_query->have_posts()) : $home_query->the_post();
get_template_part('pages');
endwhile;
else:
get_template_part( 'no-results', 'home' );
endif;
and in pages.php
<section class="page-<?php echo get_post_type();?>-<?php the_ID();?> id="<?php echo $post->post_name;?>">
<div class="container">
<?php
the_content();
?>
</div>
</section>
I have add custom post type field name 'Movies'. Now I have done these things and its working great, but the problem is, (i.e When I'm click on any movie, its showing me only one movie post, (i.e I'm click on avatar movie its showing me avatar movie post, but when I'm click on stargate movie its showing me avatar movie post. Please help its a big issue) anyone who can help me to make this code exactly which I want.
in my functions.php I have add this code:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'Movies',
array(
'labels' => array(
'name' => __( 'movie' ),
'singular_name' => __( 'movie' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then in my template file add this where I want to show post:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
I'm new for this thing, so please explain me as much as you can, where I paste it or what I do?
I can't see how you structured your links to those movie-posts, but you should give them a variable, so the page where the movie-post shows up knows what to show!
e.g $moviename
and in your template you modify the $args array to:
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
Should work, at least that explains why it always displays the same movie-post:
Your query hast no information what movie to display, at the moment it just takes the movie-posts table and displays the first one - because of the posts_per_page limit to 1.
Hope that makes sense...