How to re-use initial page content loop in WordPress? - wordpress

I'm working on a site whereby there is a simple template for a certain page in the site.
The page in question is an About Us page.
On the page there is the first main WordPress loop which just spits out the standard content of the main Wysiwyg field.
Underneath that the template has some code which spits out a loop of a custom post type called Staff. This custom post type has information on each staff member and each one is spat out there.
What I have added to the page via the backend is an Advanced Custom Field using ACF Pro. The field is called Our Story (our_story) and I'd like to be able to spit that field out on the template underneath all the staff members.
If I place the following code :
<p><?php the_field('our_story') ?></p>
into the template in the main WordPress loop which is at the start of the template then it shows up fine but I effectively want to start the same loop again but underneath the loops which have spat out the staff members.
If I copy the exact same loop which is at the start of the template to the end of the template then I don't get anything output on the page where that code is. Do I therefore need to reset the code or something or is there an easy way of perhaps caching field content and placing it elsewhere in the template?
Sorry entire template code shown here :
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>

I've adjusted your template to include named WP_Query() loops, and apply wp_reset_postdata() after each custom loop. This should allow the final the_field() call to have access to the current post ID, which is what Advanced custom fields needs in order to fetch the post metadata for the current post.
Behind the scenes, ACF runs (int) get_the_ID(); to get the ID of the current post. If your custom query loops are not reset, this won't work.
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<?php the_post_thumbnail('service_thumb'); ?>
<h4><?php the_title(); ?></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo ''.$field['choices'][ $service ].' ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p>Read more »</p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
I think it's easy to get confused with custom loop logic when it's all handled within a template - I prefer to manage post data for custom loops by means of a function or class method which returns an array of post data - you can then loop over this in your template.
Hope this helps.
Resources: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops

it's because you need a post id to get the field. By default it is the current post's id.
try this: <p><?php the_field('our_story', xxx) ?></p> where xxx is the post id of About Us.

Related

Wordpress, how to make an "if in category then" inside a loop?

How do I make this conditional work inside a loop? This seems so easy but it does not work.
The relevant part:
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
The whole loop:
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$args = array(
'post_type' => 'support_post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID,
),
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ): ?>
<h4><?php echo $cat->cat_name ; ?></h4>
<?php if (is_category('Sup-FAQ')) : ?>
<p>If in cat sup-faq</p>
<?php else : ?>
<p>If not then print this</p>
<?php endif; ?>
<ul>
<?php
while($query -> have_posts()) : $query -> the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
wp_reset_query() ;
}
?>
The loop takes all posts from the custom post type 'support post' and lists them by category. Now I need a conditional inside that loop that will display some text if a category is met. Thanks in advance.
From the is_category() documentation: To test if a post is in a category use in_category().
On the other hand, in your case you've already got the category in your outer loop. Just check $cat->name:
<?php if ($cat->name == 'Sup-FAQ'): ?>

wp_query by last month + sort posts

I'm using the WordPress plugin 'I recommend this', it allows people to 'like' posts.
The value is stored as a meta key, which we can query to generate a 'most recommended' page.
Plugin -> http://wordpress.org/support/plugin/i-recommend-this
The loop.
<?php
query_posts('&orderby=meta_value&meta_key=_recommended');
if (have_posts()): while (have_posts()) : the_post();
?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<?php endif; ?>
This works, it finds the most recommended posts.
Two questions.
How do I limit the posts returned to a date range of say, the last 3 months?
In a similar vein, how could I then have a button for 'Most popular this week' that'd allow users to see a filtered result of posts from the last 7 days?
You could try something like this, this is just a basic example:
$today = getdate();
$threemonths = $today["mon"]-3;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => $today["year"],
'month' => $threemonths,
'day' => $today["mday"],
),
'before' => array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
They break it down further in the wordpress documentation:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
I worked it out. Used a custom filter along with WP_query array.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-180 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$featuredPosts = new WP_Query( array(
'meta_key'=>'_recommended',
'orderby' => 'meta_value_num',
'order' => DESC
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<div class="figure">
<?php the_post_thumbnail('frontthumb'); ?>
</div>
<div class="fig-cover">
<div class="fig-bottom">
<div class="title-container">
<?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
<div class="price-container">
<?php echo get_post_meta($post->ID, "Price", true); ?>
</div>
<h2 class="price-title"><?php the_title(); ?> </h2>
<?php } else { ?>
<h2><?php the_title(); ?> </h2>
<?php } ?>
</div> <!-- end div title-container -->
</div>
</div>
<div class="reco">
<?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
</div>
<a class="the-post-link" href="<?php the_permalink(); ?> ">
</a>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now I just need to figure out how to do live filter on-site...

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source:
WordPress custom loop with pagination

Wordpress: single.php shows the newest post only

I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
It displays - no matter what post (&p=111 e.g.) you select - only the content of the newest post.
This is my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="page-title"><?php the_title() ?></h1>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="cover" />
<?php endif; ?>
<p class="page-text">
<?php the_content(); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
What could be wrong? I hope you've understood my problem. Thank you!
edit:
I recently updated the header file. When I delete this loop, it works fine:
<ul class="nav-dropdown">
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 5,
'exclude' => '1,2,3,4,5,6,8,9,10,11,12,13,14'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
$post_args = array(
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<li class="nav-dropdown"><?php the_title(); ?></li>
<?php
}
}
?>
</ul>
I'd change your variable names in the header as ones such as $post are reserved by Wordpress for handling single post pages.
I am not sure but please change the $post variable to any other variable and than try
may be your problem be solved.
Because $post is global variable of post.

Pagination on WordPress posts within page

I'm trying to do a custom archive for all posts, but I want it to look a little different than the category-specific archives. I've achieved this so far by placing the code below into a page on my site.
Is it possible to add pagination to something like this? I thought that 'paged' => $paged line might do it, but no such luck.
Here's my code: (I'm using a custom thumbnail size if you were wondering what that refers to.)
<?php
global $post;
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'paged' => $paged
);
$thumbnails = get_posts($args);
foreach ($thumbnails as $post)
{
setup_postdata($post);
?>
<div class="featuredarticle">
<h4 class="entry-title"><?php the_title(); ?></h4>
<div class="featuredimage">
<?php the_post_thumbnail('featured'); ?><br />
</div>
</div>
<p><?php the_excerpt(); ?></p>
<div class="entry-utility">
<span class="read-more">Read More</span>
</div>
<?php
}
?>
This code works very good within a page querying posts and use pagination.
<?php
/**
* Template Name: Page of Books
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_content();
endwhile; wp_reset_query();
?>
<div id="container">
<div id="content">
<?php
$type = 'book';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php
get_template_part( 'loop', 'index' );?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Resources