itsource

워드프레스에서 부모 페이지 ID와 현재 페이지의 제목을 얻는 방법은?

mycopycode 2023. 9. 19. 21:03
반응형

워드프레스에서 부모 페이지 ID와 현재 페이지의 제목을 얻는 방법은?

여기 내 코드가 작동하지 않는 것이 있습니다.

global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );

하지만 이건 안 돼요

미리 감사드립니다.

상위 페이지 ID의 경우

$post->post_parent;

현재 페이지 제목의 경우

$post->post_title;

상위 페이지 ID의 경우

<?php
  echo wp_get_post_parent_id(get_the_ID());
?>    

구텐베르크에서:

wp.data.select('core/editor').getEditedPostAttribute('parent')

희망은 누군가에게 도움이 될 것입니다.

원하는 경우: 포스트 부모에 대한 링크를 만듭니다.

<a href="<?= get_permalink($post->post_parent) ?>">
  <?= get_the_title($post->post_parent) ?>
</a>
&rarr; <?= the_title() ?>

결과는 다음과 같습니다.

최신 뉴스 일부 뉴스 제목

Astra 테마 및 페이지 템플릿 보기.php 나는 이것을 했습니다: $post->post_parent; 내 기능이 루프를 벗어나면 nbot이 작동하지 않을 것입니다.functions.php로 실행합니다.$post->post_parent는 페이지 템플릿에 삽입할 때는 완벽하게 작동하지만 테마 기능을 편집할 때는 작동하지 않습니다 ;)

function add_script_before_header() {

  $current = $post->ID;
  $parent = $post->post_parent;
  $grandparent_get = get_post($parent);
  $grandparent = $grandparent_get->post_parent;

  if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
    echo get_the_title($grandparent); 
  }

  $after = $parent;
  if ( is_page_template( 'look.php' ) ) {
            echo $after . ' - ';
  }
}

언급URL : https://stackoverflow.com/questions/43249168/how-to-get-parent-page-id-and-title-of-current-page-in-wordpress

반응형