2014-10-10 9 views
0
나는 기능이

나는 워드 프레스 사이트의 이동 경로를 만드는 데 사용에 AJAX를 통해 이동 경로를 만들기 위해 매개 변수로 게시물 ID를 전달하는 코드를 수정 :워드 프레스

function the_breadcrumb() { 
    $delimiter = '>'; 
    $currentBefore = '<li><a>'; 
    $currentAfter = '</a></li>'; 
    if (!is_home() && !is_front_page() || is_paged()) { 
     echo '<nav class="breadcrumb"><ul>'; 
     global $post; 
     if (is_page() && !$post->post_parent) { 
      echo $currentBefore; 
      the_title(); 
      echo $currentAfter; } 
     elseif (is_page() && $post->post_parent) { 
      $parent_id = $post->post_parent; 
      $breadcrumbs = array(); 
      while ($parent_id) { 
       $page = get_page($parent_id); 
       $breadcrumbs[] = '<li><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a></li>'; 
       $parent_id = $page->post_parent; 
      } 
      $breadcrumbs = array_reverse($breadcrumbs); 
      foreach ($breadcrumbs as $crumb) echo $crumb; 
      echo $currentBefore; 
      the_title(); 
      echo $currentAfter; 
     } 
     echo '</ul></nav>'; 
    } 
} 

하지만이 싶습니다 함수는 해당 페이지의 탐색 경로를 만드는 AJAX 함수에서이를 사용하기 위해 post_id (페이지의 id)를 매개 변수로 사용합니다.

function ajaxify() { 
    $post_id = $_POST['post_id']; 
    $breadcrumb = the_breadcrumb($post_id); 
    print_r($breadcrumb); 
    die(); // remove trailing 0 
} 

어떻게 구현할 수 있습니까?

많은 도움에 감사드립니다.

+0

당신은 함수에서 매개 변수로 $의 post_id를 추가해야합니다. 그런 다음, 함수는 텍스트를 반향하지 않고 문자열로 리턴해야합니다. – kainaw

+0

@kainaw 어떻게 할 수 있습니까? 제발 도와 줄 수있어? – Gab

+0

@kainaw 알았어. 다시 한번 감사드립니다. – Gab

답변

0

는이 같은 wp_query와 포스트 정보를 인수로 post_id를 전달하고 검색해야합니다

function the_breadcrumb($post_id){ 
$delimiter = '>'; 
$currentBefore = '<li><a>'; 
$currentAfter = '</a></li>'; 

// here your query 
$args = array(
    'page_id' => $post_id, // id of a page, post, or custom type 
    'post_type' => 'any'); 

// here is the informations 
$myPost = new WP_Query($args); 

if (!is_home() && !is_front_page() || is_paged()) { 
    echo '<nav class="breadcrumb"><ul>'; 

    if (is_page() && !$myPost->post_parent) { 
     echo $currentBefore; 
     the_title(); 
     echo $currentAfter; } 
    elseif (is_page() && $myPost->post_parent) { 
     $parent_id = $myPost->post_parent; 
     $breadcrumbs = array(); 
     while ($parent_id) { 
      $page = get_page($parent_id); 
      $breadcrumbs[] = '<li><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a></li>'; 
      $parent_id = $page->post_parent; 
     } 
     $breadcrumbs = array_reverse($breadcrumbs); 
     foreach ($breadcrumbs as $crumb) echo $crumb; 
     echo $currentBefore; 
     the_title(); 
     echo $currentAfter; 
    } 
    echo '</ul></nav>'; 
}