I'm using the default WordPress comment system and in my comment template, I use
<?php wp_list_comments() ?>
to generate the comments and I was wondering how I can modify the comment date? Currently, it displays the full date as such February 11, 2014 at 6:27 am. After looking at the WordPress codec it doesn't seem like I can modify the date format through the wp_list_comments args.
How can I modify the date format output?
You have two options:
1. You can try to modify the wordpress core file wp-includes/comment-template.php. to modify the date format you wish to have(This might seem easier but i won't suggest you to modify the core files )
2. you can create a custom callback function to display your comment as below:
You would call the function
now you can create your custom function my_custom_comment in your function.php file and it will simply replace the default listing with your custom listing format.
Now create a function named my_custom_comment in your function.php and modify the date format
For more details on wp_list_comment take a look at wp_list_comment())
function my_custom_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
extract($args, EXTR_SKIP);
if ( 'div' == $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>
<<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
<?php if ( 'div' != $args['style'] ) : ?>
<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
<?php endif; ?>
<div class="comment-author vcard">
<?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
</div>
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
<?php
/* translators: 1: date, 2: time */
printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
?>
</div>
<?php comment_text(); ?>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div>
<?php if ( 'div' != $args['style'] ) : ?>
</div>
<?php endif; ?>
In your function under comment find these:
sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
or
printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time())
then remove the %2$s and , get_comment_time() something like these:
sprintf( __( '%1$s', 'twentytwelve' ), get_comment_date() )
or
printf( __('%1$s'), get_comment_date())
again if you want to modify the date use the reference below:
http://codex.wordpress.org/Formatting_Date_and_Time
usage:
get_comment_date('D, F j')
I hope this is something you like.
Using the WordPress Codec documentation, you add a callback function as an argument to wp_list_comments.
<?php
wp_list_comments( array(
'style' => 'ul',
'callback' => 'custom_comment_template'
));
?>
Copy comment template from wp-includes/class-walker-comment.php. And modify this section to suit your need. You can use the DATE and TIME Formatting for WordPress here.
function custom_comment_template(){
// Codes you have copies from wp-includes/class-walker-comment.php
// Or any design you want to use. You can use the hooks and filters
// from wp-includes/comment-template.php like
get_comment_author_link, get_comment_author
<div class="comment-meta commentmetadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* translators: 1: Comment date, 2: Comment time. */
printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
?>
</a>
<?php edit_comment_link( __( '(Edit)' ), ' ', '' ); ?>
</div>
}
Related
I use traded comments so each comments receive multiple replies in it. I need to get replies count of each individual comment. I don't need the total replies count of all the comments.
I tried to use this code:
$comment_id = get_comment_ID();
$childrenCount = get_comment_meta( $comment_id, "childrenCount" );
$childrenCountNumber = count($childrenCount);
echo $childrenCountNumber
The other way round that shows total replies which not what I'm looking for.. This code shows all the replies of all the comments.`
Code in functions.php
function replies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments
WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND
`comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}
Then echo out in the template page:
$parents_count = replies_counter($post->ID);
$children_count = $post->comment_count - $parents_count;
echo $children_count;
This one is all about getting total replies count of all the comments. But I need replies count of each comment individually.
It will be nice if someone help me.
I always prefer to use a WordPress core function, if there is one to do what I want.
You can easily achieve that using get_comments. With the countarg set to true. This way it will retrieve only the comments count.
$args = array(
'post_id' => $post->ID, //main post id
'parent' => get_comment_ID(), //the comment id
'count' => true, //just count
);
$comments = get_comments($args); //number of comments;
why don't you write such a custom function to get subcomments for each individual comment by ID like this?
function subreplies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_ID) AS count FROM $wpdb->comments
WHERE `comment_approved` = 1 AND `comment_parent` = $id";
$data = $wpdb->get_row($query);
return $data->count;
}
Here are the code in comment.php
<?php
/**
* The template for displaying comments.
*
* This is the template that displays the area of the page that contains both the current comments
* and the comment form.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Astra
* #since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<h3>{<?php echo get_comments_number(); ?><span style="color:grey;font-size:16px"> comments</span>}</h3>
<?php astra_comments_before(); ?>
<?php if ( have_comments() ) : ?>
<div class="comments-count-wrapper">
<h2 class="comments-title">
<?php
$comments_title = apply_filters(
'astra_comment_form_title',
sprintf( // WPCS: XSS OK.
/* translators: 1: number of comments */
esc_html( _nx( '%1$s thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'comments title', 'astra' ) ),
number_format_i18n( get_comments_number() ),
get_the_title()
)
);
echo esc_html( $comments_title );
?>
</h2>
</div>
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav id="comment-nav-above" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
<h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
<div class="nav-links">
<div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
<div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>
</div><!-- .nav-links -->
</nav><!-- #comment-nav-above -->
<?php endif; ?>
<ol class="ast-comment-list">
<?php
wp_list_comments(
array(
'callback' => 'astra_theme_comment',
'style' => 'ol',
)
);
?>
</ol><!-- .ast-comment-list -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav id="comment-nav-below" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
<h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
<div class="nav-links">
<div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
<div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>
</div><!-- .nav-links -->
</nav><!-- #comment-nav-below -->
<?php endif; ?>
<?php endif; ?>
<?php
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
?>
<p class="no-comments"><?php echo esc_html( astra_default_strings( 'string-comment-closed', false ) ); ?></p>
<?php endif; ?>
<?php comment_form(); ?>
<?php astra_comments_after(); ?>
</div><!-- #comments -->
I'm using Types plugin for create a Wordpress Custom Post Type (CPT), now I need to display that content. I have created a page called: category-legislaciones.php where legislaciones is the category, from the CPT itself, created using Types plugin. This is the code I have on that page:
<?php
$args = array( 'post_type' => 'legislaciones', 'posts_per_page' => 15 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="post-title"><?php the_title(); ?></h1>
<?php $options = get_option( 'responsive_theme_options' ); ?>
<?php if ( $options['breadcrumb'] == 0 ): ?>
<?php echo responsive_breadcrumb_lists(); ?>
<?php endif; ?>
<div id="post-<?php the_ID(); ?>">
<div class="post-entry">
<?php the_content( __( 'Read more ›', 'responsive' ) ); ?>
<?php wp_link_pages( array(
'before' => '<div class="pagination">' . __( 'Pages:', 'responsive' ),
'after' => '</div>'
)); ?>
</div>
<?php if ( comments_open() ) : ?>
<div class="post-data">
<?php the_tags( __( 'Tagged with:', 'responsive' ) . ' ', ', ', '<br />' ); ?>
<?php the_category( __( 'Posted in %s', 'responsive' ) . ', ' ); ?>
</div><!-- end of .post-data -->
<?php endif; ?>
<div class="post-edit"><?php edit_post_link( __( 'Edit', 'responsive' ) ); ?></div>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php comments_template( '', true ); ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<?php if ( function_exists( 'wp_paginate' ) ) {
wp_paginate();
} ?>
<?php endif; ?>
<?php get_search_form(); ?>
<?php endwhile; ?>
But nothing is display even if I have content assigned to that category, what I am doing wrong? What is the right way to display that contents?
It looks like you are mixing
Custom Post Type (post_type => 'post_type' => 'legislaciones')
and Custom Taxonomy,
or do you really have both Custom Post Type and Custom Taxonomy with the same slug ?
If your legislaciones is a Custom Post Type, the proper name for your file would be archive-legislaciones.php and you don't need a specific query
If your legislaciones is a Custom Taxonomy, then here you are basically calling for the posts with post_type legislaciones and the Taxonomy legislaciones with a value which would be determined in the url
This is all detailed in the Codex, the page about Template Hierarchy
It does not belong to your question, but your code will repeat breadcrumb and comment form 15 times on the page, is this really what you want ?
I have a code for Archive.php page inside Wordpress but i have hard time figuring out how to trigger particular if statement (is_year() and is_month() ). For example i would like to output Yearly Archives on my page but I have no idea how to achieve that. I tried changing permalinks and changing date format inside "General Settings" but no matter I do it always outputs: "Monthly archives:".
So What do I need to change to trigger that particular output? (Daily Archives: or Yearly Archives:)
<header class="page-header">
<h1 class="page-title">
<?php if ( is_day() ) : ?>
<?php printf( __( 'Daily Archives: %s', 'domain' ), '<span>' . get_the_date() . '</span>' ); ?>
<?php elseif ( `is_month()` ) : ?>
<?php printf( __( 'Monthly Archives: %s', 'domain' ), '<span>' . get_the_date( _x( 'F Y', 'monthly archives date format', 'domain' ) ) . '</span>' ); ?>
<?php elseif ( is_year() ) : ?>
<?php printf( __( 'Yearly Archives: %s', 'domain' ), '<span>' . get_the_date( _x( 'Y', 'yearly archives date format', 'domain' ) ) . '</span>' ); ?>
<?php else : ?>
<?php _e( 'Blog Archives', 'domain' ); ?>
</h1>
</header>
<?php endif; ?>
<?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content( __( 'Read more', 'domain' ) ); ?>
</div><!--/entry-->
</div><!--/post_class-->
<?php endwhile; ?>
<?php endif; ?><!--END if THE LOOP-->
I do not see any problem with your conditionals. What do you get displayed, when you access your site like so example.com/2013, you get yearly archive, elseif example.com/2013/11 monthly archive, elseif example.com/2013/11/19, daily archive? (If a particular archive does not exists, you get 404, not empty archive)
I receive this error Parse error: syntax error, unexpected T_DEFAULT in ../../functions.php on line 22
Here is functions.php section around line 22("default" specifically is line 22):
<?php
break;
default :
?>
It's default code from Wordpress' TwentyEleven functions.php, and unfortunately I'm still very much learning PHP so I'm not at all sure why the error is occurring. Any ideas?
ETA here is the full functions.php
if ( ! function_exists( 'twentyeleven_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own twentyeleven_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* #since Twenty Eleven 1.0
*/
function twentyeleven_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
$avatar_size = 68;
if ( '0' != $comment->comment_parent )
$avatar_size = 39;
echo get_avatar( $comment, $avatar_size );
/* translators: 1: comment author, 2: date and time */
printf( __( '%1$s on %2$s <span class="says">said:</span>', 'twentyeleven' ),
sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
sprintf( '<time pubdate datetime="%2$s">%3$s</time>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
/* translators: 1: date, 2: time */
sprintf( __( '%1$s at %2$s', 'twentyeleven' ), get_comment_date(), get_comment_time() )
)
);
?>
<?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .comment-author .vcard -->
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyeleven' ); ?></em>
<br />
<?php endif; ?>
</footer>
<div class="comment-content"><?php comment_text(); ?></div>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>↓</span>', 'twentyeleven' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</article><!-- #comment-## -->
<?php
break;
endswitch;
}
endif; // ends check for twentyeleven_comment()
I ran into this problem with the code below. Note: the use of "case default", this is incorrect.
switch($vars) {
case "0":
print '1 comment';
break;
case default:
print "$vars comments";
}
The code should be as follows (default only):
switch($vars) {
case "0":
print '1 comment';
break;
default:
print "$vars comments";
}
Try to delete default:. And make sure that this line of code inside
switch(){
}
That seems to be part of the switch statement which usually take the following structure:
switch($aaa) {
case 1:
//do something
break;
case 2:
//do something else
break;
case 3:
...
...
...
default:
/do something else again
break;
}
You can get all the info you need here http://php.net/manual/en/control-structures.switch.php
I made an oversight when copy/pasting the code; forgot the opening <?php and closing ?>.
How to separate comments and pingbacks to the Twenty Ten theme? (default wordpress theme)
According to the functions.php
function twentyten_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard">
<?php echo get_avatar( $comment, 40 ); ?>
<?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author .vcard -->
<?php if ( $comment->comment_approved == '0' ) : ?>
<em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<?php
/* translators: 1: date, 2: time */
printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
?>
</div><!-- .comment-meta .commentmetadata -->
<div class="comment-body"><?php comment_text(); ?></div>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</div><!-- #comment-## -->
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
I was google and have tons results, but all in old version. Let me know if can be done.
It depends on the version of wordpress you are using, not on the theme.
Maybe this is helpful: http://themocracy.com/2010/02/separate-pingbacks-user-comments/