admin管理员组文章数量:1130349
Hello I have a query that gets a post with id=x and it works but it leaves out the comment box.
Is there a way to add "get comment box" to the query?
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Hello I have a query that gets a post with id=x and it works but it leaves out the comment box.
Is there a way to add "get comment box" to the query?
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Share
Improve this question
edited Mar 1, 2012 at 4:26
Jared
3,8453 gold badges35 silver badges58 bronze badges
asked Mar 1, 2012 at 2:53
xyzxyz
2152 gold badges7 silver badges14 bronze badges
2 Answers
Reset to default 0First of all, welcome to WPSE!
By default, the comment box does not come with the post object, what you are looking for is the comments_template function.
It should be as simple as changing your code to this:
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
comments_template();
?>
But if I'm correct this would only work for the current post in the loop.
Looking at the function in wp-includes/comment-template.php, it uses global $post and global $wp_query variables. You would need to modify that value in order for the comments template to show up for the post you are displaying.
You could modify that using query_posts instead of get_post, but be sure to reset the query afterwards:
$post_id = 104;
query_posts( array( 'p' => $post_id ) );
while( have_posts() ) : the_post();
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
comments_template();
endwhile;
wp_reset_postdata(); // Don't forget!
I haven't tested this but it should work. I would also suggest reading this awesome answer by Rarst about when to use what function to "get posts". :)
With this query you can add the comments.
function get_comments( $args = '' ) {
$query = new WP_Comment_Query;
return $query->query( $args );
}
Hello I have a query that gets a post with id=x and it works but it leaves out the comment box.
Is there a way to add "get comment box" to the query?
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Hello I have a query that gets a post with id=x and it works but it leaves out the comment box.
Is there a way to add "get comment box" to the query?
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Share
Improve this question
edited Mar 1, 2012 at 4:26
Jared
3,8453 gold badges35 silver badges58 bronze badges
asked Mar 1, 2012 at 2:53
xyzxyz
2152 gold badges7 silver badges14 bronze badges
2 Answers
Reset to default 0First of all, welcome to WPSE!
By default, the comment box does not come with the post object, what you are looking for is the comments_template function.
It should be as simple as changing your code to this:
<?php
$post_id = 104;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
comments_template();
?>
But if I'm correct this would only work for the current post in the loop.
Looking at the function in wp-includes/comment-template.php, it uses global $post and global $wp_query variables. You would need to modify that value in order for the comments template to show up for the post you are displaying.
You could modify that using query_posts instead of get_post, but be sure to reset the query afterwards:
$post_id = 104;
query_posts( array( 'p' => $post_id ) );
while( have_posts() ) : the_post();
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
comments_template();
endwhile;
wp_reset_postdata(); // Don't forget!
I haven't tested this but it should work. I would also suggest reading this awesome answer by Rarst about when to use what function to "get posts". :)
With this query you can add the comments.
function get_comments( $args = '' ) {
$query = new WP_Comment_Query;
return $query->query( $args );
}
本文标签: Query get posthow to add comment box
版权声明:本文标题:Query get post,how to add comment box 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749057852a2309634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论