在 WordPress 內可以透過 is_user_logged_in() 判斷使用者是否為登入狀態,會回傳布林值。

以下是簡單的程式碼:

if (is_user_logged_in()) {
    // 如果使用者已登入,顯示這些內容
} else {
    // 如果使用者未登入,顯示這些內容
}

如果想在頁面或文章中使用類似的功能,您也可以使用shortcode。例如,您可以在 functions.php 檔案中添加以下程式碼:

function custom_content_shortcode($atts, $content = null) {
    if (is_user_logged_in()) {
        // 如果使用者已登入,return 這些內容
        return '歡迎,已登入使用者!';
    } else {
        // 如果使用者未登入,return 這些內容
        return '請登入以查看更多內容。';
    }
}

add_shortcode('custom_content', 'custom_content_shortcode');

接著就可以在頁面或文章中使用 shortcode [custom_content] 來顯示相應的內容。

請注意,在修改主題檔案或使用程式碼時,請確保您了解 WordPress 主題和外掛程式的開發,並在修改之前備份您的網站,以防發生意外情況。