carnold437
asked 5 years ago

Part 1: HERE IS HOW to link to Display Name and not the Username.
This is mainly for those who have security concerns or want to keep their pretty links consistent with the visually displayed names.
1) Navigate to dw-question-answer-pro\templates\styles\default
2-1) Edit content-single-answer.php & content-single-question.php
2-2) Search for the below line of code

<?php printf( __( '<span><a href="%1$s">%2$s%3$s</a> %4$s answered %5$s ago</span>', 'dwqa' ), dwqa_get_author_link( $user_id ), get_avatar( $user_id, 48 ), get_the_author(), dwqa_print_user_badge( $user_id ), human_time_diff( get_post_time( 'U', true ) ) ) ?>

2-3) Append your profile page location before %1$s.  For example, /user-profile/%1$s.
2-4) Replace the string dwqa_get_author_link( $user_id ) with get_the_author_meta(‘display_name’, $user_id)
Your code should now look like the below.

<?php printf( __( '<span><a href="/user-profile/%1$s">%2$s%3$s</a> %4$s answered %5$s ago</span>', 'dwqa' ), get_the_author_meta('display_name', $user_id), get_avatar( $user_id, 48 ), get_the_author(), dwqa_print_user_badge( $user_id ), human_time_diff( get_post_time( 'U', true ) ) ) ?>

3-1) Edit content-comment.php
3-2) Replace the entirety of the code below with the same generated code above.

<a href="<?php echo dwqa_get_author_link( $comment->user_id ); ?>"><?php echo get_avatar( $comment->user_id, 16 ) ?><?php echo get_comment_author() ?></a>
<?php dwqa_print_user_badge( $comment->user_id, true ); ?>

<?php printf( _x( ‘replied %1$s ago’, ‘%s = human-readable time difference’, ‘dwqa’ ), human_time_diff( get_comment_time( ‘U’, true ) ) ); ?>
4-1) Edit content-question.php
4-2) Replace the entirety of the code below with the same generated code above.  Not quite sure why or even how 3 separate and distinct functions were written for the same frontend display, but okay (just my .02).

<?php printf( __( '<span><a href="%1$s">%2$s%3$s</a> %4$s %5$s ago</span>', 'dwqa' ), esc_url( $latest_activity['userlink'] ), $latest_activity['useravatar'], $latest_activity['username'], $latest_activity['text'], $latest_activity['time'] ) ?>

5) Save all the files and navigate to your archives or a question page…and viola!
Note that it is recommended you use a child theme and not modify the files at their original destination as any updates will overwrite your changes.
===================================================================================================
Part 2: DWQA has a bug where a non-registered users user profile link is the current question or answer page.  As they have no profile, the href link is always blank.  Follow the below to fix this annoying issue.
1) Find the below line of code.  The bug here is that it by default gives all non-registered users the ID of 0.  They should instead have a null id, which is what this is going to trick.

<?php $user_id = get_post_field( 'post_author', get_the_ID() ) ? get_post_field( 'post_author', get_the_ID() ) : 0 ?>

2) Replace the who string with the below code.
<?php
$user_id = get_post_field( ‘post_author’, get_the_ID() );

if ( $user_id == 0 ) {
printf( __( ‘<span>%1$s%2$s %3$s answered %4$s ago</span>’, ‘dwqa’ ), get_avatar( $user_id, 48 ), get_the_author(), dwqa_print_user_badge( $user_id ), human_time_diff( get_post_time( ‘U’, true ) ) );

} else { ?>

This removes the null author field but still displays the user name.
3) Add this line of code below the other printf function you modified in part 1.

<?php } ?>

4) And volia again!

==============

HERE is what it looks like altogether. For easy cut and pasting into your theme templates — content-single-answer.php, content-single-question.php, content-comment.php, content-question.php

<?php
$user_id = get_post_field( ‘post_author’, get_the_ID() );

if ( $user_id == 0 ) {
printf( __( ‘<span>%1$s%2$s %3$s answered %4$s ago</span>’, ‘dwqa’ ), get_avatar( $user_id, 48 ), get_the_author(), dwqa_print_user_badge( $user_id ), human_time_diff( get_post_time( ‘U’, true ) ) );
} else {
printf( __( ‘<span><a href=”/user-profile/%1$s”>%2$s%3$s</a> %4$s answered %5$s ago</span>’, ‘dwqa’ ), get_the_author_meta(‘display_name’, $user_id), get_avatar( $user_id, 48 ), get_the_author(), dwqa_print_user_badge( $user_id ), human_time_diff( get_post_time( ‘U’, true ) ) );
} ?>

1 Answers
DominicStaff
answered 5 years ago

This is Amazing! 
I very happy about your solution. It will help many people to resolve this issue. 
I will update this solution in the Document of the DW Q&A.
Thank you.

Powered by DW Question & Answer Pro