WordPress.org

Ready to get started?Download WordPress

Forums

Strip the domain name from the_permalink (multisite posts loop) (3 posts)

  1. fizzyfozzy
    Member
    Posted 1 year ago #

    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:

    <?php
    switch_to_blog(1);
    $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;
    wp_reset_query();
    restore_current_blog();
    ?>

    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:
    http://subdomain.mysite.com/mycategory/mypost
    rather than:
    http://mysite.com/mycategory/mypost

    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: http://wordpress.org/support/topic/remove-domain-from-get_permalink
    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.

    Thanks!

  2. haxxxton
    Member
    Posted 1 year ago #

    @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 :P )

  3. fizzyfozzy
    Member
    Posted 1 year ago #

    Hi and thanks for the quick response!

    Yes the page returns the links to the posts as:
    http://subdomain.mysite.com/mycategory/mypost

    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(); ?>">

    Using:

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

    To get the slug.

Topic Closed

This topic has been closed to new replies.

About this Topic