Hướng dẫn này cung cấp các bước để đăng ký vùng tiện ích con “Nội dung quảng cáo” tùy chỉnh và chèn (các) tiện ích con được đặt trong vùng tiện ích này sau một đoạn được chỉ định trong nội dung trên các bài đăng đơn lẻ trong Genesis.
Bước 1
Thêm phần sau vào functions.php của chủ đề con :
// Registers `ad-content` widget area.
genesis_register_widget_area(
array(
'id' => 'ad-content',
'name' => __( 'Ad Content', 'text-domain' ),
'description' => __( 'This is the ad content section.', 'text-domain' ),
)
);
add_filter( 'the_content', 'custom_insert_widget_area' );
/**
* Filters the content to insert widget area after specified paragraph of single post content.
*
* @param string $content Existing post content.
* @return string Modified post content.
*/
function custom_insert_widget_area( $content ) {
ob_start();
genesis_widget_area( 'ad-content', array(
'before' => '<div class="ad-content widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
$widget_area_html = ob_get_clean();
if ( is_singular( 'post' ) ) {
$content = custom_insert_after_paragraph( $widget_area_html, 3, $content ); // change 3 to the paragraph after which you would like to insert the widget area.
}
return $content;
}
// Callback function that inserts the passed in HTML after the specified paragraph and returns the modified content.
function custom_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ( $paragraphs as $index => $paragraph ) {
if ( trim( $paragraph ) ) {
$paragraphs[ $index ] .= $closing_p;
}
if ( $paragraph_id === $index + 1 ) {
$paragraphs[ $index ] .= $insertion;
}
}
return implode( '', $paragraphs );
}
Trong thay đổi 3
ở trên đối với số đoạn văn mà sau đó khu vực tiện ích sẽ được chèn vào.$content = custom_insert_after_paragraph( $widget_area_html, 3, $content );
Bước 2
Chuyển đến Giao diện> Tiện ích và kéo (các) tiện ích mong muốn của bạn vào vùng tiện ích Nội dung Quảng cáo.