douhu2525 2009-06-12 17:42
浏览 16
已采纳

设置Wordpress页面及其所有子项的标题图像

Wordpress. I've got a handful of pages, each with children -- varying depths.

Let's say it's like this:

+about
-- page1
-- page2
+stuff
-- page1
--- sub-sub-page
-- page2

My problem: I want to set a graphic header for all the "about" pages, including all its children... and a different graphic header for all "stuff" pages and all its children.

I could do this using a custom field for each individual page, but I don't trust my users to remember to set this field.

I thought about adding to the page.php template and telling the page to find its parent top-level page to determine which "family" it's in, then set the header graphic that way...but I don't want to invest a ton of time (and run-time) resources traversing up the hierarchy.

Any ideas?

Much thanks in advance...

UPDATE:

Taking the code from below, I modified it to use outside of the Wordpress Loop (have not yet integrated the "default" value yet).

    <?php 
    global $wp_query;
$postid = $wp_query->post->ID;

$header_swf = get_post_meta($postid, 'header_swf', true);

if (!$header_swf) {
    $parent = get_page($wp_query->post->post_parent); //array of values for this page's parent, directly above it

   while (!$header_swf && $parent) {
        $header_swf = get_post_meta($parent->ID, 'header_swf', true);
        $parent = get_page($parent->post_parent); //new parent
    }

}

?>
  • 写回答

2条回答 默认 最新

  • douhanxujiuji6098 2009-06-12 17:52
    关注

    Note: This answer assumes (perhaps incorrectly) there is no built-in method to do this.

    You could write something in the template that looked for a predefined custom field. If none exists, it could ask the parent page for its version of the custom field, and so on.

    $val = get_post_custom_values('header_image');
    if (!val) {
        $parent = get_page($post->post_parent);
        while (!$val && $parent) {
            $val = get_post_custom_values('header_image', $parent->id);
            $parent = get_page($parent->post_parent);
        }
        if (!$parent && !val) // has failed to find a header image!
            $val = 'my default value';
    }
    

    Or something like that.....

    I should point out that might be really database heavy so it might be worth caching the value for each given URL. I'd personally recommend memcached but there are other methods.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导