I have a custom template I am using for my page and in that custom template I get the_content() then I use a custom query. I want to be able to only get 6 of my posts then be able to go to prev and next to see the rest of the posts.
Here is some of my code:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<div id="the_content">
<?php the_content(); ?>
</div>
<?php
$args = array('numberposts'=> 6,
'post_type'=>'project'
);
$posts = get_posts( $args );
if($posts){
foreach( $posts as $post ) : setup_postdata($post);
//do stuff
wp_reset_postdata();
endforeach;
endwhile;
</div>
</article>
?>
Try this
<?php posts_nav_link();
Reference: http://codex.wordpress.org/Next_and_Previous_Links
Related
I want to display one post at a time inside a div and replace the content to display the next (adjacent) post's content on the click of a button.
I am currently looping through the posts using wp_query, but all posts are displayed stacked on top of each other.
query_posts($args);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
$count = $wp_query->post_count;
$i = 1;
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() && $i < 9) : $wp_query->the_post();
?>
I am pulling in the following content:
<?php echo $i?> / <?php echo $count?>
<?php the_title();?>
<?php the_content();?>
I also have a conditional surrounding the button to pull in the adjacent link's permalink, but it is grabbing the entirety of the permalink when I only need the post's slug. I'm imagining something like this, but it doesn't work:
$next_slug = get_slug(get_adjacent_post(false, '', false));
This is essentially AJAX navigation through posts. This answer should be able to do what you're asking.
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title( '<h1>','</h1>' ); ?>
<div class="post-thumbnail"><?php the_post_thumbnail(array(250, 250)); ?> </div>
<div class="entry-content"><?php the_content(); ?></div>
<div class="btn"><?php next_post_link('%link', '%title'); ?></div>
</article>
<?php endwhile; ?>
</div>
I'm having some real trouble with the below code in my taxonomy.php template in Wordpress. The query is working (i.e pulling posts only from within that custom taxonomy) but it is only displaying 2 posts (4 are in the taxonomy).
All my efforts to convert this into a standard loop using $args simply result in posts from all taxonomies being pulled into the page. I was hoping it is as simple as adding posts_per_page => -1 but this just causes every post in the entire site to display.
As I understand it from the codex, taxonomy pages should pull the relevent posts by default, without a need for a loop?
Any help much appreciated!
taxonomy.php
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
UPDATE
<main>
<?php
$args = array(
'posts_per_page' => -1
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
If you have 6 different taxonomies then there will be 6 different template files to show those taxonomies appropriately. In your case your templates will be taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.php and taxonomy-a-z.php
In this way once these templates are created your templates will be showing appropriate posts. To achieve that you can use posts_per_page argument or you can visit this page for better understanding about fetching posts WP_Query Codex Page Hope that makes sense by now
I would like to display post from only one category. How should I change this function?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post','paged' => $paged);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'post-format/content', get_post_format() );
endwhile;
else:
get_template_part( 'post-format/content', 'none' );
endif;
?>
<?php
/*
Template Name: Boats'n'Hoes
*/
?>
<?php get_header(); ?>
<div id="main">
<div id="content" class="narrowcolumn">
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
reference :- https://wordpress.org/support/topic/display-the-posts-of-one-category-in-a-page-solved
Something I created for a client was a template that lists posts from a category that has the same name as the page.
So, create a Page called "funnies" if you want to list all posts in the "technology" category.
Oh, and the original content of the Page is displayed too, if you need an introductory text.
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Pass this with $args
'category_name' => 'cat-name',
I am using the relationship field within a custom post type, but my content is not showing up. I have doubled checked against the documentation but I cant see why it wouldn't be working.
Any help would be great!
Documentation here: http://www.advancedcustomfields.com/resources/relationship/
My html is:
<?php
// The Query
$args = array(
'post_type' => 'top-car'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$posts = get_field('top-cars');
if( $posts ): ?>
<ul id="recom">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<div class="recom-single">
<h2><?php the_field( 'model' ); ?></h2>
</div>
<!--Close Recom Single-->
</li>
<!--Close First Car-->
<?php endforeach; ?>
</ul>
<!--Close Slider-->
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we have no cars available at this time, please contact us.'); ?></p>
<?php endif; ?>
I'm using the SuperSimple Theme for a wordpress blog and am trying to customize the category.php page. I want to have a big image for the latest post in each category on top of a smaller grid of all the older posts.
So far I have it working the way I want, except the top image (div id="post1") is just the most recent post overall instead of the latest post for the category. Here's one of the category pages: http://meanmargie.com/category/hospitality/
And here's the code I have:
<header class="header">
<h1 class="entry-title"><?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<div id="post1">
<?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<br>
<div id="post2">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; ?>
</div>
First of all, return the category ID of the category page you are on with get_query_var('cat')
$category = get_query_var('cat');
$cat_id = $category->cat_ID;
Now, return that id back into WP_Query, using 'cat'=> $cat_id,. Please note, showposts is depreciated, use posts_per_page instead
DO NOT USE query_posts. You should never ever use query_posts. It breaks and alters the main query and fails outright in most cases with pagination.
Here's the solution I ended up using:
<div id="post1">
<?php //new query to limit number of posts
$wp_query = new WP_Query();
$wp_query->query($query_string."&posts_per_page=1&paged=".$paged);
?>
<?php WP_Query; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div class="caption"><?php the_title(); ?></div><div class="excerpt"> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>