Code Tạo danh sách tác giả trong themes NewsPaper của Wodpress

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi admin, 1/9/20.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,878
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code mình đang code riêng cho themes Newspaper nên áp dụng đúng themes mới ra được kết quả nhé. Còn không sẽ bị báo lỗi, các dòng code PHP xài được cho mọi website, nhưng code HTML, CSS chỉ sử dụng cho danh sách tác giả author list của themes newspaper thôi.

    Demo:


    demo-danh-sach-tac-gia.jpg

    Bước 1:
    • Vào thư mục gốc của themes ngang hàng với function.php tạo 1 tập tin tên: authorlist.php
    • Copy toàn bộ code sau bỏ vào:
    PHP:
    <?php
    /*
    Template Name: User Page
    */
    get_header();
    $part_cur_auth_obj = (get_query_var('author_name')) ? get_user_by('slug'get_query_var('author_name')) : get_userdata(get_query_var('author'));
    $number 10;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged 1) * $number;
    $users get_users();
    $total_users count($users);
    $query get_users('&offset='.$offset.'&number='.$number);
    $total_pages intval($total_users $number) + 1;
    ?>
    <style>
    #users
    {
        list-style: none;
        margin: 0px;
        padding: 0px;
    }
    #users li
    {
        margin: 0px;
        border-bottom: 1px dotted #ccc;
        margin-bottom: 25px;
    }
    #users .user-avatar
    {
        float: left;
        width: 100px;
    }
    #users .user-avatar img
    {
        max-width: 80px;
        border-radius: 50%;
    }
    #users .user-data
    {
        float: left;
        width: calc(100% - 105px);
    }
    #users h4
    {
        margin-top: 0px;
        margin-bottom: 4px;
    }
    </style>
    <div class="td-main-content-wrap td-container-wrap">
    <div class="td-container">
        <div class="td-crumb-container">
           <div class="entry-crumbs">
           <span><a title="" class="entry-crumb" href="/" data-wpel-link="internal">Trang ch&#x1EE7;</a></span>
           <i class="td-icon-right td-bread-sep td-bred-no-url-last"></i> <span class="td-bred-no-url-last">T&#x00E1;c gi&#x1EA3;</span>
           </div>
        </div>
        <div class="td-pb-row">
            <div class="td-pb-span8 td-main-content">
                <div class="td-ss-main-content">
                    <div class="td-page-header">
                        <h1 class="entry-title td-page-title">
                            <?php echo get_the_title(); ?>
                        </h1>
                    </div>
                    <ul id="users">
                        <?php foreach($query as $q) { ?>                  
                            <li class="user clearfix">
                                <div class="user-avatar">
                                    <?php echo get_avatar$q->ID80 ); ?>
                                </div>
                                <div class="user-data">
                                    <h4 class="user-name">
                                        <a href="<?php echo get_author_posts_url($q->ID);?>">
                                            <?php echo get_the_author_meta('display_name'$q->ID);?>
                                        </a>
                                    </h4>
                                    <p><?php $post_des_seo get_user_meta($q->ID'wpseo_metadesc'true); echo $post_des_seo?></p>
                                </div>
                            </li>
                        <?php }  ?>
                    </ul>
                </div>
            </div>
            <div class="td-pb-span4 td-main-sidebar">
                <div class="td-ss-main-sidebar">
                    <?php dynamic_sidebar'td-default' ?>
                </div>
            </div>
        </div>
    </div>
    </div>
    <?php
    /*
    if ($total_users > $total_query) {
        echo '<div id="pagination" class="clearfix">';
        echo '<span class="pages">Pages:</span>';
            $current_page = max(1, get_query_var('paged'));
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => 'page/%#%/',
                'current' => $current_page,
                'total' => $total_pages
                'prev_next'    => false,
                'type'         => 'list',
            ));
        echo '</div>';
    }
    */
    get_footer();
    Tiếp tục vào quản trị admin của wordpress, chọn page -> tạo page mới.
    • Url bạn tùy chọn hoặc để là: author-list
    • Phần giao diện chọn: User Page
    • Tùy chọn Sidebar position theo ý bạn nhé.
    • Rồi lưu lại, xem sẽ có danh sách tác giả ngay
    Lưu ý: mình đang xài plugin SEO Yoast để tối ưu author, nếu bạn nào không xài plugin này thì đổi 1 đoạn code ở trên này thành code sau nhé (Nếu đang xài plugin seo yoast rồi thì khỏi đổi):

    Đổi đoạn:
    PHP:
    <p><?php
            $post_des_seo 
    get_user_meta($q->ID'wpseo_metadesc'true);
            echo 
    $post_des_seo?></p>
    Thành đoạn:
    PHP:
    <p><?php echo get_the_author_meta('description'$q->ID); ?></p>
    Để chỉ lấy những tác giả có bài viết thì bạn sử dụng code sau:
    PHP:
    <ul id="users">
        <?php foreach($query as $q) {
        if(
    count_user_posts($q->ID) > 0){
        
    ?>                          
            <li class="user clearfix">
                <div class="user-avatar">
                    <?php echo get_avatar$q->ID80 ); ?>    
                </div>
                <div class="user-data">
                    <h4 class="user-name">
                        <a href="<?php echo get_author_posts_url($q->ID);?>">
                            <?php echo get_the_author_meta('display_name'$q->ID);?>
                        </a>
                    </h4>
                    <p><?php echo get_the_author_meta('description'$q->ID); ?></p>
                </div>
            </li>
        <?php } }  ?>
    </ul>
    tao-author-list.png

    Để tạo liên kết danh sách tác giả ở Breadcrumbs của chi tiết author tác giả thì bạn làm như sau:
    • Truy cập vào đường dẫn trên plugin mặc định của themes NewsPaper
    • /wp-content/plugins/td-composer/legacy/common/wp_booster/td_page_generator.php
    • Mở tập tin này lên tìm tới dòng code của hàm: author_breadcrumbs_array
    PHP:
    if ($skip_extra === false) {
        
    //extra crumb
        
    $breadcrumbs_array [] = array (
            
    'title_attribute' => '',
            
    'url' => '',
            
    'display_name' => __td('Authors'TD_THEME_NAME)
        );
    }
    Đổi thành /author-list/ hoặc liên kết danh sách của bạn tùy biến
    PHP:
    if ($skip_extra === false) {
        
    //extra crumb
        
    $breadcrumbs_array [] = array (
            
    'title_attribute' => '',
            
    'url' => '/author-list/',
            
    'display_name' => __td('Authors'TD_THEME_NAME)
        );
    }
    tao-link-cho-breadcrumbs-cua-chi-tiet-tac-gia.jpg
     
    Cảm ơn đã xem bài:

    Code Tạo danh sách tác giả trong themes NewsPaper của Wodpress

    Chỉnh sửa cuối: 26/8/21
  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,878
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Thêm html, hình ảnh và trình sản thảo Editer vào 1 field mới trong profile

    Bước 1: Vào function.php của themes chèn code sau để tạo Field mới profile author.
    PHP:
    //tao fied moi cho author
    add_action( 'show_user_profile', 'extra_user_profile_fields' );
    add_action( 'edit_user_profile', 'extra_user_profile_fields' );
    function extra_user_profile_fields( $user ) { ?>
        <h3>Chi tiết về tác giả</h3>
        <table class="form-table">
        <tr>
            <th><label for="contentauthor">Chi tiết về tác giả</label></th>
            <td>
            <?php wp_editor(get_the_author_meta'contentauthor'$user->ID ), "contentauthor", array(
                    
    'wpautop'       => true,
                    
    'media_buttons' => false,
                    
    'textarea_name' => 'contentauthor',
                    
    'editor_class'  => 'cs-content',
                    
    'textarea_rows' => 10
                
    ) ); ?>
        
                <br />
                <span style="display: block;margin-top: 5px;margin-left: 5px;" class="description">Nội dung giới thiệu chi tiết về tác giả (bắt buộc nhập)</span>
            </td>
        </tr>
        </table>
    <?php }
    add_action'personal_options_update''save_extra_user_profile_fields' );
    add_action'edit_user_profile_update''save_extra_user_profile_fields' );
    function 
    save_extra_user_profile_fields$user_id ) {
        if ( !
    current_user_can'edit_user'$user_id ) ) {
            return 
    false;
        }
        
    update_user_meta$user_id'contentauthor'$_POST['contentauthor'] );
    }
    Bước 2: Hiển thị lên chi tiết tác giả như hình dưới

    Phiên bản NewsPaper 9.8 trở xuống, vào thư mục: /public_html/wp-content/plugins/td-composer/legacy/Newspaper/author.php

    Chèn code sau vào vị trí : cần hiển thị content này.
    Mã:
    <style>
    .author-box-wrap {
        margin-bottom: 20px;
    }
    .author h2
    {
        color: red;
        font-size: 25px;
        font-weight: bold;
        margin: 10px 0px 15px 0px;
    }
    .author h3
    {
        font-size: 20px;
        font-weight: bold;
        margin: 10px 0px 15px 0px;
    }
    </style>
    <?php
    $content = get_the_author_meta( 'contentauthor', $part_cur_auth_obj->ID );
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
    //echo wpautop( get_the_author_meta( 'contentauthor', $user->ID ) );
     ?>
    <h4 class="td-related-title td-block-title" style="font-size: 18px;font-weight: bold;background-color: #046738;padding: 14px 10px 10px 15px;border-radius: 5px;color: white;margin-top: 30px;">BÀI VIẾT CỦA TÁC GIẢ</h4>
    DEMO
    trinh-soan-thao.jpg chen-noi-dung-content-vao-author-tac-gia-chi-tiet.jpg
    Bước 2: Phiên bản NewsPaper 9.8 trở lên (Nếu bước 2 trên không được, và bạn đang sử dụng thêm plugin td-standard-pack)
    • Sử dụng plugin td-standard-pack thì bạn muốn sửa author truy cập vào: /public_html/wp-content/plugins/td-standard-pack/Newspaper/parts/page-author-box.php
    Chèn code sau vào cuối file:
    PHP:
    <style>
    .author-box-wrap {
        margin-bottom: 20px;
    }
    .author h2
    {
        color: red;
        font-size: 25px;
        font-weight: bold;
        margin: 10px 0px 15px 0px;
    }
    .author h3
    {
        font-size: 20px;
        font-weight: bold;
        margin: 10px 0px 15px 0px;
    }
    </style>
    <?php
    $content 
    get_the_author_meta'contentauthor'$part_cur_auth_obj->ID );
    $content apply_filters('the_content'$content);
    $content str_replace(']]>'']]&gt;'$content);
    echo 
    $content;
    //echo wpautop( get_the_author_meta( 'contentauthor', $user->ID ) );
     
    ?>
    <h4 class="td-related-title td-block-title" style="font-size: 18px;font-weight: bold;background-color: #046738;padding: 14px 10px 10px 15px;border-radius: 5px;color: white;margin-top: 0;float: left;width: 100%;">BÀI VIẾT CỦA TÁC GIẢ</h4>
     
    Chỉnh sửa cuối: 24/11/23
    XMENROMA thích bài này.
  3. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,878
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Đóng khung hiển thị hình ảnh tác giả

    Demo themes phiên bản Newspaper 9.8:
    dong-khung-o-tac-gia.jpg

    Truy cập vào:
    • /public_html/wp-content/plugins/td-composer/legacy/common/wp_booster/td_module_single_base.php
    Chỉnh ngày sửa, ngày cập nhật
    PHP:
    if (td_util::get_option('tds_p_show_date') != 'hide') {
       
    $td_article_date_unix get_the_time('U'$this->post->ID);
       
    $td_article_date_edit get_the_modified_time('U'$this->post->ID);
      
    $buffy .= '<span class="td-post-date cs-sg" style="margin-top: 5px;margin-right: 20px;">Ng&#x00E0;y vi&#x1EBF;t';
      
    $buffy .= '<time style="display: block;margin-top: 10px;" class="entry-date updated td-module-date' $visibility_class '" datetime="' date(DATE_W3C$td_article_date_unix) . '" >' get_the_time(get_option('date_format'), $this->post->ID) . '</time>';
       
    $buffy .= '</span>';
       
    $buffy .= '<span class="td-post-date cs-sg" style="margin-top: 5px;">Ng&#x00E0;y s&#x1EED;a';
      
    $buffy .= '<time style="display: block;margin-top: 10px;" class="entry-date updated td-module-date' $visibility_class '" datetime="' date(DATE_W3C$td_article_date_edit) . '" >' get_the_modified_time(get_option('date_format'), $this->post->ID) . '</time>';
       
    $buffy .= '</span>';
    }
    Hiển thị hình ảnh trong post author bài viết
    PHP:
    if (td_util::get_option('tds_p_show_author_name') != 'hide') {

       
    $user_email get_the_author_meta'user_email' );
       
    // Convert email into md5 hash and set image size to 80 px
       
    $user_gravatar_url 'https://www.gravatar.com/avatar/' md5($user_email) . '?s=80';

        
    $buffy .= '<div class="td-post-author-name cs-authorimg'$post_author_no_dot '"><img style="max-width: 40px;float: left;" alt="'get_the_author_meta('display_name'$this->post->post_author) .'" src="'.$user_gravatar_url.'"/><div class="td-author-by" style="display: block;margin-right: 2px;width: calc(100% - 55px);float: left;margin-left: 10px;margin-top: 5px;">T&#x00E1;c gi&#x1EA3;</div> ';
        
    $buffy .= '<a style="display: inline-block;margin-left: 11px;margin-top: 10px;" href="' get_author_posts_url($this->post->post_author) . '">' get_the_author_meta('display_name'$this->post->post_author) . '</a>' ;
        
    $buffy .= '</div>';
    }
    Chỉnh lượt xem
    PHP:
    function get_views() {
        
    $buffy '';
        if (
    td_util::get_option('tds_p_show_views') != 'hide') {
            
    $buffy .= '<div class="td-post-views" style="text-align: center;">';
           
    $buffy .= '<span style="display: block;margin-bottom: 5px;margin-top: 5px;">S&#x1ED1; l&#x1EA7;n xem</span>';
            
    // WP-Post Views Counter
            
    if (function_exists('the_views')) {
                
    $post_views the_views(false);
                
    $buffy .= $post_views;
            }
            
    // Default Theme Views Counter
            
    else {
               
    $buffy .= '<span class="td-nr-views-' $this->post->ID '">' td_page_views::get_page_views($this->post->ID) .'</span>';
            }

            
    $buffy .= '</div>';
        }
        return 
    $buffy;
    }
    CSS style
    HTML:
    .td-post-date {
        color: #000;
    }
    .single-post .td-post-featured-image,
    .single-post .td-post-sharing-visible
    {
        float: left;
        width: 100%;
    }
    .td-module-meta-info .td-post-views {
        float: right;
        margin-right: 22px;
    }
    .single-post .td-post-title .td-module-meta-info {
        float: left;
        width: 100%;
        background-color: #dcefc9;
        padding: 6px 10px 8px 7px;
        border-radius: 3px;
    }
    
    Download code: Ở đây.
     
    Chỉnh sửa cuối: 8/9/20
    XMENROMA thích bài này.
  4. dung0886876888
    Tham gia ngày:
    29/6/20
    Bài viết:
    58
    Đã được thích:
    2
    Điểm thành tích:
    18
    Giới tính:
    Nam
    cảm ơn bạn đã chia sẻ, chúc bạn một ngày vui vẻ
     
  5. docopdienxeoto
    Tham gia ngày:
    7/11/20
    Bài viết:
    1
    Đã được thích:
    0
    Điểm thành tích:
    3
    Giới tính:
    Nam
    rất hữu ích, cảm ơn bác đã chia sẽ
     
  6. XMENROMA
    Tham gia ngày:
    11/3/21
    Bài viết:
    1
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nam
    Cám ơn bạn đã chia sẽ. Bài viết rất chi tiết và hữu ích.