简体   繁体   English

该帖子的永久链接不起作用

[英]Permalink for the post doesn't work

I have a page which retrieves blogposts from several blogs under the same tag or category. 我有一个页面,可以检索来自同一标签或类别下的多个博客的博客文章。 I have problems with retrieving the permalink for the blogpost, it redirects to the same blog all the time and says that the page doesn't exist, ie it always goes to blog #5 stated in the 5th line and ignores the switch statement Here is my code: 我在检索博客文章的永久链接时遇到问题,它始终重定向到同一博客,并说该页面不存在,即它始终转到第5行中所述的博客#5,而忽略了switch语句。我的代码:

<?php 
        global $wpdb;
        global $post;
        $tag = $_GET['avain'];
        $postarr = array();
        switch_to_blog( 5 );
        $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id != {$wpdb->blogid} AND site_id = '{$wpdb->siteid}'  AND spam = '0' AND deleted = '0' AND archived = '0'", ARRAY_A);
        array_unshift($blogs, 1); 
        foreach($blogs as $blog) {
            $wpdb->set_blog_id($blog[ 'blog_id' ]);
            $tax_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE (taxonomy = 'post_tag' OR taxonomy = 'category') AND term_id IN (SELECT term_id FROM $wpdb->terms WHERE name='$tag')");
            $post_id = $wpdb->get_results("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = $tax_id");
             foreach ($post_id as $id) {
$postarr[] = $id->object_id; 
}

$postsAsCommaSepString = implode( ',', $postarr ); 

$posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE ID IN    ($postsAsCommaSepString)");
            foreach ($posts as $post):setup_postdata($post);
            $author = get_the_author();
            switch ($author) {
case "Kate":
    switch_to_blog(3);
    break;
case "Maria":
    switch_to_blog(4);
    break;
case "Germanica":
    switch_to_blog(5);
    break;

}
                        ?>

                <div class="post"><header class="post-header"><div class="date-holder"><span><?php the_time('j F, Y') ?></span></div>
                <a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a></header>
                <div class="post-content"><?php the_content(); ?></div></div>
            <?php endforeach; 
            wp_reset_postdata(); 
        }
        ?>

Is that because of you put 是因为你放

 switch_to_blog(5);

in the 5th line, and the page redirects you to that blog BEFORE the switch statement is even read? 在第5行中,并且该页面将您重定向到该博客,甚至尚未读取switch语句?

Just asking, I don't really understand your code, but I hope you find this helpful! 只是问,我不太了解您的代码,但是希望您对此有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM