I'm trying to add a custom loop to display post titles from the category homepage. Here's the code I have so far so, but it's not displaying any posts. BTW, I'm using this code in single.php.
<?php $recentPosts = new WP_Query(); ?>
<?php if ( $recentPosts->have_posts() ) : ?>
<?php $recentPosts->query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => 10 ) ); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I'm not too sure what's going on, but would appreciate any help I can get.
You're doing a lot of unnecessary messing around with your query object. Since you're dealing with an archive page, you should be able to create a new template file called category-homepage.php (assuming the slug of the category is homepage). In it, you could place something like the following:
$args = array(
'category_name' => 'homepage',
'posts_per_page' => 10
);
$recentPosts = new WP_Query( $args );
if ( $recentPosts->have_posts() ) :
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Again, since it's a category archive page, you shouldn't be doing anything to single.php. You can read more about template hierarchy in the Codex.
Related
On my wordpress page template Im trying to use the get_title() in my arguments array for the a loop but i cant get it to return any posts.
This is where im up to;
<?php
$args = array(
'category_name' => 'the_title();'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
Im unable to get any posts to return on this page unless i manually enter the slug for the category i want to use which is not ideal as the page slug will always be the same as the post category slug.
My logic behind this is that i dont want to have an individual page templeate for each page with the different cat as i will end up with hundreds of page templetes.
Any help appreciated
Cheers
Jon
You're passing the_title(); as a string do to ' '. Also, the_title() gets title of the object in the loop, not of a category.
You can do the following:
global $post;
$args = array('category_name' => $post->post_name);
You have passed the_title(); as a string, so will be treated as string, not a function, so it will not return title. You can use global variable $cat, So your code will be,
<?php
global $cat;
$args = array('cat' => $cat);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
Been researching a pagination issue I am having with WordPress. My page-foobar.php file is:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$foo_query = new WP_Query( $foobar_args );
if ( $foo_query->have_posts() ) : ?>
<?php while ( $foo_query->have_posts() ) : $foo_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
I added:
<nav>
<?php previous_posts_link('« Newer',$foo_query->max_num_pages); ?>
<?php next_posts_link('Older »',$foo_query->max_num_pages); ?>
</nav>
after referencing this answer after the endif;:
The link shows in the url as site/foobar/page/2/ but displays the 404 page. After some further research I ran across an article that mentioned the page-foobar.php and posttype-foobar.php names can clash so to rename one of them. I renamed posttype-foobar2.php, changed my functions.php, tested and still get sent to the 404 page with the correct URL. Another article mentioned that you must use wp_query() so I changed my page-foobar.php to:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
pagination:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
I did test this by adding this before <?php wp_reset_postdata(); ?> and after <?php endwhile; ?>:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
tested and I still get sent to a 404 page. When I change next_posts_link() to <?php var_dump(next_posts_link('« Older Entries', $wp_query->max_num_pages)); ?> it returns NULL. If I change next_posts_link() to get_next_posts_link() I get string(99). What am I doing wrong with my pagination?
Further Referenced:
Wordpress pagination (next_posts_link) on custom wp_query not showing
next_posts_link 404 error
WP_Query and next_posts_link
How can I properly get my pagination to go to the next page?
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
After doing some further research I ran across the solution to my problem and I hope it can help someone else. The answer submitted by Sindhu does have merit in respect to the addition of:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
and the modification to the arguments:
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
Codex for get_query_var(). After making this additional change it still didn't work so I started testing my Permalinks. When I ran the code under Default the pagination worked. When I changed to Post name and saved twice it still didn't work. After browsing I decided to search for custom post type giving 404 error and I ran across "Fixing Custom Post Type 404 Errors In WordPress" which mentioned I could be creating a double slug. After review I was in fact doing so and WordPress was clashing with the double slug. I decided to changed my posttype-foobar.php to posttype-foobar_t.php and updated my functions.php file. Instead of modifying the database on my localhost to fix the slug issue I decided to install a clean installation with the modified changes and it works.
I would also like to point out some of my findings in regards to a large majority of posts mentioned you have to call the query as $wp_query = new WP_Query( $foobar_args );.
I'm just getting back into Wordpress and php after a few years away. I'm pretty ignorant when it comes to php syntax so making lots of mistakes.
I need to display a single post on a page, it's title and contents, and it needs to be specific to a category that I've created.
I grabbed this of the web and tried to change it, but I can't get it to work.
<?php query_posts('category_name=qaadrant&showposts=1');
while (have_posts()) : the_post();
// do whatever you want
?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
Any help much appreciate. Niall
Please use awesome WP_QUERY Class see the reference here:
https://codex.wordpress.org/Class_Reference/WP_Query
Using following code to get posts from category you selected. No-paging is set to make query faster:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'qaadrant',
'no-paging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php echo get_the_title(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!
I want to build an author.php which displays two different loops:
The #1 Loop shall display all Posts from an Author which are made with the Custom Post Type "News".
The #2 Loop shall displayy all Posts from the Author which are made through a normal Post.
Here is my Code:
<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
This line will identify the current Author... And here are my Loops:
1
<?php
$args = array( 'post_type' => 'news','author=$curauth->ID','posts_per_page' => -1 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
2
<?php
$query = new WP_Query(array( 'post_type' => 'post','author=$curauth- >ID','posts_per_page' => -1 ));
while ( $query->have_posts() ) : $query->the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
The Problem with this is the >>'author=$curauth- >ID'<< doesn´t work in the string, i guess. It outputs ALL the posts and doesn't separate between different authors.
Maybe you can try to use usual WP loop whith
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
And add:
wp_reset_query();
after first list.
In this case I don't think you need custum WP_QUery and if you keep it sample this may remove some bug or error.
Hope this will help