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
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; ?>
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'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'm trying to get the category name to show above posts. I'm customizing bones and am a little stuck.
Here's the code that has my posts displaying, and I have each in a category in the wp admin. I'm just not sure how to get the category to display above the appropriate posts.
Hope this makes sense. Thanks in advance!!
<?php
$args = array( 'post_type' => 'custom_type', 'posts_per_page' => 100 );
$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>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
It looks like you are doing a custom loop (as opposed to "The loop"), hence you'll need to pass the id of the post to get_the_category. However, judging by the docs, it seems that WP_Query->the_post() sets the global post.
So, if this does not work:
<h2><?php echo get_the_category() ?></h2>
You would do:
$postsQuery = new WP_Query( $args );
$posts = $postQuery->get_posts();
foreach ( $posts as $i => $e ) {
$category = get_the_category($e->ID);
}
https://developer.wordpress.org/reference/functions/get_the_category/
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/The_Loop
You can use like <?php the_category();?>. This will returns the categories name with link. Hope this help you!
Hi I could not post in comments, but if above answer is working for you as you posted in comments but having issue with getting category as it give array due to many categories of a post, so you need to fetch first category from the array. Use below code snippet:
if ( ! empty( $category ) ) {
echo '' . esc_html( $category[0]->name ) . '';
}
Well I can't figure this one out...
I have this Wordpress I use as a photo gallery blog.
I have a basic setup using the main default loop for posts.
Like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; ?>
<b>Not Found</b>
<?php endif; ?>
In the sidebar and where ever, I want to appear random posts.
I've managed to do that. With this:
<?php query_posts($query_string . 'showposts=1&orderby=rand'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; endif; ?>
It looks amazing! In theory.
There are duplicate posts all over the place. And that just looks stupid.
I have read lots of articles but I just can't seem to get it to work :(
Any help would be much appreciated.
Try this code for random post.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Or You can get help from this url mention below
http://codex.wordpress.org/Template_Tags/get_posts
After a good night of sleep, here's what I have done:
Creating array with post ID:
<?php $already_posted = array(); ?>
The Main loop where at the end I record the post ID to array:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php $already_posted[]= $post->ID; endwhile; ?>
<?php else : ?>
<b>Not Found</b>
<?php endif; ?>
And the random post code using post__not_in to avoid duplicates and again recording post ID:
<?php $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => $already_posted );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
//the post
<?php $already_posted[]= $post->ID; endforeach; ?>
Works evertime!
You can do amazing stuff with this :)
Thanks to paislee and Arvind Pal for helping out.
Skip would-be duplicates by remembering displayed ID's from the first loop
$displayed = array(); // create an array that we'll use associatively
In your first loop, each time:
$displayed[get_the_ID()] = TRUE; // <-- save all post IDs in here
Change your random loop opening like this:
<?php if (have_posts()) : while (have_posts()) : the_post();
// skip post IDs you've already seen
if ($displayed[get_the_ID()]) continue;
?>
Due to randomness in the number of duplicates, you may want to alter your query so that it gets all posts, and change the second loop to break once the desired number of random posts is reached.
Notes
showposts is depracated. Replace showposts=1 with posts_per_page=-1