【问题标题】:Dynamically link to child page WordPress动态链接到子页面 WordPress
【发布时间】:2010-11-27 17:24:56
【问题描述】:

我在 WP 网站上使用子页面如下;

产品(父页面) -办公室(子页面 1) --Office Gallery(子页面1的孩子) -学校(第2页) --学校画廊(子页面2的孩子) ....等等

如何在我的主题中仅使用一个模板在每个子页面上创建指向其子页面的链接?我需要能够给这个链接一个 css 类名。换句话说,我需要让我的页面模板中的代码看起来像:

<a class="gallery-button" href="RETURN LINK TO CHILD PAGE OF CURRENT PAGE"></a>

或者类似的......

我尝试将 wp_list_pages 用于 WordPress Codex 中的子页面,但这会返回一个列表,我真的只需要指向子页面的永久链接。

这容易吗?不可能?

提前致谢。

【问题讨论】:

    标签: wordpress hyperlink


    【解决方案1】:

    您需要查询帖子以获取相关帖子的第一个子项;

    <?php
        if ($children = get_children('post_type=page&numberposts=1')) {
            $first_child = $children[0];
            $first_child_permalink = get_permalink($first_child->ID);
            echo '<a class="gallery-button" href="' . $first_child_permalink . '">Link Text</a>';
        }
    ?>
    

    【讨论】:

      【解决方案2】:

      这是我在 WP 3.9 中使用的 TheDeadMac 代码的修订版:

      $children = get_pages("child_of=".$post->ID."&sort_column=menu_order");
      $first_child = $children[0];
      $first_child_permalink = get_permalink($first_child->ID);
      echo '<a href="' . $first_child_permalink . '">Link Text</a>';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 2013-09-22
        • 2021-01-09
        • 1970-01-01
        • 2012-12-12
        • 1970-01-01
        相关资源
        最近更新 更多