1

我试过了,它实际上显示的是当前页面内容而不是父内容:

<?php
   $my_postid = 3105;
   $content_post = get_post($my_postid);
   $content = $content_post->post_content;
   $content = apply_filters('the_content', $content);
   echo $content;
?>

并且还尝试了以下方法,但我收到警告:

Warning: Missing argument 1 for get_page(), called in:

代码:

  <?php 
     $page = get_page();
     $parentID = $page['post_parent'];
     if($parentID != '0') {
       $parentPage = get_page($parentID);
       $parentContent = $parentPage['post_content'];
       $parentContent =  apply_filters('the_content', $parentContent); 
       echo $parentContent;
     } 
  ?>
4

3 回答 3

2
global $post;
if ($post->post_parent) {
    $parent = get_page($post->post_parent);
    echo apply_filters('the_content', $parent->post_content);
}
于 2012-06-28T18:19:23.893 回答
1

给定一些加载的帖子:

$id = 3105;
$content_post = get_post($id);

您可以获取其父级:

$parent_id = $content_post->post_parent; // Will be 0 if the post has no parent
$parent = get_post($parent_id);

并输出内容:

echo $parent->post_content;
于 2012-06-28T15:22:04.770 回答
0

这就是最终起作用的方法,关键是使用全局 $post;

<?php
 global $post;
 $args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->post_parent,
'order_by' => 'menu_order',
'order' => 'ASC'
);
 $attachments = get_posts($args);
 if($attachments)
  {
   echo '<ul class="imagelist">';
   foreach($attachments as $attachment)
 {
   echo '<li>';
   $large = wp_get_attachment_image_src($attachment->ID, 'large');
   $thumb = wp_get_attachment_image($attachment->ID, 'thumbnail');
   echo '<a href="'. $large[0] .'">' . $thumb . '</a>';
   echo '</li>';
 }
  echo '</ul>';
 }
?>
于 2012-06-29T08:09:36.280 回答