Support » Fixing WordPress » Strip the domain name from the_permalink (multisite posts loop)

  • I am trying to write a loop that will pull in posts from the top level site of my multisite installation into the homepage of the sub-sites.

    So far I have:

    $top_level_posts = new WP_Query("cat=1&posts_per_page=3");
    if ($top_level_posts->have_posts()) : while ($top_level_posts->have_posts()) : $top_level_posts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; else: ?>
    <li>More posts coming soon!</li>
    <?php endif;

    Which displays the correct posts but it’s getting the_permalink wrong as it tries to load the post on the subdomain I am trying to display the list on, rather than the proper link to the post on the top-level domain.

    So it’s putting:
    rather than:

    Can anyone tell me is there a way to trim down the_permalink to strip out the domain name part of the url? As that way I could use
    echo get_site_url(1)
    to put the correct domain name at the beginning of the link and then follow on with the rest of the permalink…

    I found this post:
    which suggests using substr to cut down but that would rely on the subdomain names always being the same length… problem is I have many and they vary in length. They do all end in .com though… so is there a way to strip off anything before (and including) .com maybe?

    Or maybe I am just making this whole thing much more complicated than it needs to be!!?

    Any suggestions appreciated.


Viewing 2 replies - 1 through 2 (of 2 total)
  • @fizzyfozzy

    can you confirm that it is definitely adding the whole domain to the link rather than just ‘/mycategory/mypost’ and that the rest is being added by the browser?

    you may have to have a look using firebug or the view source to see what the PHP is actually rendering out..

    either way i may be able to help but they are different methods for each so id rather ask first than try to write out both.. (tl;dr: im lazy 😛 )

    Hi and thanks for the quick response!

    Yes the page returns the links to the posts as:

    I’d love it is it just returned /mycategory/mypost as then I’d just beable to add echo get_site_url(1) in front of it 🙂

    I have actually just found a work-around that works for me but only because I use a ‘no category base’ plug on this particular installation – so I’d be interested in a better solution if you have one.

    My work-around is:
    <a href="<?php echo get_site_url(1) ?>/<?php echo the_slug(); ?>">


    function the_slug() {
    	$post_data = get_post($post->ID, ARRAY_A);
    	$slug = $post_data['post_name'];
    	return $slug;

    To get the slug.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Strip the domain name from the_permalink (multisite posts loop)’ is closed to new replies.