I have a small problem on a wordpress custom post type page, whereby the page content underneath seems to output the last piece of content from one of the custom post types. The code is below.
We wanted to move the page content to below the custom post type and so moved the get_template_part( 'content', 'page' ); function to below the custom post loop. When this function is above the loop, then the page content outputs properly, but above the custom post loop (i.e not as desired).
I know the fix could be fixed, but if so please could you point me in the right direction?
<div id = "feature_boxes_wrap2">
<?php query_posts(array(
'posts_per_page' => 3,
'post_type'=>'feature_box'
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class = "feature_box2">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<h2 class="feature2"><?php the_title();?></h2>
<?php $feature_content = get_the_content(); ?>
<?php echo substr($feature_content, 0, 100); ?>
<br>
Read more
</div><!-- #feature_box2 -->
<?php endwhile; // end of the loop. ?>
</div><!-- #feature_boxes_wrap2 -->
<?php
the_post();
get_template_part( 'content', 'page' ); ?>
Here, try this:
Add a wp_reset_query() before you call the_post(); [...] and that should do the trick.
For more information: http://codex.wordpress.org/Function_Reference/wp_reset_query
Related
I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
I have a very simple loop on my single.php page that includes the previous/next post links. For some reason, both links appear on the first and last entries, even though it should only show one or the other. Any idea why this functionality might be broken? It happens in the default theme and doesn't seem to be an issue with a plugin.
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php previous_post_link(); ?>
<?php next_post_link(); ?>
<?php endwhile; // end of the loop. ?>
I'd like my homepage to display my latest posts which are portfolio projects of mine. Beneath these project thumbnails I've got my static content which I'm using the Repeater add-on from Advanced Custom Fields to grab. I cant get it all to work on the same page... either get the blog to work or the ACF stuff to work. Never both because one needs to be a designated static page and one needs to be a posts page. I don't understand how to tie it together and make it appear as one page.
I've experimented with the settings in the reading panel..
I've tried using front-page.php
I've read up on the WP hierarchy etc
I've tried using templates...
I've tried wp_reset_postdata(); which I read about elsewhere on Stack Overflow.
What settings must I use in the reading panel. Do I need to use a template file?
Here's the code I'm working with, I've split the code between templates and different files already, but just for ease of reading its all together here (maybe that's the right way to do it anyway, I wouldn't know..)
<!-- The posts/portfolio items -->
<?php get_header(); ?>
<div>
<?php if(have_posts()) : ?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<!-- Permalink,title and post thumbnail here (omitted) -->
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<h2>No Posts found</h2>
<?php endif; ?>
</div>
<!-- Now for the ACF Stuff -->
<?php if(get_field('care_list')): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Please help a frustrated learner! Thanks in advance.
It looks like you're going to need to add the post id of your 'home page' (the one with the ACF repeater on it) to the get_field() function like so:
<?php $post_id = **post_id_of_your_homepage_here**; ?>
<?php if(get_field('care_list', $post_id)): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
This is because the $post_id parameter defaults to the current post being brought up by wordpress, which means ACF is looking for the repeater on the last Portfolio item/post you are displaying. If you set the $post_id parameter to the ID of your homepage, ACF will instead look for the repeater on that page.
Source: http://www.advancedcustomfields.com/resources/functions/get_field/#parameters
If I'm understanding correctly, you have a bunch of posts and you want to display a list of them with title and post thumbnail on your homepage, and then display a custom field you've assigned to the homepage underneath the list of posts?
Step 1: Create a new page template by copying page.php, changing the name to homepage.php and adding this to the top:
<?php
/*
Template Name: Homepage
*/ ?>
Step 2: Crete a Wordpress page called "Homepage" and in the attributes module in the right sidebar of the page creation tool, select "Homepage" as your page template.
Step 3: In your reading settings, change the front page from posts page to "Homepage." Now your homepage is your page called "Homepage."
Step 4: Make something like this the full code on your new page template homepage.php. It will output your posts list followed by your page custom field:
<?php get_header(); ?>
<?php $the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php if(get_field('repeater_field_name')): ?>
<?php while(has_sub_field('repeater_field_name')): ?>
<?php the_sub_field('sub_field_1'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.
I'm trying to style a search page in wordpress. In search.php I can style most of the page but then the following statement (which I got from the original uneditted page) generates the content.
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
This ALMOST displays the page as I want it, but there are a few elements off the page, making it expand etc. I can't figure out what file is generating this content!
Using the instructions I created a content-search.php and change the line of code to this...
get_template_part( 'content', get_post_format() );
Which works...but it doesn't display much of anything because I don't know what to put in my page within seeing the original.
Anyone have any clue?
You can use a template part named post-search.php and can use it inside your search.php file like
get_template_part( 'post' , 'search')
but you have to create a php file inside your theme folder and name it post-search.php and inside this file just put the WordPress' loop i.e.
<?php while (have_posts()) : the_post(); ?>
<div class="post-entry clearfix"> <!-- Main wrapper -->
<div class="post-entry-content"> <!-- Post-entry-content -->
<h2><?php the_title(); ?></h2>
<div class="post-entry-date">Posted on <?php the_time('F Y') ?> with <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></div>
<?php the_excerpt(); ?>
Read More ?
</div><!-- END of post-entry-content -->
</div><!--End of main wrapper -->
<?php endwhile; ?>
and your search.php could be something like this
<?php get_header(' '); ?>
<div id="post-wrap">
<div id="post-content">
<?php if (have_posts()) : ?>
<?php get_template_part( 'post' , 'search') ?> // This will load/include the file post-search.php and result will be displayed as formatted in this file
<?php else : ?>
<p>Sorry, it does not exist !</p>
<?php endif; ?>
</div><!-- END post-conten -->
<?php get_sidebar(' '); ?>
</div><!-- END post-wrap -->
<?php get_footer(' '); ?>
This is just an example, change div/h2 id/class names according to your theme css.
Note: I'm currently using this approach in one of my site and I've one file named 'post-entry.php' in my theme folder and in my every template file (index.php, search.php e.t.c) I just use this file by calling
<?php get_template_part( 'post' , 'entry') ?>