
內容簡介
此外掛可讓您在任何類型的文章中建立多對多的關聯:文章、頁面、自訂文章類型等等。
方便的使用者介面可輕鬆設定文章之間的關聯,新的連接 metabox 會顯示在相關文章的編輯頁面上,其中包含搜尋文字、文章類型和項目過濾的選項。
可使用標準的 WP_Query() 和 get_posts() 來取得相關的文章。
使用獨立的資料庫表格,當文章狀態更改時會更新其關聯,並在文章刪除時刪除關聯。
使用 WP_Query 取得相關文章
// 在主循環中,不需要當前文章 ID(p2p_rel_post_id),如果您沒有設置它,將使用當前文章:
$args = array(
'p2p_rel_key' => 'prod_to_bars', // 這是您的連接鍵名稱。必填欄位。
'p2p_rel_post_id' => 1, // 文章 ID。在主循環中不需要。
'p2p_rel_direction' => 'any', // 連接方向。默認為 'any'。選填欄位。以下為詳細說明(可以是 'any' | 'from_to' | 'to_from')
// 在這裡您當然可以添加您需要的 WP 標準參數:文章類型、狀態、日期、分頁等等。
);
//(此時,與任何其他 WP 循環一樣):
// 建立查詢
$the_query = new WP_Query( $args );
// 循環
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// 找不到文章
echo '<p>Nothing related</p>';
}
// 回復原來的文章資料
wp_reset_postdata();
使用 get_posts() 取得相關文章
// 在主循環中,不需要當前文章 ID(p2p_rel_post_id),如果您沒有設置它,將使用當前文章:
$args = array(
'p2p_rel_key' => 'prod_to_bars', // 這是您的連接鍵名稱。必填欄位。
'p2p_rel_post_id' => 1, // 文章 ID。在主循環中不需要。
'p2p_rel_direction' => 'any', // 連接方向。默認為 'any'。選填欄位。以下為詳細說明(可以是 'any' | 'from_to' | 'to_from')
'post_type' => 'any', // 過濾的文章類型,可以是陣列。選填欄位。預設為 'post'。(可以是 'any' 讀取所有文章)
'suppress_filters' => false // 必填欄位
// 在這裡您當然可以添加您需要的 WP 標準參數:文章類型、狀態、日期、分頁等等。
);
//(此時,與任何其他 WP 循環一樣):
$rel_posts = get_posts( $args );
print_r( $rel_posts );
直接取得相關文章資料庫資料
// 在主循環中,不需要使用當前文章 ID(element_id),如果您沒有設置它,將使用當前文章:
$args = array(
'key' => 'prod_to_bars', // 這是您的連接鍵名稱。必填欄位。
'element_id' => 1, // 文章 ID。在主循環中不需要。
'element_type' => 'any', // 過濾的文章類型,可以是陣列。選填欄位。預設為 'post'。(可以是 'any' 讀取所有文章)
'status' => 'any', // 過濾的文章狀態,可以是陣列。選填欄位。預設為 'publish'。(可以是 'any' 讀取所有文章)
'direction' => 'any', // 連接方向。默認為 'any'。選填欄位。以下為詳細說明(可以是 'any' | 'from_to' | 'to_from')
);
global $P2P_Relationships;
$rel_posts = $P2P_Relationships->get_raw ( $args );
// 只有文章陣列
外掛標籤
開發者團隊
原文外掛簡介
This plugin allows you to create many-to-many relationships between posts of any type: post, page, custom post types, etc.
Configure post 2 post connections easily in a friendly interface.
The new connection metaboxes will appear on the related post edition pages. Search text, post type and term combo filter available as option for it.
Use the standard WP_Query() and get_posts() to get the related posts.
Solid-rock relationships: use his own database table, updated on post status change and removed on post deletion.
Getting related: the WP_Query way
// inside main loop, current post ID (p2p_rel_post_id) not needed, current post will be used if you don't set it:
$args = array(
'p2p_rel_key' => 'prod_to_bars', // This is your connection key name. Required.
'p2p_rel_post_id' => 1, // The post ID. Inside main loop dont needed.
'p2p_rel_direction' => 'any', // The connection direction. 'any' by default. Optional. Explained below. ( can be 'any' | 'from_to' | 'to_from' )
// Of course, here you can add the standard WP arguments you need: post type, status, dates, pagination, etc.
);
// (at this point, as any other WP looping):
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '
- ';
- ' . get_the_title() . '
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '
';
}
echo '
';
} else {
// no posts found
echo '
Nothing related
';
}
// Restore original Post Data
wp_reset_postdata();
Getting related: the get_posts() way
// inside main loop, current post ID (p2p_rel_post_id) not needed, current post will be used if you don't set it:
$args = array(
'p2p_rel_key' => 'prod_to_bars', // This is your connection key name. Required.
'p2p_rel_post_id' => 1, // The post ID. Inside main loop dont needed
'p2p_rel_direction' => 'any', // The connection direction. 'any' by default. Optional. Explained below. ( can be 'any' | 'from_to' | 'to_from' )
'post_type' => 'any', // The filtered post types, can be an array. Optional. 'post' by default. (can be 'any' for all)
'suppress_filters' => false // Required
// Of course, here you can add the standard WP arguments you need: post type, status, dates, pagination, etc.
);
// (at this point, as any other WP looping):
$rel_posts = get_posts ( $args );
print_r( $rel_posts );
Getting related: getting it raw
// inside main loop, current post ID (element_id) not needed, current post will be used if you don't set it:
$args = array(
'key' => 'prod_to_bars', // This is your connection key name. Required.
'element_id' => 1, // The post ID. Inside main loop dont needed.
'element_type' => 'any', // The filtered post types, can be an array. Optional. 'post' by default. (can be 'any' for all)
'status' => 'any' // The filtered post status, can be an array. Optional. 'publish' by default. (can be 'any' for all)
'direction' => 'any', // The connection direction. 'any' by default. Optional. Explained below. ( can be 'any' | 'from_to' | 'to_from' )
);
global $P2P_Relationships;
$rel_posts = $P2P_Relationships->get_raw ( $args );
// Only an array of related post IDs, not the posts objects.
print_r ( $rel_posts );
The connection direction
By default, the connections are bidirectional (any). However, you can get related posts only in one direction: ‘from_to’ or ‘to_from’.
You can do the same logic at interface level for your users in the backoffice: you can setup your connection hidding the from metabox or the to metabox (UI mode setting).
