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
Related
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 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 .
I'm trying to loop through posts and generate a few different posts each time the page is hit. I've used this exact same process before and it's always worked fine, but for some reason on this site it's behaving differently. It will load random posts, but only the first time. In other words, it's loading them in a scattered order, but it loads the same posts in the same order every time. Instead, I want it loading completely different posts every the page is hit. I have no idea what's going on with it! I've tested plugins and themes already, no luck. Any ideas?
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 5
);
$wp_query = new WP_query($args);
while(have_posts() ) : the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
wp_reset_query();
?>
Any installed theme or plugin may have used orderby, so try to remove it first when you're using for your own query.
<?php
remove_all_filters('posts_orderby'); //Reset orderby filters
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 5
);
$wp_query = new WP_query($args);
while(have_posts() ) : the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
wp_reset_query();
?>
I want to show few random posts at the end of each single post. I found this code for this purpose.
<div>
<h2>Random Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
But the problem is that when I use this code on my site, it also cause the random output of next and previous post links present at the bottom of each post for better navigation.
I want to know how to terminate the effect of random posts so that pre and next post links are displayed in their original order.
Try
<?php wp_reset_query(); ?>
More info here : http://codex.wordpress.org/Function_Reference/wp_reset_query
I've got a blog and a website; the two are separate things all together. For example, I have www.domainname.com as the main site, and the blog would be at www.domainname.com/blog/
It's a WordPress blog, however I don't want people looking at the WordPress front-end, so I would like to write a php function that would pull the posts from WordPress to a separate page on the main site.
The function I've got so far is as follows:
<div id="blogPosts">
<?php
require('../path1/path2/wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=DESC&orderby=post_title');
foreach ($posts as $post) : start_wp();
?>
<h4 class="blogDate"> <? the_date(); ?> </h4>
<hr />
<h5 class="blogTitle"> <? the_title(); ?> </h5>
<p class="blogText"> <? the_excerpt() ?> </p>
<br />
<?php
endforeach;
?>
</div>
It will display the page fine, but it's not posting the posts to the page at all. Any ideas why it won't work?
first, put this before u include wp-blog-header.php
define('WP_USE_THEMES', false);
and i think you are looking wp_query not get_posts, if you want to use the loop functions
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
Are you sure you are getting the posts? Try dumping your posts after you fetched them, like this:
var_dump($posts)
Does the $posts-variable contain anything? If not, you probably won't get the posts, because $wpdb (the database connection class) is still undefined.
Secondly, what happens when you try
<?php echo $post->post_date ?>
instead of
<? the_date(); ?>
It could be that wordpress doesn't realise you are in a loop and therefor the loop-functions (e.g. the_date, the_title, ...) won't work.
let me know if one of these things works.
Just from a quick search I see that usage of get_posts( $args ) expects $args to be an array with these optional values
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish' );