��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>I need a way to filter the titles in the page parent dropdown on the editor page
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

I need a way to filter the titles in the page parent dropdown on the editor page

programmeradmin4浏览0评论

We are using custom breadcrumb titles for a hierarchical custom post type. We would like to have the page parent dropdown use the breadcrumb title if it is available and then the title if it is not.

We are using custom breadcrumb titles for a hierarchical custom post type. We would like to have the page parent dropdown use the breadcrumb title if it is available and then the title if it is not.

Share Improve this question asked Mar 12, 2019 at 11:22 Dane MorganDane Morgan 1961 silver badge8 bronze badges 4
  • How is the custom breadcrumb title set? With a plugin? – Jacob Peattie Commented Mar 12, 2019 at 11:25
  • Correct. We are setting it through the Yoast plugin. – Dane Morgan Commented Mar 12, 2019 at 11:31
  • 2 Here's the relevant code: github/WordPress/WordPress/blob/5.1/wp-admin/includes/… Those args are passed to get_pages; I don't know if you can use the page_attributes_dropdown_pages_args filter to set up the query how you want? Alternatively you might need to hook into wp_dropdown_pages, or replace its output with a filter. – Rup Commented Mar 12, 2019 at 11:50
  • Yes, I've drilled down through this section into wp_dropdown_pages and wp_parse_args. I can see in these how I could filter the query itself to return a different set or subset of pages, but I can't see how I could filter the output to replace the title with another value. – Dane Morgan Commented Mar 12, 2019 at 12:23
Add a comment  | 

1 Answer 1

Reset to default 1

I suppose you aren't still looking for this, but I was, and I managed to figure out a solution. I'm posting it up here as this came up pretty high in the search results.

What I did was to filter on the dropdown box generator for WordPress.

It passes in an array of args. One is the id of the select element, which for that part of the page is parent_id, and it also passes in the post_type so I restricted that to the post type I wanted to modify.

The technique is then to modify the array of WP_Query objects with a new title.

Unfortunately, by the time this filter is called, the full html markup is generated for the select box, so what I did was to just grab that snippet of html out of the WordPress core and put it into my filter. That way it would regenerate it with my modified WP_Query objects.

function customise_parent_page_dropdown($output, $parsed_args, $pages)
{
// CUSTOMISE HERE TO FILTER TO THE POST_TYPE YOU WANT
    if("parent_id" === $parsed_args['id'] && "mix-question" === $parsed_args['post_type']) {
        // update the page titles
        foreach($pages as $key => $page){
// CUSTOMISE HERE TO ALTER THE TITLE HOW YOU WANT
            $previous_answer = get_field('previous_answer', $pages[$key]->ID);
            if($previous_answer !== "") {
                $pages[$key]->post_title = $previous_answer . " – " . $pages[$key]->post_title;
            }
        }

        // regenerate the select tag - clone of the wordpress core code
        // https://github/WordPress/WordPress/blob/d5b8d282e8c03929aafb1fa660181b7db9ad55d3/wp-includes/post-template.php#L1187
        if ( ! empty( $pages ) ) {
            $class = '';
            if ( ! empty( $parsed_args['class'] ) ) {
                $class = " class='" . esc_attr( $parsed_args['class'] ) . "'";
            }
    
            $output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";
            if ( $parsed_args['show_option_no_change'] ) {
                $output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n";
            }
            if ( $parsed_args['show_option_none'] ) {
                $output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";
            }
            $output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );
            $output .= "</select>\n";
        }
    }

    return $output;
}
add_filter('wp_dropdown_pages', 'customise_parent_page_dropdown', 100, 3);

In my example code I'm pulling in an ACF field that I wanted to prefix it with. Change this to whatever you want.

Potential pitfalls with this approach are that if somebody calls the dropdown generator on this post type with an id of parent_id it will match and filter.

Also if WordPress changes the way it generates the select tag in the future then it will need to be updated here as well. I think these are reasonable trade-offs for getting the solution I wanted.

发布评论

评论列表(0)

  1. 暂无评论