duanjing4667 2018-06-05 15:41
浏览 23

if:false时如何获得下一个$ args?

I am new in programming... (The question absolutely about programming, but I use wordpress) I try to be very clear:

I have a subdomain based multisite network. If the users posts to their sites, I get a clone from the current post to my network home. This clone have a canonical url, what shows to the original post, and the post slug's also concur (the ID's dont.)

example: x user posted:

   url: xusersite.network.com/4243345/this-is-slug-by-post-title
   canonical: xusersite.network.com/4243345/this-is-slug-by-post-title

I get:

   url: network.com/123677745/this-is-slug-by-post-title
   canonical: xusersite.network.com/4243345/this-is-slug-by-post-title

Now I want get the clone post's ID, on the subsite, by the original post... so I have this code:

   switch_to_blog( 1 );
   $canonical = 'xusersite.network.com/4243345/this-is-slug-by-post-title';
   $slug = 'this-is-slug-by-post-title'; // current slug
   $args = array(
     'name'        => $slug,
     'post_type'   => 'post',
     'post_status' => 'publish',
     'numberposts' => 1
   );
   $my_posts = get_posts($args);

Thats ok, but the problem, if an another user posted with the same title for his blog, example: y user posted:

   url: yusersite.network.com/72543/this-is-slug-by-post-title
   canonical: yusersite.network.com/72543/this-is-slug-by-post-title

I get a post, with the same slug:

   url: network.com/776536556733/this-is-slug-by-post-title
   canonical: yusersite.network.com/72543/this-is-slug-by-post-title

So my php knowledge now here it is, I can do this:

   if( $my_posts ) :
     $cloneid = $my_posts[0]->ID;
     $clonecanonical = wp_get_canonical_url( $cloneid );
     if( $clonecanonical == $canonical ) :
     $exit = 'true';
     else : 
     $exit = 'false';
     endif;
   endif;

How can I get the next $my_posts, so $my_posts[1]->ID, if the $exit is false? How can I get the right $my_posts?

  • 写回答

1条回答 默认 最新

  • dongta1824 2018-06-05 15:55
    关注

    Not sure to have understood, but you should get the numeer of posts in my posts using count($my_posts) and then use a for or while to iterate through the full $my_posts.

    something like this:

        if( $my_posts ) :
        $numPosts=count($my_posts)
        //if $numPosts = 0 then $my_posts[0] does not exist
        //but if you are here $numPosts must be > 0
        if $numPosts > 0 {
            for ( $i = 0; $i < $numPosts; $i++ ){
                $cloneid = $my_posts[$i]->ID;
                $clonecanonical = wp_get_canonical_url( $cloneid );
                if( $clonecanonical == $canonical ) :
            $exit = $my_posts[$i]->ID;
        endif;
            }
        }
        endif;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大