random comments on page wordpress - wordpress

my question is, how can i show random comments on a page on wordpress? in my website i got a wp page where people leaves lots of comments, i want them to show randomly and not assorted by date time, here's the code of the paginated comments, what should i do? thanks :)
<?php foreach ($comments as $comment) : ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<div class="paginated-comments-number" style="float: left; color: #999; width: 30px; text-align: left;"></div>
<?php
if ( function_exists('get_avatar') )
echo get_avatar( $comment, 48 );
?>
<cite><?php comment_author_link() ?>
</cite>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?><br />
<small class="commentmetadata"><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('edit',' ',''); ?></small>
<?php comment_text() ?>
</li>
<?php
/* Changes every other comment to a different class */
$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<p>
<!-- Start Paginated Comments Pages -->
<?php if ( Paginated_Comments_have_pages() ) : ?>
</p>
<p>
<?php endif; ?>
<!-- End Paginated Comments Pages -->
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>

Here's a suggestion for a shortcode function that does what you're asking for:
add_shortcode( 'randomComment', 'randomComment_handler' );
function randomComment_handler($post_id) {
extract( shortcode_atts( array(
'post_id' => '0',
), $atts ) );
$out = "";
$comments = get_comments("post_id=$post_id&status=approve");
if ($comments) {
$ndx = mt_rand(0,sizeof($comments)) - 1;
$comment = $comments[$ndx];
$out = "<div class='randomComment'><div class='randomAuthor'>".$comment->comment_author."</div><div class='randomText'>".$comment->comment_content."</div></div>";
}
return $out;
}
This goes into your functions.php, and lets you put the shortcode on any page or post to show a random comment:
[randomComment post_id="1337"]
Just change the post_id according to the post you want to pull the random comment from.

Related

Instead of showing the latest posts, how to show the latest post from each category on the homepage in WordPress?

I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?
<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
//Post Title code
//Post Thumbnail code
//Post Content / Excerpt code
//Post Meta code
</article> <!-- /post -->
<?php if ( ! is_single() ) { ?>
</div>
<?php
<?php } ?>
It's very easy to add latest post from each category.
First of all get all the categories of blog by using below code:
$categories = get_categories();
Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.
Now you need to define the arguments for your query. Inside the braces, add this:
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Next, insert your query, using the WP_Query class:
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This will display each category posts in your home page. Please try to use it and let me know if you have any issue.

Hide featured image from post [Tesseract]

I would like to hide featured images from all posts. I tried Hide feautered image plug-in. Also some formulas from similar posts like:
.blog .entry-thumbnail { display: none; }
but it doesn't work.
My content single file is bellow:
<?php
/**
* #package Tesseract
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $featImg_pos = get_theme_mod('tesseract_blog_featimg_pos');
if ( has_post_thumbnail() && ( !$featImg_pos || ( $featImg_pos == 'above' ) ) )
tesseract_output_featimg_blog(); ?>
<?php //if ( my_theme_show_page_header() ) : ?>
<header class="entry-header">
<?php the_title( '<div id="blogpost_title"><h1 class="entry-title">', '</h1></div>' ); ?>
<?php
$postDate = get_theme_mod('tesseract_blog_date');
if ( $postDate == 'showdate' ) { ?>
<span><i class="fa fa-calendar" aria-hidden="true"></i><?php the_time('F j, Y'); ?></span>
<?php } ?>
<?php
$postAuthor = get_theme_mod('tesseract_blog_author');
if ( $postAuthor == 'showauthor' ) { ?>
<span><i class="fa fa-user" aria-hidden="true"></i><?php the_author(); ?></span>
<?php } ?>
<?php
$mypostComment = get_theme_mod('tesseract_blog_comments');
if ( ( $mypostComment == 'showcomment' ) && ( comments_open() ) ) { ?>
<span><i class="fa fa-comments-o" aria-hidden="true"></i><?php //comments_number('(No Comments)', '(1 Comment)', '(% Comments)' );?><?php comments_popup_link(
'No comments exist', '1 comment', '% comments'); ?></span>
<?php }
?>
</header><!-- .entry-header -->
<?php //endif; ?>
<?php if ( has_post_thumbnail() && ( $featImg_pos == 'below' ) )
tesseract_output_featimg_blog(); ?>
<div class="entry-content">
<div class="entry-meta">
<?php tesseract_posted_on(); ?>
</div><!-- .entry-meta -->
<?php if ( has_post_thumbnail() && ( $featImg_pos == 'left' ) ) { ?>
<div class="myleft">
<?php tesseract_output_featimg_blog(); ?>
<?php the_content(); ?>
<?php the_tags()?>
</div>
<?php } elseif ( has_post_thumbnail() && ( $featImg_pos == 'right' ) ){ ?>
<div class="myright">
<?php tesseract_output_featimg_blog(); ?>
<?php the_content(); ?>
<?php the_tags()?>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php } ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'tesseract' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
It is quite complicated, so do you guys know how to solve this?
Try commenting out the lines that say tesseract_output_featimg_blog();, so this:
<?php tesseract_output_featimg_blog(); ?>
becomes this:
<?php /* tesseract_output_featimg_blog(); */ ?>
I see 4 occurrences, and it looks like they're related to different featured image positions in the theme.
As a side note, changing your theme files isn't recommended unless you're doing it within a child theme.
Some themes give their users an option to choose if they really want to use a featured image for their post. If you have just started with your blog/website I would rather suggest to search for one such wordpress theme.
Also, in agreement with #Jonathan I would also suggest NOT to play around with your theme files. I tried changing theme files a couple of days ago, for the same reason as you have, and ultimately ended up breaking my theme code. As a result, I was unable to login to my blog. So, better be careful.

