Metabox多自定义帖子选择->显示所选项目?

时间:2019-08-21 作者:artist learning to code

我正在构建一个需要一些交叉信息的主题(?)自定义帖子类型之间。

我有一个元框来选择自定义帖子类型,并将它们显示在另一个自定义帖子类型的单个页面中。到目前为止,事情的成功率是50%。。。

现在我正在使用此代码显示他们的缩略图(但我需要他们的标题和自己的meta):

$guests = get_post_meta($post->ID, \'cl_guest\', true);
$guest_id = get_post_meta(get_the_ID(), \'cl_guest\', true);
$guest_post = get_post( $guest_id[0] ); 
foreach($guest_id as $guest) { 
echo get_the_post_thumbnail($guest_post->ID, \'thumbnail\', array( 
       \'class\' => "img-responsive img-circle", 
       \'alt\' => trim(strip_tags( $guest_post->post_title )),
       \'title\' => trim(strip_tags( $guest_post->post_title ))
       )); 
        }
它显示正确的帖子数量,但重复第一项(按升序id排序)。

感谢您的帮助<;3.

1 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

根据你发布的代码,我想cl_guest post meta包含一个post ID数组。基于这个假设,您可以这样做。

1) 将这些助手函数添加到主题的功能中。php文件

// custom template tag to render guests
function render_guests( array $guests ) {
  if ( $guests ) {
    // you could use foreach loop instead of array_walk
    array_walk( $guests, \'render_guest\' );
  }
}

// Helper function to get guest ids
function get_guest_ids( int $id ) {
  $meta = get_post_meta( $id, \'cl_guest\', true);
  return ( $meta && is_array( $meta ) ) ? array_map( \'intval\', $meta ) : array();
}

// helper function to render single guest
function render_guest( int $id ) {
  // get meta if needed
  $meta = get_guest_meta( $id );
  $title = get_the_title( $id );
  $thumbnail = get_guest_thumbnail( $id, $title, $title );
  // modify output as needed
  echo \'<div class="guest">\' . $thumbnail . $title . \'</div>\';
}

// helper function
function get_guest_meta( int $id ) {
  return get_post_meta( $id, \'some_meta_key\', true );
}

// helper function
function get_guest_thumbnail( int $id, string $alt = \'\', string $title = \'\' ) {
  return get_the_post_thumbnail(
    $id, 
    \'thumbnail\', 
    array( 
     \'class\' => "img-responsive img-circle", 
     \'alt\'   => esc_attr( $alt ),
     \'title\' => esc_attr( $title )
    )
  );
}
2)将自定义tempalte标记添加到您要显示来宾的帖子模板中

<?php render_guests( get_guest_ids( get_the_ID() ) ); ?>
还有,你可能想知道为什么我要将代码拆分为多个函数。原因是我只是在练习一种新的编码风格,其中每个函数都应该做一件事

EDIT 1下面是如何分别渲染图像和标题+元数据。第一对新的助手函数。

function render_guest_images( array $ids ) {
  if ( $ids ) {
    array_walk( $ids, \'render_guest_image\' );
  }
}

function render_guest_image( int $id ) {
  $title = get_the_title( $id );
  echo get_guest_thumbnail( $id, $title, $title );
}

function render_guest_infos( array $ids ) {
  if ( $ids ) {
    array_walk( $ids, \'render_guest_info\' );
  }  
}

function render_guest_info( int $id ) {
  $title = get_the_title( $id );
  $meta = get_guest_meta( $id ); // this could either return data that you render here or ready-to-use html that you just concatenate to the echo below
  echo \'<div class="guest-info">\' . $title . \'</div>\';
}

// add this before you add the rendering functions on your post template
$guest_ids = get_guest_ids( get_the_ID() );

// somewhere on your post template
render_guest_images( $guest_ids );

// somewhere else on your post template
render_guest_infos( $guest_ids );

EDIT 2

没有单独的辅助对象和渲染函数

// before any loops
$guest_ids = get_post_meta( get_the_id(), \'cl_guest\', true);
$guest_ids = ( $guest_ids && is_array( $guest_ids ) ) ? array_map( \'intval\', $guest_ids ) : array();

$guest_posts = array();
if ( $guest_ids ) {
  foreach ( $guest_ids as $guest_id ) {
    if ( $post = get_post( $guest_id ) ) {
      $guest_posts[] = $post;      
    }
  }
}

// titles only
if ( $guest_posts ) {
  foreach( $guest_posts as $guest_post ) {
    echo esc_html( $guest_post->post_title );
  }
}

// thumbnails
if ( $guest_posts ) {
  foreach( $guest_posts as $guest_post ) {
    echo get_the_post_thumbnail( 
      $guest_post->ID, 
      \'thumbnail\', 
      array( 
      \'class\' => "img-responsive img-circle", 
      \'alt\'   => esc_attr( $guest_post->post_title ),
      \'title\' => esc_attr( $guest_post->post_title )
      )
    );
  }
}

// meta only
if ( $guest_posts ) {
  foreach( $guest_posts as $guest_post ) {
    $meta = get_post_meta( $guest_post->ID, \'some_meta_key\', true );
    // do something with meta
  }
}