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(); ?>
Related
I've poured over dozens of posts, and the documentation, nothing has helped.
I have a very simple problem: I want to query for all posts of some category, then split up the results into pages. I have tried a few different things, but essentially the next_posts_link() and get_next_posts_link() never returns anything.
Latest code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged,
'cat' => 135
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="med-item talks-texts">
<p class="bold"><?php the_title(); ?> (<?php the_time('Y'); ?>)</p>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
<?php next_posts_link(); ?>
Add parameters to the function. In your case:
<?php echo get_next_posts_link('Older', $query->max_num_pages);?>
<?php echo get_previous_posts_link('Newer', $query->max_num_pages);?>
It looks to me as if the next_post_link() needs to be within the loop. You have it outside. There's more on this exact use-case in the WP docs for that function here:
https://developer.wordpress.org/reference/functions/next_posts_link/#div-comment-1297
Note the extra parameter needed for a custom WP_Query, as that's what you're running here. In short:
// next_posts_link() usage with max_num_pages.
next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
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 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.
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