簡體   English   中英

過濾器和掛鈎之間有什么區別以及如何在子主題中使用它們

[英]what is the difference between filters and hooks & how can i use them in child theme

wordpress中的filter和hooks有什么區別。

如何在子主題中使用以下過濾器?

<?php
foreach ( $results as $result ) {
// external plugins can modify or disable field
$result = apply_filters( 'cp_package_field', $result, 'ad' );
if ( ! $result )
continue;
?>

如何在子主題中使用以下掛鈎?

/**
* called in cp_add_new_listing() to hook into inserting new ad process
*
* @since 3.2.1
* @param int $post_id
*
*/
function cp_action_add_new_listing( $post_id ) {
do_action( 'cp_action_add_new_listing', $post_id );
}

對於您的問題的第一部分,這里有一個指向我很久以前發現的解釋的鏈接,該解釋非常全面,它有助於我理解差異。

https://wordpress.stackexchange.com/questions/1007/difference-between-filter-and-action-hooks

要在子主題中使用掛鈎,您可能需要在子主題的functions.php文件中包含以下代碼:

/**
 * Our callback function to the hook
 * @param  int $post_id id of the post
 * @return void
 */
function my_child_theme_new_listing_cb( $post_id ) {
    if($post_id == 10) { //Or whatever you want
        echo 'Hello World';
    }
    //We do not have the responsibility to return something as it is a hook
}

add_action( 'cp_action_add_new_listing', 'my_child_theme_new_listing_cb', 10, 1 );

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM