Chúng ta sẽ tiếp tục phần còn lại của series: Hướng dẫn cách lập trình child theme (custom code) cho Genesis Framework đơn giản.
Ở phần này chúng ta sẽ thực hiện Custom Breadcrumb, Featured Post List, Related Posts cho child theme.
Hãy cùng bắt đầu nào!
Custom Breadcrumb cho Genesis Child
Dán nó ở function.php
//* Modify breadcrumb arguments.
add_filter( 'genesis_breadcrumb_args', 'genesis_child_breadcrumb_args' );
function genesis_child_breadcrumb_args( $args ) { $args['home'] = 'Home'; // Can be changed by adding text in quotation marks ' ' $args['sep'] = ' / '; // Can be changed by adding text in quotation marks ' ' $args['list_sep'] = ', '; // Can be changed by adding text in quotation marks ' ' $args['prefix'] = '<div class="breadcrumb"><div class="wrap">'; // Can be changed by adding text in quotation marks ' ' $args['suffix'] = '</div></div>'; $args['heirarchial_attachments'] = true; // Can be changed by adding text in quotation marks ' ' Genesis 1.5 and later $args['heirarchial_categories'] = true; // Can be changed by adding text in quotation marks ' ' Genesis 1.5 and later $args['display'] = true; $args['labels']['prefix'] = 'Monster :'; // Can be changed by adding text in quotation marks ' ' $args['labels']['author'] = 'Archives for '; // Can be changed by adding text in quotation marks ' ' $args['labels']['category'] = 'Archives for '; // Can be changed by adding text in quotation marks ' ' Genesis 1.6 and later $args['labels']['tag'] = 'Archives for '; // Can be changed by adding text in quotation marks ' ' $args['labels']['date'] = 'Archives for '; // Can be changed by adding text in quotation marks ' ' $args['labels']['search'] = 'Search for '; // Can be changed by adding text in quotation marks ' ' $args['labels']['tax'] = 'Archives for '; // Can be changed by adding text in quotation marks ' ' $args['labels']['post_type'] = 'Archives for ';// Can be changed by adding text in quotation marks ' ' $args['labels']['404'] = 'Not found: '; // Can be changed by adding text in quotation marks ' ' Genesis 1.5 and later
return $args;
}
// Home breadcrumb icon.
add_filter ( 'genesis_home_crumb', 'genesis_child_breadcrumb_home_icon' );
function genesis_child_breadcrumb_home_icon( $crumb ) { $crumb = '<a href="' . home_url() . '" title="' . get_bloginfo('name') . '"><i class="dashicons dashicons-admin-home"></i> Home</a>'; return $crumb;
}
//* Reposition the breadcrumbs
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
add_action( 'genesis_after_header', 'genesis_do_breadcrumbs' );PHPFeatured Post List cho Genesis Child
Để bắt đầu tạo các hàm của bạn, hãy đi tới thư mục chủ đề con của bạn và tạo thêm Floder “lib” theo chiều ngang bằng tệp Function.php. Trong Thư mục “lib” tạo một tệp có tên là featured-posts.php để quá trình chuẩn bị hoàn tất; tiếp theo hãy làm như dưới đây
Sao chép và dán mã bên dưới vào tệp function.php để chủ đề con gọi tệp featured-posts.php.
// Add Featured Posts List.
include_once( get_stylesheet_directory() . '/lib/featured-posts.php' );PHPSao chép và dán mã bên dưới vào tệp feature-posts.php để tạo Danh sách bài đăng nổi bật đặt nó trên trang chủ blog của bạn.
<?php
/** * Featured Posts List. * * This file adds Featured Posts List to the Code Genesis Child Theme. * * @package Code Genesis Child * @author Monster * @license GPL-2.0-or-later * @link https://quicksite.vn/ */// Admin Setting
add_action('admin_menu', 'posts_add_pages');
function posts_add_pages() { // Add new menu in Setting: add_submenu_page('edit.php','Featured Posts List', 'Featured Posts List', 8, 'postsoptions', 'posts_options_page');
}
// Main function to diplay on front end
add_action( 'genesis_before_content', 'featuredpostsList' );
function featuredpostsList() { global $post, $wpdb, $posts_settings; if( is_home()){ // posts_id from database $posts_id = $posts_settings['posts_id']; ?><div class="featured-posts-container"><?php if($posts_id): $i = 0; $posts_idarray = explode(',',$posts_id); foreach ($posts_idarray as $list){ $post = new WP_Query('p='.$list.''); $post->the_post(); $categories = get_the_category(); ?> <div class="featured-posts list-<?php echo $i; ?>" style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>')"> <div class="featured-posts-content"> <div class="featured-posts-cat"><a href="<?php echo get_category_link( $categories[0]->term_id ); ?>"><?php echo esc_html( $categories[0]->name );?></a></div> <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2> <div class="featured-posts-time"><?php the_time('M j, Y') ?></div> </div> </div><?php $i ++; ?> <?php } else : ?> <p><?php _e( 'Sorry, no Featured Post List to display. Please set in the Setting', 'genesis-child' );?></p> <?php endif; ?></div><?php }
}
$posts_settings = get_option('posts_settings'); $data = array( 'posts_id' => '' ); $ol_flash = '';
/* Functions */function posts_options_page() { global $ol_flash, $posts_settings, $_POST, $wp_rewrite; if (isset($_POST['posts_id'])) { $posts_settings['posts_id'] = $_POST['posts_id']; update_option('posts_settings',$posts_settings); $ol_flash = __('Your Featured List has been saved.', 'genesis-child'); } if ($ol_flash != '') echo '<div id="message"class="updated fade"><p>' . $ol_flash . '</p></div>'; ?> <div class="wrap"> <h2><?php _e('Add Posts ID to Create Featured Post List','genesis-child') ?></h2> <form action="" method="post"> <table class="optiontable form-table"> <tr><h3><?php _e('Display Featured Post List to your site.','genesis-child') ?></h3></tr> <tr> <th><?php _e('Post ID :','genesis-child') ?></th> <td> <input type="text" name="posts_id" placeholder="1, 2, 3" value="<?php echo htmlentities($posts_settings['posts_id']); ?>" size="50%" /></th><br /> <label for="disabled"><?php _e('To Add Multiple Post IDs use " , " for exmple : " 1, 2, 3"','genesis-child') ?></label> </td> </tr> </table> <p class="submit"> <input type="submit" class="button-primary" value="<?php _e('Save Featured Post List','genesis-child') ?>" /> </p> </form> </div> <?php
}/* 1, 736, 725, 691 */PHPTrong bài viết này, tôi cung cấp cho bạn một mẫu CSS; CSS này không phản hồi. Bạn cần có kỹ năng CSS để tạo phản hồi nhanh và tạo nhiều bố cục khác. Sao chép và dán mã bên dưới vào tệp style.css
/* Featured Post List */.featured-posts { float: left; box-shadow: inset 0 0 0 1500px #2222225e; -webkit-filter: initial; filter: initial; -webkit-transform: initial; -khtml-transform: initial; -moz-transform: initial; -ms-transform: initial; -o-transform: initial; transform: initial; -webkit-transition: all 0.2000s linear; transition: all 0.2000s linear; position: relative; background-position: 50% 50%; background-size: cover;
}
.featured-posts:hover { -webkit-filter: initial; filter: initial; -webkit-transform: initial; -khtml-transform: initial; -moz-transform: initial; -ms-transform: initial; -o-transform: initial; transform: initial; -webkit-transition: all 0.2000s linear; transition: all 0.2000s linear; box-shadow:inset 0 0 0 2000px #2222222e;
}
.featured-posts.list-0 { width: 50%; height: 462px; border-right: 5px solid #fff;
}
.featured-posts.list-1 { width: 50%; height: 231px; border-bottom: 5px solid #fff;
}
.featured-posts.list-2 { border-right: 5px solid #fff;
}
.featured-posts.list-2,
.featured-posts.list-3 { width: 25%; height: 231px;
}
.featured-posts.list-0
.featured-posts.list-2,
.featured-posts.list-3 { margin-bottom: 40px;
}
.featured-posts-content { position: absolute; top: 20px; left: 25px; right: 25px; max-height: 95px; z-index: 1; font-size: 14px;
}
.featured-posts-cat a { background-color: #fe2e2e; color: #fff; font-size: 10px; font-size: 1rem; font-weight: 300; padding: 5px 10px; text-transform: uppercase; border-radius: 3px;
}
.featured-posts h2 a,
.featured-posts-time { color: #fff; text-transform: capitalize; font-family: "Roboto",Arial,sans-serif; text-shadow: 1px 1px 3px rgba(0,0,0,.2);
}
.featured-posts h2 a:hover,
.featured-posts-time:hover { color: #fff; padding-left: 10px; border-left: 5px solid red
}
.featured-posts h2 a { font-size: 16px;
}
.list-0 h2 a, .list-1 h2 a { font-size: 24px; line-height: 30px;
}CSSHiển thị bài viết liên quan cho Genesis Child
Hiển thị bài viết liên quan theo danh mục
Hiển thị các bài đăng liên quan về chủ đề Genesis Child bắt đầu bằng một action hook, ‘genesis_entry_footer’. Hook này nằm trên tất cả các bài viết, trang và chuyên mục nên để hiển thị chính xác trên trang bài viết, chúng ta thêm điều kiện is_single. Sao chép và dán vào tệp tin.php chủ đề con của bạn.
// Select posts in the same categories as the current post.
if ( ! function_exists( 'related_post_category' ) ):
add_action( 'genesis_entry_footer', 'related_post_category', 40 );
function related_post_category() { if ( is_single ( ) ) { // Get a list of the current post's categories global $post; $categories = get_the_category($post->ID); if ($categories) { $category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'post__not_in' => array($post->ID), // Ensure that the current post is not displayed 'posts_per_page' => 3, // Number of related posts to display 'orderby' => 'rand', // Randomize the results ); $my_query = new wp_query($args); if( $my_query->have_posts() ): $i = 0; ?> <div class="related-posts-container"><div class="related-posts-title-block"><h3 class="related-posts-title"><?php _e( 'Related Articles', 'longviet' );?></h3></div> <div class="related-posts-inner"><ul class="related-posts-blook"> <?php while ($my_query->have_posts()):$my_query->the_post(); ?> <li class="related-posts-<?php echo $i; ?>"> <div class="related-posts-img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a> <span class="entry-time"><?php echo esc_html( get_the_date() ) ?></span></div> <div class="item-details"> <h4 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> </div> </li> <?php $i ++; ?> <?php endwhile; ?> <?php else : ?> <div class="related-posts-container"><div class="related-posts-title-block"><h3 class="related-posts-title"><?php _e( 'Related Articles', 'longviet' );?></h3></div> <div class="related-posts-inner"><ul class="related-posts-blook"> <p><?php _e( 'Sorry, no related articles to display', 'longviet' );?></p> </ul></div></div> <?php endif; wp_reset_postdata(); // Reset postdata } }
}
endif;PHPHiển thị bài viết liên quan theo thẻ
Giống như trên, mã bên dưới phải được sao chép và dán vào tệp function.php chủ đề con của bạn.
// Select posts in the same tags as the current post.
if ( ! function_exists( 'related_post_tags' ) ):
add_action( 'genesis_entry_footer', 'related_post_tags', 40 );
function related_post_tags() { if ( is_single ( ) ) { global $post; // Get a list of the current post's tags. $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), // Ensure that the current post is not displayed 'posts_per_page' => 3, // Number of related posts to display 'orderby' => 'rand', // Randomize the results ); $my_query = new wp_query($args); if( $my_query->have_posts() ): $i = 0; ?> <div class="related-posts-container"><div class="related-posts-title-block"><h3 class="related-posts-title"><?php _e( 'Related Articles', 'longviet' );?></h3></div> <div class="related-posts-inner"><ul class="related-posts-blook"> <?php while ($my_query->have_posts()):$my_query->the_post(); ?> <li class="related-posts-<?php echo $i; ?>"> <div class="related-posts-img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(235, 160)); ?></a> <span class="entry-time"><?php echo esc_html( get_the_date() ) ?></span></div> <div class="item-details"> <h4 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> </div> </li> <?php $i ++; ?> <?php endwhile; ?> <?php else : ?> <div class="related-posts-container"><div class="related-posts-title-block"><h3 class="related-posts-title"><?php _e( 'Related Articles', 'longviet' );?></h3></div> <div class="related-posts-inner"><ul class="related-posts-blook"> <p><?php _e( 'Sorry, no related articles to display', 'longviet' );?></p> </ul></div></div> <?php endif; wp_reset_postdata(); // Reset postdata } }
}
endif;PHPMã CSS
Mỗi người có một quan điểm thẩm mỹ khác nhau trong bài viết này; Mình sẽ code CSS để bạn có cái nhìn tổng quan. Để hài lòng, bạn có thể viết theo cách của bạn. Sao chép CSS và dán nó vào tệp style.css của bạn.
/* Related Articles */.related-posts-container .related-posts-title-block { border-bottom: 2px solid #222;
}
.related-posts-container .related-posts-title { display: inline-block; padding: 5px; margin: 0; color: #fff; background-color: #222; line-height: 1; font-size: 16px; font-weight: 600; font-family: open sans,arial,sans-serif;
}
.related-posts-inner { display: block; margin: 20px 0;
}
.related-posts-inner .related-posts-blook { display: flex;
}
.related-posts-inner li { width: 31.33333%; display: block; clear: both; list-style-type: none; margin-right: 3%;
}
.related-posts-inner .related-posts-2 { margin-right: 0%;
}
.related-posts-inner .related-posts-img { position: relative;
}
.related-posts-inner .entry-time { position: absolute; top: 0; padding: 3px; color: #fff; background-color: #222; line-height: 1; font-size: 10px; font-weight: 600; font-family: open sans,arial,sans-serif;
}
.related-posts-inner .entry-time::before { content: '\f469'; font-family: dashicons; font-size: 15px; color: #fff; padding-right: 3px; vertical-align: middle;
}
.related-posts-inner .item-details { margin-top: 20px;
}
.related-posts-inner .entry-title { font-size: 18px; line-height: 1; font-weight: 600; font-family: open sans,arial,sans-serif;
}CSSOk như vậy là xong phần 3, hẹn gặp các bạn ở phần tiếp theo!








