如何在WordPress中以程式設計方式插入文章(PHP程式碼片段)

如何在WordPress中以程式設計方式插入文章(PHP程式碼片段)

您是否正在尋找一種高效、自動的方式為 WordPress 網站新增文章?雖然 WordPress 儀表板可以很好地管理您的內容,但有時您想通過程式設計將內容新增到您的網站。在 WordPress 網站中以程式設計方式插入文章可以幫您省時省力。

在本篇文章中,我們將向您展示如何在 WordPress 中以程式設計方式插入文章。

什麼是程式設計方式插入文章?

程式設計方式插入文章是一個使用程式碼向 WordPress 網站新增帖子的過程。這與通過 WordPress 儀表盤手動新增文章不同,後者可能需要更多時間、精力和手動輸入。

通過程式化插入文章,您可以建立高效的自動化流程,從而節省時間並簡化文章建立過程。

為什麼要在 WordPress 中以程式設計方式插入文章?

建立自動新增文章的程式可以節省時間,尤其是在 WordPress 中。有了正確的程式碼,在 WordPress 中插入帖子就可以快速、輕鬆地完成。

您需要圍繞我們在本文中使用的程式碼片段進行構建。例如,新增一個迴圈可以幫助你在網站中插入無限量的文章。

這對以下幾類網站很有幫助:

  • 需要擴充套件的大型網站
  • 自動內容管理
  • 建立插入使用者內容的函式

現在,讓我們開始插入文章!

在 WordPress 中以程式設計方式插入文章(PHP)

我們將使用 wp_insert_post() 函式為 WordPress 網站新增文章。該函式允許您傳遞一個包含所有文章資訊的陣列,以便立即將其新增到您的網站。

該函式有很多選項,包括帖子狀態、作者、標題,當然還有內容。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Insert post programmatically
$new_post = array(
'post_title' => 'My new post',
'post_content' => 'Content to insert.',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $new_post );
if( $post_id ){
echo "Post inserted successfully with the post ID of ".$post_id;
} else {
echo "Error, post not inserted";
}
<?php // Insert post programmatically $new_post = array( 'post_title' => 'My new post', 'post_content' => 'Content to insert.', 'post_status' => 'publish' ); $post_id = wp_insert_post( $new_post ); if( $post_id ){ echo "Post inserted successfully with the post ID of ".$post_id; } else { echo "Error, post not inserted"; }
<?php
// Insert post programmatically
$new_post = array(
 'post_title' => 'My new post',
 'post_content' => 'Content to insert.',
 'post_status' => 'publish'
);

$post_id = wp_insert_post( $new_post );

if( $post_id ){
 echo "Post inserted successfully with the post ID of ".$post_id;
} else {
 echo "Error, post not inserted";
}

一旦程式碼執行,它就會立即新增文章,所以你要確保它是在一個不會一直執行的函式內。這樣,你就可以避免文章重複釋出,也不用擔心文章被多次釋出。

在 WordPress 中以程式設計方式插入自定義文章型別內容(PHP)

通過 wp_insert_post() 函式,您還可以向自定義文章型別新增文章。無論您是將文章型別用於作品集,還是用於利基用例,這個功能都會有奇效。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Insert custom post programmatically
$new_post = array(
'post_title' => 'My new example post',
'post_content' => 'My new content!',
'post_status' => 'public',
'post_type' => 'my_post_type'
);
$post_id = wp_insert_post( $new_post );
if( $post_id ){
// Update custom field on the new post
update_post_meta( $post_id, 'my_custom_field', 'Hello!' );
} else {
echo "Error, post not inserted";
}
<?php // Insert custom post programmatically $new_post = array( 'post_title' => 'My new example post', 'post_content' => 'My new content!', 'post_status' => 'public', 'post_type' => 'my_post_type' ); $post_id = wp_insert_post( $new_post ); if( $post_id ){ // Update custom field on the new post update_post_meta( $post_id, 'my_custom_field', 'Hello!' ); } else { echo "Error, post not inserted"; }
<?php
// Insert custom post programmatically
$new_post = array(
 'post_title' => 'My new example post',
 'post_content' => 'My new content!',
 'post_status' => 'public',
 'post_type' => 'my_post_type'
);

$post_id = wp_insert_post( $new_post );

if( $post_id ){
 // Update custom field on the new post
 update_post_meta( $post_id, 'my_custom_field', 'Hello!' );
} else {
 echo "Error, post not inserted";
}

wp_insert_post() 返回新的文章 ID,非常適合更新自定義欄位。在本例中,我還包括在提交文章後更新自定義欄位。

在 WordPress 中以程式設計方式更新文章(PHP)

如果您想更新現有文章,只需在函式中新增 ID 欄位即可。使用任何現有文章的 ID 都可以用您設定的新資訊更新文章。您可以不動任何欄位,以避免修改。例如,如果您只想更新 post_title,就需要刪除我示例函式中的 post_content 一行。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Update a post programmatically
$my_post_id = 15
$update_post = array(
'ID' => $my_post_id,
'post_title' => 'My new post title',
'post_content' => 'Overwrite post content',
'post_status' => 'public'
);
wp_insert_post( $update_post );
<?php // Update a post programmatically $my_post_id = 15 $update_post = array( 'ID' => $my_post_id, 'post_title' => 'My new post title', 'post_content' => 'Overwrite post content', 'post_status' => 'public' ); wp_insert_post( $update_post );
<?php
// Update a post programmatically
$my_post_id = 15
$update_post = array(
 'ID' => $my_post_id,
 'post_title' => 'My new post title',
 'post_content' => 'Overwrite post content',
 'post_status' => 'public'
);

wp_insert_post( $update_post );

小結

在 WordPress 中以程式設計方式插入文章是一種在網站上新增文章時省時省力的好方法。通過使用程式碼,您可以建立高效、自動化的流程,使文章建立過程變得更加簡單。

希望這篇文章能幫助您瞭解如何在 WordPress 中以程式設計方式插入文章。

如果您還有任何關於 WordPress 開發的問題,請在下面的評論中告訴我們。

評論留言