Exemplo n.º 1
0
/**
 * Add the post permalink as a property on the post object.
 *
 * Helpful when you need URLs for a large number of posts and don't want to
 * melt your server with 3000 calls to `get_permalink()`.
 *
 * This is most efficient when $pages contains the complete ancestry for each post. If any post
 * ancestors are missing when calculating hierarchical post names it will load them,
 * at the expensive of a few extra queries.
 *
 * @param  array $pages An array of post objects keyed on post ID. Works with all post types.
 * @return array $pages The input array with $post->url set to the permalink for each post.
 */
function bu_navigation_get_urls($pages)
{
    if (is_array($pages) && count($pages) > 0) {
        foreach ($pages as $page) {
            $url = '';
            if ('page' === $page->post_type) {
                $url = bu_navigation_get_page_link($page, $pages);
            } else {
                if (BU_NAVIGATION_LINK_POST_TYPE === $page->post_type) {
                    $url = $page->post_content;
                } else {
                    $url = bu_navigation_get_post_link($page, $pages);
                }
            }
            $page->url = $url;
        }
    }
    return $pages;
}
Exemplo n.º 2
0
 public function test_bu_navigation_get_post_link_query_var()
 {
     global $wp_rewrite;
     $wp_rewrite->set_permalink_structure('');
     delete_option('rewrite_rules');
     register_post_type('cpt_one', array('public' => true, 'hierarchical' => true, 'query_var' => false));
     register_post_type('cpt_two', array('public' => true, 'hierarchical' => true, 'query_var' => true));
     register_post_type('cpt_three', array('public' => true, 'hierarchical' => true, 'query_var' => 'foo'));
     $cpt_one = $this->factory->post->create(array('post_type' => 'cpt_one', 'post_status' => 'publish'));
     $cpt_two = $this->factory->post->create(array('post_type' => 'cpt_two', 'post_status' => 'publish'));
     $cpt_three = $this->factory->post->create(array('post_type' => 'cpt_three', 'post_status' => 'publish'));
     $cpt_one = get_post($cpt_one);
     $cpt_two = get_post($cpt_two);
     $cpt_three = get_post($cpt_three);
     $this->assertEquals(get_post_permalink($cpt_one), bu_navigation_get_post_link($cpt_one));
     $this->assertEquals(get_post_permalink($cpt_two), bu_navigation_get_post_link($cpt_two));
     $this->assertEquals(get_post_permalink($cpt_three), bu_navigation_get_post_link($cpt_three));
 }