如果wordpress用户是姓名,则显示帖子类别Y.

问题描述 投票:1回答:1

这是我到目前为止所需要的:

在自定义页面中,我需要显示不同的帖子标题,具体取决于访问它的用户。例如:

如果用户(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();}
?>

谢谢

php wordpress
1个回答
2
投票

这基于我所知道的信息,所以它可能需要调整,但你的“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_ids。

然后你可以这样做:

获取用户类别。

$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,
                )
            ),
        );

P.S:这段代码是未经测试的,因为我直接在这里写,但是让您了解如何使类别选择更具动态性。

© www.soinside.com 2019 - 2024. All rights reserved.