B
B
bulkmaker2019-11-20 09:13:31
Search Engine Optimization
bulkmaker, 2019-11-20 09:13:31

Ring linking with custom post types?

I have a code which I use for ring linking.

<?php
/**
 * Plugin Name: Линковка проектов
 */
function circular_links_post_ids($n){ // Линковка проектов 
  global $post;
  $nDisplay = $n;
  $cats = wp_get_post_categories($post->ID);
  if ($cats) {
    $args=array(
      'category__in' => $cats,
      'posts_per_page'=>-1,
      'caller_get_posts'=>1,
      'orderby'=>'date',
      'order'=>'ASC',
    );
 		$myposts = get_posts($args);
    foreach($myposts as $position=>$mypost){
      if($mypost->ID == $post->ID){
        $currPosition = $position-2;
        break;
      }
    }
    $total = count($myposts);
    $postsCount = $total - $currPosition;
    $postIDs = array();
    if($postsCount >= $nDisplay){
      for($i=0;$i<$nDisplay;$i++){
        $postIDs[] = $myposts[$currPosition+$i]->ID;
      }
    }else{
      for($j=0;$j<$postsCount;$j++){
        $postIDs[] = $myposts[$currPosition+$j]->ID;
      }
      $firstPostsNum = $nDisplay - $postsCount;
      for($z=0;$z<$firstPostsNum;$z++){
        $postIDs[] = $myposts[$z]->ID;
      }
    }
    $array2 = array ("ID" => "$post->ID");
    $postIDs = array_diff($postIDs, $array2);
    return $postIDs;
  }else{
    return false;
  }
}

function circular_links($numb){ // Линковка проектов 
    $orig_post = $post;
    $postIDs = circular_links_post_ids($numb);
    $args = array(
      'post__in' => $postIDs,
      'order' => 'ASC'
    );
    $my_query = new WP_Query($args);
    while( $my_query->have_posts() ) {
      $my_query->the_post();
      ?>
      <?php 	get_template_part('parts/card-project2'); // шаблон карточки проекта ?>
    <?php }
    $post = $orig_post;
    wp_reset_query();
}

How can I convert it to custom post types and custom categories?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question