Hook cho bài viết trong WordPress

Hook cho bài viết trong WordPress

Hôm nay mình sẽ chia sẻ cho các bạn một số đoạn Hook cho Bài viết (Post) trong WordPress không sử dụng Plugin cực kỳ đơn giản.

Khuyến cáo: Nên sử dụng Theme Child để viết code

Xem thêmHiển thị bài viết liên quan trong chi tiết bài viết WordPress

Các hook nằm trong Element blog post của Flatsome

Trong element Blog Post của flatsome thì hiện tại có 2 hook là:

flatsome_blog_post_before
flatsome_blog_post_after
flatsome_blog_post_before
flatsome_blog_post_after
//* Customize the entry meta in the entry footer
    add_filter( 'genesis_post_meta', 'post_meta_filter' );
    function post_meta_filter( $post_meta ) {
    $post_meta = 'Posted on [post_date] by [post_author_posts_link] under [post_categories before=""] with tag [post_tags before=""][post_edit]';
    return $post_meta;
    }

 

//* Customize the entry meta in the entry footer
    add_filter( 'genesis_post_meta', 'post_meta_filter' );
    function post_meta_filter( $post_meta ) {
    $post_meta = 'Posted on [post_date] by [post_author_posts_link] under [post_categories before=""] with tag [post_tags before=""][post_edit]';
    return $post_meta;
    }

 

//* Customize the entry meta in the entry footer
    add_filter( 'genesis_post_meta', 'post_meta_filter' );
    function post_meta_filter( $post_meta ) {
    $post_meta = 'Posted on [post_date] by [post_author_posts_link] under [post_categories before=""] with tag [post_tags before=""][post_edit]';
    return $post_meta;
    }

 

 

 

//* Customize the entry meta in the entry footer
    add_filter( 'genesis_post_meta', 'post_meta_filter' );
    function post_meta_filter( $post_meta ) {
    $post_meta = 'Posted on [post_date] by [post_author_posts_link] under [post_categories before=""] with tag [post_tags before=""][post_edit]';
    return $post_meta;
    }

 

//* Customize the entry meta in the entry footer
    add_filter( 'genesis_post_meta', 'post_meta_filter' );
    function post_meta_filter( $post_meta ) {
    $post_meta = 'Posted on [post_date] by [post_author_posts_link] under [post_categories before=""] with tag [post_tags before=""][post_edit]';
    return $post_meta;
    }
.add_to_cart_mobile {
    position: absolute;
    bottom: 0;
    z-index: 999999999999;
    /* display: flex; */
    width: 100%;
    display: none;
}
.add_to_cart_mobile button#add_to_cart_mobile {
    width: 50%;
    margin: 0 !important;
    border: none;
    line-height: 36px;
    background: #ddd;
    font-size: 14px;
    color: #ee4e2e;
    text-transform: capitalize;
    font-weight: 400;
    position: relative;
    display: inline-block;
}
.add_to_cart_mobile a.button.alt.buy-now {
    width: 50%;
    font-size: 14px;
    margin-right: 0;
    float: right;
}

 

 

 

 

 

Bây giờ mình sẽ bắt đầu code luôn cho các bạn không phải đọc dài dòng nữa nhé.

Thêm Hook vào trước tiêu đề bài viết

function showhook(){
echo ‘itcodewp.com – Nội dung hiển thị trước title bài viết’;
}
add_action(‘flatsome_blog_post_before’,‘showhook’);

Thêm Hook vào sau tiêu đề bài viết

 

function showhook(){
echo ‘itcodewp.com – Nội dung hiển thị sau title bài viết’;
}
add_action(‘flatsome_blog_post_after’,‘showhook’);

Các hook nằm trong single của Flatsome

Tiếp theo mình sẽ hướng dẫn các bạn 1 số đoạn Hook nằm trong chi tiết bài viết. Thực ra nó cũng giống như 2 đoạn code trên thôi, các bạn cùng theo dõi nhé.

Thêm nội dung vào đầu bài viết

function hook_before_content( $content ) {
if( is_single() && ! empty( $GLOBALS[‘post’] ) ) {
if ( $GLOBALS[‘post’]->ID == get_the_ID() ) {
$itcodewp_before_content = ‘itcodewp.com – Nội dung thêm vào trước chi tiết nội dung bài viết’;
$itcodewp_before_content .= $content;
}
return $itcodewp_before_content ;
}
}
add_filter( ‘the_content’, ‘hook_before_content’ );

Thêm nội dung vào giữa bài viết

add_filter(‘the_content’, ‘contentafter2para’);
function contentafter2para($content){
if(is_single()){
$itcodewp_content= ‘itcodewp.com – content sau 3 câu (tương tư như 3 thẻ p/div/h1….n)’;
$after = 3;
$end = ‘</p>’;
$content_itcodewp = explode($end, $content);
foreach($content_itcodewp as $key => $cont){
if(trim($cont)) {
$content_itcodewp[$key] .= $end;
}
if(($key + 1) == $after){
$content_itcodewp[$key] .= $itcodewp_content
}
}
$content = implode(, $content_itcodewp);
}
return $content;
}

Thêm nội dung vào sau bài viết

function aftercontent( $content ) {
if( is_single() && ! empty( $GLOBALS[‘post’] ) ) {
if ( $GLOBALS[‘post’]->ID == get_the_ID() ) {
$content .= ‘Nội dung bạn muốn thêm vào sau content’;
}
}
return $content;
}
add_filter(‘the_content’, ‘aftercontent’);

Vậy là đã xong rồi. Các bạn vào lại trang thanh toán rồi F5 lại để xem thay đổi nhé (Bạn hãy xóa Cache trước nếu bạn dùng cache nhé, hoặc bạn Ctrl + F5 cũng được)

Xem thêmTùy biến trang thanh toán (checkout) Woocommerce

Xem thêmLàm đẹp form trong trang thanh toán (checkout) Woocommerce

Trong quá trình thay đổi text button trang thanh toán mà bạn gặp lỗi thì hãy comment cho ITCODEWP.COM biết, mình sẽ hướng dẫn hỗ trợ bạn.

Chúc các bạn thành công!

Nguồn: https://itcodewp.com/

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *