【问题标题】:Wordpress single profile page with acf gallery sharing images带有 acf 库共享图像的 Wordpress 单一个人资料页面
【发布时间】:2020-03-03 03:19:59
【问题描述】:

下午好开发人员,我开发了一个项目,其中包含用户个人资料,其中包含使用 ACF 字段创建的记录,包括图片库字段,我的问题是......因为我只使用一个页面来显示此个人资料,图片库即使使用“附加到帖子”选项也可以共享,因为我只使用一个页面,例如“/profile?userid=userid”。

还有其他好的做法吗?

我想要建议。

零件轮廓编辑

function acf_profile_edit( $atts ) {

$a = shortcode_atts( array(
    'field_group' => ''
), $atts );

$uid = get_current_user_id();

if ( ! empty ( $a['field_group'] ) && ! empty ( $uid ) ) {
    $options = array(
        'post_id' => 'user_'.$uid,
        'field_groups' => array( intval( $a['field_group'] ) ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() )
    );

    ob_start();

    acf_form( $options );
    $form = ob_get_contents();

    ob_end_clean();
}

return $form;
}

add_shortcode( 'profile_edit', 'acf_profile_edit' );

编辑... 这段代码解决了我的问题

add_filter('ajax_query_attachments_args', 'restrict_images_to_user');
function restrict_images_to_user($query) {
    $gallery_images = 'field_5e4d799b34145';
    $gallery_videos = 'field_5e5597e37f2c7';
    if ( isset($_POST['query']['_acfuploader'] )
        && ( isset($_POST['query']['_acfuploader']) == $gallery_images || isset($_POST['query']['_acfuploader']) == $gallery_videos ) ) {

        $author = get_current_user_id();
        $query['author'] = $author;
    }
    return $query;
}

【问题讨论】:

  • 如果你使用 userid=userid 怎么能不被黑?
  • 感谢您的回答,在“/profile?userid=userid”的情况下,我只在显示个人资料页面上使用它,在编辑页面上我使用 word_press 函数 get_current_user_id()。我编辑了答案

标签: wordpress advanced-custom-fields


【解决方案1】:

如果您使用的是一个页面,请使用名为 page-profile.php 的页面模板,page- 后面的位必须与您分配的页面名称匹配。

然后使用 WordPress Post Loop:

if (have_posts()) :
   while (have_posts()) :
      the_post();
         the_content();
   endwhile;
endif;

您可以为帖子分配用户名,帖子循环只会返回与该用户名关联的内容。

另一种方法是获取当前用户名,然后您可以在用户名中添加一个额外的acf字段然后进行检查。

这是一个示例,完全正确,但也许可以为您提供建议。

If(content && username == currentuser)
{
   $content = acf content;
}

稍后在您的页面中,您可以在需要的地方回显内容,并且仅是当前用户名的内容,或者您​​也可以使用用户 ID 来执行此操作。

【讨论】:

  • 你的建议正是我所做的,在这里重新考虑,我认为这是一个 acf 错误,因为即使在后端,配置文件仍然共享相同的图像库,这不可能发生,在至少在我的例子中。
  • ACF 将有一个支持团队,我建议将此问题添加到他们的支持论坛:)
  • 好主意,谢谢你的时间
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 2018-12-09
  • 1970-01-01
相关资源
最近更新 更多