【问题标题】:How to display Custom Post Type for specific post IDs如何显示特定帖子 ID 的自定义帖子类型
【发布时间】:2018-11-21 16:18:28
【问题描述】:

我是 wordpress 新手,正在开发一种名为 RankThem 的自定义帖子类型。成员按数字(通过重力表格提交)每周排名前 5 的社论。我想显示特定于帖子 ID 的成员头像、姓名和提交的排名编号,每个社论都会在帖子 ID 上显示。我一直在努力为每个数据输出编写一个函数,但没有成功。任何帮助将不胜感激。

function get_rankthem_post_types()
{
    $args = array(
        'posts_per_page' => -1,
        'post_type'=>'rankthem'
    );
    $_PostID = get_posts($args);

    /*string to return*/
    $content = '';

    foreach($_PostID as $key=>$val)
    {
        $content .= '<strong>'.$val->user_id.'</strong><br />'; 
        $content .= $val->user_avatar.'<hr />'; 
        $content .= $val->post_content.'<hr />';
    }

    return $content;
}

【问题讨论】:

  • 嘿 get_posts 返回一个帖子数组,因此您必须循环遍历它两次,每个帖子一次,然后再次访问每个帖子的数据,例如 user_avatar 等。如果它也可以是空的它没有任何帖子。
  • 哎呀!它没有用。最终将我锁定在管理员之外,并且必须在我的密码被接受之前通过 FTP 删除文件。知道为什么会这样吗?

标签: wordpress custom-post-type gravity-forms-plugin


【解决方案1】:
function get_rankthem_post_types()
{
    $args = array(
        'posts_per_page' => -1,
        'post_type'=>'rankthem'
    );

    //this will be an array of posts
    $posts = get_posts($args);

    /*string to return*/
    $content = '';

    foreach ($posts as $post)
     {
      foreach($post as $key => $val)
      {
        $content .= '<strong>'.$val->user_id.'</strong><br />'; 
        $content .= $val->user_avatar.'<hr />'; 
        $content .= $val->post_content.'<hr />';
      }
    }

    return $content;
}

【讨论】:

    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多