How to list only subpages title and link in a wordpress page div

I have a webblog using wordpress i'm using wp_list_pages() function to get list its Sub Pages Links (excluding main page) in a list, I've tried the below codes but its returning all Pages Sub Page.
I also tried this Argument child_of but its not happning. Please suggest i have tried so many things...
<?php
$output = wp_list_pages('echo=0&depth=1&child_of=0');
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
}
echo $output;
?>
This code will list the subpages and siblings of the current page:
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
}
else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div id="submenu">
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
U can try this code this code may be useful to u
<?php
global $wpdb, $post;
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) :
foreach ( $child_pages as $pageChild ) :
setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle">
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark"
title="<?php echo $pageChild->post_title; ?>">
<?php echo $pageChild->post_title; ?>
</a>
</h2>
<?php endforeach; endif; ?>

Wordpress Comment reply link does not appear

I am using custom code to print the comments but the problem is whatever i do, i cant print the comment reply link under any comment....
here is the code
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
}
/* This variable is for alternating comment background */
/*$oddcomment = 'class="alt" ';*/
$oddcomment = 'alt';
?>
<!-- You can start editing here. -->
<?php if ($comments) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
<ol class="commentlist">
<?php foreach ($comments as $comment) : ?>
<!--<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">-->
<li class="<?php echo $oddcomment; ?> <?php if ($comment->comment_author_email == get_the_author_email()) { echo 'author_comment'; } ?>" id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<cite><?php comment_author_link() ?></cite> Says:
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<br />
<small class="commentmetadata"><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('edit',' ',''); ?>
</small>
<?php comment_text() ?>
<div class="reply">
<?php comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) );
?>
</div>
</li>
<?php
/* Changes every other comment to a different class */
/*$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';*/
$oddcomment = ( empty( $oddcomment ) ) ? 'alt' : '';
?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
But when i use wp_list_comments functions i can see the reply link appear automatically i am using wordpress 3.2.1
It can be helpful to read the source for comment_reply_link (http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L1061), as there are multiple conditions that can lead to the link not appearing. Work through each one at a time, and you'll likely find your answer.
The one that tripped me up, is that the link will not appear if comments are closed. So it can be helpful to review the comment settings on the blog to make sure that providing replies is actually allowed on this post.
Try this:
comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth'])))
Thanks to the following codex-entry (http://codex.wordpress.org/Function_Reference/comment_reply_link), I managed to find the actual use of the comment_reply_link() on the following link: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L1061
This leaves me with the following question - did you try adding either the second or the third parameter for the function? I think there might be something fishy going on under the hood, making the comment-link not know where to actually link.
Try removing the following snippet
comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) );
and instead use the following one
comment_reply_link( array('reply_text' => 'Reply this comment'), comment_ID(), the_ID() );
Let me know if it works out for you!
Enable nested comments in Admin > Settings > Discussion:
Enable threaded (nested) comments levels deep

960gs different classes on teasers posts

I'm having hard time trying to use teasers post in my wordpress theme (based on 960gs), as you can see here http://img17.imageshack.us/img17/794/schermata20110420a15045.png what I got till now is one "featured" post and three teasers post with thumbnails that will probably be six (so it'll have seven posts displaied in the homepage). The problem is that to do so I have to assign a class "grid_2 alpha" to the teasers post and I don't know how to assign this class to just the first teaser on the left, lefting the other ones with no alpha or omega class and putting the omega class to just the last teaser post (the seventh).
If can help, here's the code I'm using for the loop:
<?php $firstClass = 'firstpost'; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $firstClass; ?>">
<?php $firstClass = 'grid_2 alpha'; ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" width="140" height="100" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>
also I would like to know how I can add some text above the teaser section under the first featured post. Sorry for the too many questions and for my bad english, as you can understand I'm not a developer but I searched for one week and couldn't find anything helpful for my problems. Thanks in advance for any help, I really appreciate it.
<?php $count = 0; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1):
$class = "firstpost';
elseif ($count == 2):
$class = "grid_2 alpha";
elseif ($count == $wp_query->post_count):
$class = "grid_2 omega";
else:
$class = "grid_2";
endif;
?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $class; ?>">
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>

Resources