I'm Using WP 3.4.2 , a child theme of twentyeleven. I have created a category called "featured media".
I want to get and display posts which have this category.
I checked around before coming here, I found variations of the same answer, in forums. Generally I am advised to write:
$args = array('category'=> x);
get_posts($args);
I don't know the category id. Some forums have advised me to go to Dashboard--> posts -->categories, find my category and hover over the "Edit" link, and read the category id from the status bar. My browser shows me this address: localhost/myblog/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=7&post_type=post
This apparently means my category id is 7. The above code returns and empty array. This has become really frustrating for me. I believe the method is correct. Can anyone tell me what I'm doing wrong?
Are you setting global $post?
Heres the WordPress example from their website:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
http://codex.wordpress.org/Template_Tags/get_posts
Related
For years on the front page of my site, I have had a list of all posts from four categories.
Old Front Page
This week, my code stopped working, so I am trying to replace it. I have come up this code, but it doesn't work. It knows there are three posts in the category, but it shows the most recent post three times rather than the three posts.
`<?php $args = array( 'category' => 62, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<li> • <?php the_title(); ?></li>
<?php endforeach; ?> `
This code is just for one of the categories, it is repeated 4 times. The text is used as HTML in a text widget. How do I get this code to show the three posts rather than the most recent three times? Thanks!
I'm trying to hack a WordPress Theme that uses WP_Query for a "Recent Posts" widget. Currently it is limited to return only a specific Category, while I'd like it to return a limited number of posts from all categories. I have tried and succeeded at this before with other Plugins/Themes. In this case it doesn't work, irrespective of what change I try to implement, I get 4000+ posts in the query.
<?php $the_query = new WP_Query('cat='.$category.'&showposts='.$postnum);
while ($the_query->have_posts()) : $the_query->the_post();?>
What could be the common culprit of not having showposts or post_per_page working correctly as a limiter?
are you able to get a correct output from your variable $postnum ?
also try using this
$args = array('category_name' => 'my-category-slug', 'posts_per_page' => 3);
<?php
query_posts( $args );
while ( have_posts() ) : the_post();
echo 'content';
endwhile;
wp_reset_query();
?>
Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?
Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>
There is plenty of info out there on changing post categories, just nothing relating specifically to changing post categories after a comment is made.
I'd like to have a section of my site showing only posts with comments.
I've tried adding this code I found on this website for changing tags, in about a dozen different places
<?php wp_set_object_terms( '<?php the_ID(); ?>', 'new', 'category', 'true' ); ?>
I've tried using it in
<?php if ( have_comments() ) : ?>
<?php endif; ?>
I've tried using wp_insert_comment and comment_post hooks with other codes around the internet, with no success.
I guess the same thing could be achieved with adding tags to posts after a comment is made but I've no idea where to start there.
Have any of the experts on here ever seen a wordpress blog with a seperate area only showing posts with comments? I'm no coder, I might be attempting the impossible without even being aware of it.
Any help appreciated.
You can use comment_post action hook. This function can't take post ID or $post object as parameters, so you have to add global $post; to get access to it.
From wp-includes/comment.php:
do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
Example usage
//in functions.php
add_action( 'comment_post', 'so_custom_comment_post' );
function so_custom_comment_post(){
global $post;
//Be sure the term 'new' is already available
wp_set_object_terms( $post->ID, 'new', 'category', true );
}
Hope it helps! Let me know if you get stuck.
<?php
$args = array('post_type' => 'post');
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
//display posts with comments
$comments = get_comments(array(
'post_id' => $post->ID));
foreach($comments as $comment) {
echo '<li>'.the_permalink().'</li>';
}
endwhile;
?>
I am using WordPress 3, and I created a custom post type called article, which gives me the URL format of mywebsite/articles/article-title. How do I see all the article entries in the URL mywebsite/articles?
Assuming you set up everything correctly and you want to see the post type on a public template page, try this code into mycustomtemplate.php or the equivalent.
<?php $loop = new WP_Query( array( 'post_type' => 'article', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
You can customize the loop just like you would blog posts and pages. If you want to get all on one page you'll want to remove the limit of 10 on post_per_page I wouldn't suggest it though. I would say set it to 50 or 100 and still use pages.
Source: Custom post types in WordPress
I have the simplest solution. Just create file archive-{custom post type}.php and then, just do loop content as usual.
If you already set the permalink, just type yourdomain.com/{custom post type}.
You can easily achieve this from the definition of your custom post type, starting with WP 3.1. Just set has_archive to true.
Source: http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types