[英]If wordpress user is Name, show post category Y
到目前為止,這是我需要的:
在自定義頁面中,我需要顯示不同的帖子標題,具體取決於訪問它的用戶。 例如:
如果用戶(id或名稱,無論是什么)是“ username1”,我要顯示類別1的帖子列表。如果用戶(id或名稱,無論是什么),是“ username2”,我想顯示類別的帖子列表2。
我有的:
<?php
$user_id = get_current_user_id();
if ($user_id = 36) {?>
<?php $catquery = new WP_Query( 'resume_category=26&posts_per_page=50'
); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php
the_title(); ?></a></li>
<?php endwhile;
wp_reset_postdata();}
?>
謝謝
這是基於我所知道的信息,因此可能需要進行調整,但是您的“ resume_category”看起來像是一個分類法,這意味着您需要稍微不同地創建WP_Query。
$user_id = get_current_user_id();
if ($user_id == 36) {
$args = array(
'post_type' => 'YOUR POST TYPE',
'posts_per_page' => 50,
'tax_query' => array(
array(
'taxonomy' => 'resume_category',
'field' => 'term_id',
'terms' => '26',
),
),
);
$query = new WP_Query( $args );
echo '<ul>';
while($query->have_posts()) : $query->the_post();
echo"<li><a href=\"".the_permalink()."\"rel=\"bookmark\">".the_title()."</a></li>";
endwhile;
wp_reset_postdata();
}
這仍然是一個非常手動的過程,因為您需要為允許加入的每個用戶手動編輯代碼。
您想要做的是有一個自定義元字段,該字段可將resume_category中的可用選項作為選擇列表,多重選擇器等,使您可以為用戶分配類別。 您需要在自定義字段中存儲該分類法中所選分類的term_id。
然后,您可以執行以下操作:
獲取用戶類別。
$user_categories = get_user_meta($user_id, 'categories_field');
然后建立您的稅收查詢。
$terms = array();
foreach $user_categories as $category {
$terms[] = $category;
}
然后您的查詢變為:
$args = array(
'post_type' => 'YOUR POST TYPE',
'posts_per_page' => 50,
'tax_query' => array(
$tax_query = array(
'taxonomy' => 'resume_category',
'field' => 'term_id',
'terms' => $terms,
)
),
);
PS:此代碼未經測試,因為我直接在此處編寫,但為您提供了如何使類別選擇更加動態的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.