【问题标题】:Wordpress loop media by "uploaded By"Wordpress 循环媒体由“上传者”
【发布时间】:2015-06-06 22:53:27
【问题描述】:

我有一个网页,我想展示某个作者上传的图片。

如果我查看媒体,在后端,每张图片都有一个“上传者”属性,然后是作者姓名。


(来源:discoveryourwonder.com

我试过使用这个循环:

<?php
// The Query
$args = array(
   'author'      => $author_name_variable, // Replace with author id
   'post_status' => 'any',
   'post_type'   => 'attachment'
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . the_content() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

这是非常错误的。有些作者会显示每个媒体文件,有些则不显示;其余的都是不准确的。这是一个真正的盲目射击:/

目标是遍历所有媒体文件,然后将所有文件的the_content() 发布为具有相应的上传者名称。

【问题讨论】:

    标签: php wordpress loops gallery author


    【解决方案1】:

    如果有人能评论为什么 ID 比“作者”论点中的蛞蝓更可靠,我将不胜感激。

    我想出了一个解决方案。显然,“作者”参数不喜欢用户名,所以我将其转换为 ID,它现在可以工作了。我使用get_user_by( 'slug', $username ); 来获取特定用户名的所有信息,然后将该数组分配给一个变量。然后我过滤变量以仅使用 ID 并将其通过参数传递。

    这是工作循环:

    <?php
    // The Query
    $profileid = get_user_by( 'slug', $username );
    $args = array(
       'author'      => $profileid->id, // Replace with author id
       'post_status' => 'inheret',
       'post_type'   => 'attachment'
    );
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . the_content() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 2012-12-26
      • 2012-07-25
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多