Is there anyway to list the post IDs for all the posts in the loop on a page in Wordpress?
Id like to make a navigation bar out of the current posts on the page.
Wordpress 3.3
Thanks!!
Use query_posts() to get all posts and loop through it for ids, example:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
Related
I have a single.php file like below
<?php get_header(); ?>
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo '<div">';
the_content();
echo '</div>';
} // end while
} // end if
?>
<?php get_footer(); ?>
now I need to get Current Custom Post Type Associated Taxonomy Term as link on the top of the page so if users click on the link the page navigate to taxonomy.php.
Can you please let me know how to do this?
Thanks
You can use get_the_term_list within your loop to get the associated taxonomy term links:
global $post
$terms = get_the_term_list($post->ID, 'your_taxonomy');
echo $terms;
This was a good solution for me as I only ever had a single taxonomy term associated to each post.
I need to get some posts by category and display them out of the wordpress. I need these posts hidden in the wp blog. But visible in my custom page. Via php.
I'm noob with wordpress. Can anyone direct me about how can I do that?
Your file should be something like this:
<?php
require '/path/to/wordpress/root/wp-load.php';
$the_query = new WP_Query( 'post_status=private' );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
?>
Read more about WP_Query class to get posts which you need.
I'm trying to edit my author.php wordpress template so that it shows posts by any one author, but only from one particular category. So far, I've been trying the query_posts function which fetches the category okay, but not the author. Depending on which way I do it, so far the posts either don't display at all or all posts in that category appear regardless of the author.
This is the appropriate code which I've seen quoted by a wordpress.org admin, but it doesn't work for me and I can't find any other examples. Any ideas why that doesn't work? Thanks for your help in advance.
//Gets author info to display on page and for use in query
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
//Queries by category and author and starts the loop
<?php
query_posts('category_name=blog&author=$curauth->ID;');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
//HTML for each post
<?php endwhile; else: ?>
<?php echo "<p>". $curauth->display_name ."hasn't written any articles yet.</p>"; ?>
<?php endif; ?>
============ ALSO TRIED ============
<?php
new WP_Query( array( 'category_name' => 'blog', 'author' => $curauth->ID ) );
?>
This doesn't work either, however it does filter the posts by author, just not by category! What am I doing wrong?
Thanks!
This task can be done using pre_get_posts filter. By this way it's also possible to filter for author in addition than for category:
// functions.php
add_action( 'pre_get_posts', 'wpcf_filter_author_posts' );
function wpcf_filter_author_posts( $query ){
// We're not on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
// We're displaying an author post lists
// Here you can set also a specific author by id or slug
if( $query->is_author() ){
// Here only the category ID or IDs from which retrieve the posts
$query->set( 'category__in', array ( 2 ) );
}
}
}
I just had this same issue, which I solved using a check for if(in_category('blog')) after the loop started, like this:
if ( have_posts() ) : while ( have_posts() ) : the_post();
if(in_category('blog')) {
?>
<!-- Loop HTML -->
<?php } endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
Of course the $curauth check would come before this.
How can I call certain posts by their number to place them as featured posts in specific places of my template ?
I've read about sticky posts , But I have about 3 places to place my featured posts and I guess I need a loop to get posts by their number I defined.
Any HELP ?!
<?php
// The Query
$query = new WP_Query( 'p=7' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.