如何在WP中的父页面上显示子页面的自定义字段?

发布于 2024-09-11 20:26:35 字数 331 浏览 2 评论 0原文

我将如何显示父页面上子页面的两个自定义字段。到目前为止,这是我的代码:

<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
   <ul>
    <?php echo $children; ?>
</ul>
<?php } ?>

如何显示两个标题为 details6details7 的自定义字段?

How would I show let's say two custom fields from the subpages on a parent page. Here's my code so far:

<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
   <ul>
    <?php echo $children; ?>
</ul>
<?php } ?>

How would I show two custom fields with titles details6 and details7?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向日葵 2024-09-18 20:26:35

get_post_meta 函数就是您要寻找的:

<?php 

    // Get the page's children
    $children = get_pages("child_of=" . $post->ID);

    if (!empty($children)) { 
        echo '<ul>';
        foreach($children as $child) {
            // Get the 2 meta values from the child page
            $details6 = get_post_meta($child->ID, 'details6', true); 
            $details7 = get_post_meta($child->ID, 'details7', true); 

            // Display the meta values
            echo '<li>Details 6 = ' . $details6 . ', Details 7 = ' . $details7 . '</li>';
        }
        echo '</ul>';
    }        
?> 

The get_post_meta function is what you're looking for:

<?php 

    // Get the page's children
    $children = get_pages("child_of=" . $post->ID);

    if (!empty($children)) { 
        echo '<ul>';
        foreach($children as $child) {
            // Get the 2 meta values from the child page
            $details6 = get_post_meta($child->ID, 'details6', true); 
            $details7 = get_post_meta($child->ID, 'details7', true); 

            // Display the meta values
            echo '<li>Details 6 = ' . $details6 . ', Details 7 = ' . $details7 . '</li>';
        }
        echo '</ul>';
    }        
?> 
°如果伤别离去 2024-09-18 20:26:35

这也有效。它可能比帕特的版本有更多错误。

    <?php
  $args = array(
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish'

);
$postslist = get_posts($args);
foreach($postslist 为 $post):
setup_postdata($post);
?>

    <?php the_title(); ?>
    <?php the_permalink(); ?>
    <?php the_permalink(); ?>
    <?php echo get('details8'); ?>
    <?php echo get('details9'); ?>


    <?php endforeach; ?>

This works too. It's probably more buggy then Pat's version.

    <?php
  $args = array(
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish'

);
$postslist = get_posts($args);
foreach ($postslist as $post) :
setup_postdata($post);
?>

    <?php the_title(); ?>
    <?php the_permalink(); ?>
    <?php the_permalink(); ?>
    <?php echo get('details8'); ?>
    <?php echo get('details9'); ?>


    <?php endforeach; ?>
断念 2024-09-18 20:26:35
// Get the page's children
$children = get_pages('child_of=' . $post->ID. '&depth=-2' );

if (!empty($children)) { 
    echo '<ul class="localPlaces">';
    foreach($children as $child) {
        // Get the 2 meta values from the child page
        $details6 = get_post_meta($child->ID, 'address 1', true); 
        $details7 = get_post_meta($child->ID, 'number', true); 
        $details8 = get_the_title($child->ID);

        // Display the meta values
        echo '<h3>'. $details8 . '</h3>';
        echo '<li>' . $details6 . ' ' . $details7 . '</li>';
    }
    echo '</ul>';
}       

这个方法至少也显示了帖子的标题

// Get the page's children
$children = get_pages('child_of=' . $post->ID. '&depth=-2' );

if (!empty($children)) { 
    echo '<ul class="localPlaces">';
    foreach($children as $child) {
        // Get the 2 meta values from the child page
        $details6 = get_post_meta($child->ID, 'address 1', true); 
        $details7 = get_post_meta($child->ID, 'number', true); 
        $details8 = get_the_title($child->ID);

        // Display the meta values
        echo '<h3>'. $details8 . '</h3>';
        echo '<li>' . $details6 . ' ' . $details7 . '</li>';
    }
    echo '</ul>';
}       

This method at least shows the title of the post also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文