I got one "front-page.php" that is a static one side page. If I use the Wordpress loop to see my latest posts at the front-page.php they all shown up.
Now I want to create a news page so I created a file "page-news.php". Removed the loop code from front-page and pasted it into page-news. Though, nothing happens.
Loop code:
<?php get_header();?>
<?php
if (have_posts()):
while (have_posts()): the_post();?>
<?php the_title();?>
<?php the_content();?>
<?php
endwhile;
else: echo '<p>no posts were found</p>';
endif;
?>
<?php get_footer();?>
What have I missed?
you need to add wp_Query
the main page is consider a blog page so it have the Query default .
$args = array (
/*'cat' => $catNum,*/
'post_type' => 'post',
'pagination' => false,
'posts_per_page' => '-1',
'ignore_sticky_posts' => false,
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
you should add this code before
if (have_posts()):
while (have_posts()): the_post();?>
the this part about have_posts() will be
// The Loop
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();
dont forget to add wp_reset_postdata(); at the end so you can use many Query in one page .
Related
I'm working on adding pagination to a page and I am following along on the official codex but I am not getting results. Right now I want to populate pagination after 1 post per page (for testing purposes). No pagination shows, the h3 tags return empty. I have tried multiple queries other than the one below to no avail. Help is sorely needed and appreciated.
<?php
$paged = (get_query_var ('paged')) ? get_query_var ('paged') : 1;
$args = array(
'post_type' => array('post'),
'posts_per_page' => 1,
'paged' => $paged,
'cat' => 5,
'tag__not_in' => 22
);
$the_query = new WP_Query ( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- THE CONTENT -->
<?php endwhile; ?>
<h3><?php next_posts_link('Next'); ?></h3>
<h3><?php previous_posts_link('Previous');?></h3>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I have a posts loop in home.php:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
wp_reset_postdata();
} ?>
How can I add pagination (numbers)? I looked for a tutorial. I tried a lot of functions but none worked.
Thank You
Should be possible using this code:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
mid_size defines how many page numbers will be displayed on either side of the current page in that line.
For more details including individual texts for the previous/next page links see:
https://codex.wordpress.org/Function_Reference/the_posts_pagination
Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.
I cannot find a solution for my problem and I am pretty new to WordPress templates.
I want to load all the direct childs from my active page and load them together with the template which also uses one added image in acf. The idea is to have more than 1 static design for the loaded content.
This is the code I am using, but the_content() only loads the content field itself and not the template or the acf image.
I tried get_post but it doesn't work.
i would really appreciate if you can help me with that. :)
Best wishes,
Mike
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
while ( $parent->have_posts() ) : $parent->the_post();
the_content();
endwhile;
endif;
wp_reset_query();
?>
Your query is sound, but the issue is that you're not pulling any of the info in your loop.
The way WordPress works, is that on the individual template file that you are working with, you need to specify the specifics of the layout from those pages within this parent template.
How it is done is entirely dependant on the layout(s) of your child page(s), your template file structure (are you separating your templates into smaller, reusable elements (i.e. using get_template_part()), etc...
Essentially, you'll need to add into this template that you're working on, the additional content in the layout you require.
See the example below for a rough idea:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
?>
<?php if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<h3><?php the_title(); ?>
<div class="img-from-child">
<img src="<?php get_field('img_src_from_child'); ?>" />
</div>
<?php the_content(); ?>
<?php endwhile;
<?php endif; ?>
<?php wp_reset_query(); ?>
I am creating a landing page (one page with many section) in WordPress, and I'm calling each section using get_template_part(). So I have something like this;
<?php get_template_part( 'content', 'reasons-to-purchase'); ?>
<?php get_template_part( 'content', 'testimonials'); ?>
<?php get_template_part( 'content', 'reasons-to-purchase'); ?>
Each content file then has a loop to return the posts that match its category, with posts_per_page set to one, as shown below (content-reasons-to-purchase.php);
<?php
$args = array(
'post_type' => 'section',
'category_name' => 'Reasons-To-Purchase',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query ( $args );
if (get_category_by_slug('Reasons-To-Purchase')->category_count > 0 ) {?>
<!-- Featured content image or slider. -->
<div class="container panel">
<?php if ( have_posts() ) : while ( $the_query->have_posts () ) : $the_query->the_post(); ?>
<?php the_content(); ?>
</div>
<?php } ?>
<?php wp_reset_query(); ?> <div class="clearfix"></div>
What I would like to is create as many as these sections in which ever order I like and the loop within each section pulls the next post assigned to that category. So for example in the first 'reasons-to-purchase' section the first post is pulled and then in the second 'reasons-to-purchase' the second post is called. At the moment I reset the loop in each file using 'wp_reset_query()' so it doesn't interfere with the next section which may be different. Basically the loop has to continue for the next similar named section without duplicating any posts..
Any ideas on how to do this or advice would be most appreciated.
You need to pass page parameter before calling get_template_part() but get_template_part() does not support the re-use of your variables, you need to use locate_template() and include()
$page=1;
include(locate_template('content-reasons-to-purchase.php'));
include(locate_template('content-testimonials.php'));
$page=2;
include(locate_template('content-reasons-to-purchase.php'));
$page=3;
include(locate_template('content-reasons-to-purchase.php'));/* and so on page 4,5,6... */
In you template make a slight change of code
if(empty($page)){
$page=1;
}
$args = array(
'post_type' => 'section',
'category_name' => 'Reasons-To-Purchase',
'posts_per_page' => 1,
'orderby' => 'date',
'paged'=>$page, /* <======= get page no from your file i.e locate_template(...) and use here*/
'order' => 'DESC'
);
Hope it makes sense