在 WordPress 中回显父页面的副标题 - 第 (2) 部分 [英] Echo the subtitle of a PARENT page within WordPress - Part (2)

查看:21
本文介绍了在 WordPress 中回显父页面的副标题 - 第 (2) 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始主题: 在 WordPress 中回显父页面的副标题?

我发现对 Mark 生成的与上述原始主题相关的代码有一个单独的需求.幸运的是,我能够与另一个社区成员合作实现类似的功能来处理这个问题也将涉及的相同元素的 href.

I've discovered a separate need for the code Mark produced in relation to the original topic above. Luckily, I was able to work with another community member to achieve a similar function to handle the href for the same element this question will relate too.

我需要根据父页面的副标题值填充标题标签.但是,如果没有设置父级,我需要它默认为整个站点的默认子标题.

I need to have the title tag populate based off the subtitle value of the parent page. However, if no parent is set, I need it to default to the default subhead of the entire site.

我们能够在此处为 href 属性实现此目的:如何根据设置的 post_parent 设置链接 - WordPress

We were able to achieve this for the href attribute here: How to set a link based off a post_parent being set - WordPress

当前代码是这样处理的:

<?php
    if ($post->post_parent) {
        $permatitle = get_post_meta($post->post_parent, '_base_page_subtitle', true);
    } else {
        $permatitle = get_post_meta($post->ID, '_base_page_subtitle', true);
    }
?>

<a href="<?php echo $permalink; ?>" title="<?php echo $permatitle; ?>">

但是,我也需要它在子页面上工作.任何帮助将不胜感激,我会为任何可以为解决此问题做出贡献的人点赞.

However, I need it to work on child pages as well. Any help would be appreciated and I'll upvote anyone that can contribute to solving this.

推荐答案

如果您正在尝试实现相同的目标 (参考此处) 但这次是标题,

if you are trying to achieve the same thing (refer here) but this time for the title,

你可以这样做:

<?php
if ($post->post_parent!=0) {
    // for child pages
    $permatitle = get_post_meta(end( get_ancestors( get_the_ID(), 'page' )), '_base_page_subtitle', true); 
} elseif($post->ID==0||count(get_pages('child_of='.$post->ID))==0) { 
    //for HP or pages with no child
    $permatitle = get_post_meta(get_option( 'page_on_front' ), '_base_page_subtitle', true); 
} else { 
    // for top level pages/parents
    $permatitle = get_post_meta($post->ID, '_base_page_subtitle', true); 
} 

或者更好的是,将两者结合起来:

<?php 
if ($post->post_parent!=0) {
    // Handling of Child Pages
    $permalink = get_permalink( end( get_ancestors( get_the_ID(), 'page' )));
    $permatitle = get_post_meta(end( get_ancestors( get_the_ID(), 'page' )), '_base_page_subtitle', true); 
} elseif($post->ID==0||count(get_pages('child_of='.$post->ID))==0) {
    // Homepage or Pages with no Parent
    $permatitle = get_post_meta(get_option( 'page_on_front' ), '_base_page_subtitle', true);
    $permalink = home_url();
} else { 
    // Handling of Top Level/Parent Pages
    $permatitle = get_post_meta($post->ID, '_base_page_subtitle', true);
    $permalink = get_permalink( end( get_ancestors( get_the_ID(), 'page' )));
}

然后您可以在以下之后执行此操作:

<a href="<?php echo $permalink; ?>" title="<?php echo $permatitle; ?>">

这篇关于在 WordPress 中回显父页面的副标题 - 第 (2) 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