Getting Permalink from Post ID in Wordpress 3 - wordpress

I'm building a small list of recent comments, and would like to make links to the actual posts that the comments were placed on. Unfortunately, there is no comment_permalink or post_permalink that I can find, so I thought maybe there would be a get_permalink() function, but again, none that I could find on http://codex.wordpress.org/Function_Reference/.
From the $post->ID alone, how can I find the permalink for that particular post? Not that it's completely necessary, but here is what I have so far:
<?php $comments = get_comments( array( 'status'=>'approve', 'number'=>5 ) ); ?>
<p class="recently-posted-comments">Recent Comments</p>
<ul>
<?php foreach ($comments as $comment): $parent = get_post($comment->comment_post_ID); ?>
<li><?php print $comment->comment_author; ?>
on <?php print $parent->post_title; ?></li>
<?php endforeach; ?>
</ul>
My intent is to convert the $parent->post_title into a permalink.

I thought maybe there would be a get_permalink() function, but again, none that I could find.
http://codex.wordpress.org/Function_Reference/get_permalink
I'd also recommend using that over get_page_link()
get_permalink() checks the post type and returns the result of the appropriate function;
Pages use get_page_link()
Attachments use get_attachment_link()
Custom post types use get_post_permalink()

The confusion comes as a result of ambiguous function names. I was looking for something that suggested a link for a "post," yet found nothing. Out of curiousity, I came across and tested get_page_link(), only to find that it does exactly what I was looking for.
Unfortunately I assumed that "page" was an exclusive term reserved for pages in wordpress, rather than posts. It appears in this context it is representative of both.

Related

Wordpress - Two loops on author.php?

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(); ?>

Display all comments for a WordPress blog on a single page?

What I need - The code to perform the following:
I am trying to setup a WordPress template that will display all the comments that have been posted to my blog. How do I pull all comments and have all the same formatting that is applied to comments under a single post? Such as the formatting that occurs when comments are displayed using the comments.php template.
Note I want to pull all the comments from my blog to a single page. I still want the comment pagination but instead of having 20 comments under post #1, 20 under post #2, etc. I want to have all 40 show up at one time on one page.
You want to use the get_comments() function.
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
See also the apply_filters() function to apply comment output filters to specific fields.
<?php echo apply_filters('comment_text', $comment->comment_content); ?>
EDIT:
For pagination, you can use the offset and number parameters of the get_comments() arguments:
<?php
$args = array(
'number'=>20,
'offset'=>0,
'status'=>'approve',
);
foreach (get_comments($args) as $comment) {
// ...
}
?>

Wordpress search field affecting custom query

Just came across an issue with my Wordpress template that I can't figure out. I am using a custom built menu (done simply with a quick query_posts() call), but when searching for certain terms, my query is affected. Not a clue as to why.
Here's my menu code:
<?php $main_cats=explode(",",$options['main_cats']); ?>
<?php $myargs = array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'); ?>
<?php query_posts($myargs);
while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_query();?>
This should end up with 4 menu items. However, when searching for a few "job" related items, it seems that the Job Manager plugin steps in (http://pento.net/projects/wordpress-job-manager-plugin/) and I get one menu result stating "This job doesn't exist". However, I don't understand how that plugin could possible affect my query.
Any thoughts?
This code assume that $main_cats has the right data, before testing this, check that the variable is correct:
<?php
$main_cats=explode(",",$options['main_cats']);
$menu_items=get_posts(array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'));
foreach($menu_items as $menu_item){ ?>
<li><?php echo $menu_item->post_title; ?></li>
<?php } ?>
Maybe Use native menu function to to create menu's, http://codex.wordpress.org/Function_Reference/wp_nav_menu
Don't use query_posts($myargs), query_posts() is meant for altering the main loop.
Use WP QUERY http://codex.wordpress.org/Class_Reference/WP_Query (or get_posts).
Try wp_reset_query(); before your query as well as after, like you have. I think that the plugin query is happening before your yours. There may be some carry over from that. Reseting before your query might give you a clean slate.

WordPress default number of results returned

For some reason this query is only returning the 10 most recently published results, there should be at least 15 or so. So my question is, if I don't specify the number of results in my query, will WordPress default to 10 or something?
<?php getGallery('test', 'test2'); ?>
function getGallery($galleryLink, $altLink){
global $post;
// search for any pages with a custom field of 'test'
query_posts('meta_key='.$galleryLink.'&post_type=page');
if (have_posts()) while (have_posts()) : the_post();
$link = get_post_meta($post->ID, $galleryLink, true);
$alt = get_post_meta($post->ID, $altLink, true);
$permalink = get_permalink($id);?>
<a href='<?php echo $permalink ?>'><img src="<?php echo $link ?>" alt="<?php echo $alt ?>"/></a>
<?php endwhile;
wp_reset_query();
}
default number of returned posts in $wp_query is defined in Dashboard > Settings > Reading. There you have defined 10, but remember that other people can set diffrent number on their blogs.
i hope you know that if you want to define your own number of posts you should add posts_per_page=X to query :) if you want to always return all posts matched to query put -1 in place of X
You should be able to modify the default value of 10 to any number you specify with the posts_per_page parameter in the query, like so:
query_posts('meta_key='.$galleryLink.'&post_type=page&posts_per_page=20');
Sub in any value you like instead of 20. Better yet, use a variable and stay away from literals.
Yes. The standard Loop query shows the number of posts defined in your Reading Settings page in WordPress, which defaults to 10.
See the examples under "Pagination Parameters" in the WP_Query documentation if you want to alter this programatically, rather than relying on the setting. For example:
$query = new WP_Query( 'posts_per_page=-1' );
...will retrieve all posts.
If this is your default loop, just go to Settings --> Reading and change "Blog pages show at most" to your desired number. If you specify a number that is less than your total amount of posts you'll have to include some sort of navigation to allow the visitor to go previous posts.

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources