I want to include the content of an existing page in the header.php file? What's the easiest way to do this? I am guessing that there is a way to load an existing page via a php call, however I am not sure of the correct syntax.
Thanks, in advance.
You could instantiate a new query:
$query = new WP_Query( 'page_id=7' );
After doing so, you make a loop to display the query's content.
<?php if ( $query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); // Important, so this loop does not affect the global post object afterwards ?>
<?php endif; ?>
This is based on WP's official codex: https://codex.wordpress.org/Class_Reference/WP_Query
Related
I used the following code in my page template:
<?php
while(have_posts()):the_post();
the_content();
?>
But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.
You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php
/**
* Template Name: My Template
*/
the_post();
the_content();
?>
If you save the following code as a page template and call the page template into a page by choosing it, then the page will show the page content without any hassle.
Note: it's THE minimal bit of code.
use the following code
Try using wp_reset_query()
<?php
wp_reset_query();
while(have_posts()):the_post();
the_content();
endwhile;
?>
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(); ?>
I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/
<?php $query = new WP_Query('posts_per_page=1&'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?><br />
<?php echo paginate_links( $args ) ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
Here is the query I'm using, it should display the first post title then displays the pagination links, however it only shows the post title without the pagination links. What's wrong?
The paginate_links function should go outside the loop and you should use the query variable "paged"
Please see this link http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html for more information on using this.
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