get_post_meta( 122, 'key_ismi', true )
Yukaridaki sekilde kullandiysaniz 122 id'li postu aramistir. O da yoksa bos donmustur.
Asagidaki sekilde kullanirsaniz yazinin id'sini otomatik saptar;
$deger = get_post_meta( get_the_ID(), 'key_ismi' );
Siz metabox eklediginiz yontemi paylasin; biz yazdirmaniza yardimci olalim direkt.
fonksiyon kodlarım:
class yazı_ozet { private $config = '{"title":"Yazu0131Ozet","prefix":"yazu0131_ozet","domain":"doz","class_name":"yazu0131_ozet","post-type":["post"],"context":"normal","priority":"default","fields":[{"type":"textarea","label":"Yazu0131 u00d6zet","default":"u00d6zet yazu0131n","id":"yazu0131_ozetyazi-ozet"}]}';
public function __construct() {
$this->config = json_decode( $this->config, true );
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
}
public function add_meta_boxes() {
foreach ( $this->config['post-type'] as $screen ) {
add_meta_box(
sanitize_title( $this->config['title'] ),
$this->config['title'],
[ $this, 'add_meta_box_callback' ],
$screen,
$this->config['context'],
$this->config['priority']
);
}
}
public function save_post( $post_id ) {
foreach ( $this->config['fields'] as $field ) {
switch ( $field['type'] ) {
default:
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = sanitize_text_field( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
}
}
}
public function add_meta_box_callback() {
$this->fields_table();
}
private function fields_table() {
?><table class="form-table" role="presentation">
<tbody><?php
foreach ( $this->config['fields'] as $field ) {
?><tr>
<th scope="row"><?php $this->label( $field ); ?></th>
<td><?php $this->field( $field ); ?></td>
</tr><?php
}
?></tbody>
</table><?php
}
private function label( $field ) {
switch ( $field['type'] ) {
default:
printf(
'<label class="" for="%s">%s</label>',
$field['id'], $field['label']
);
}
}
private function field( $field ) {
switch ( $field['type'] ) {
case 'textarea':
$this->textarea( $field );
break;
default:
$this->input( $field );
}
}
private function input( $field ) {
printf(
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">',
isset( $field['class'] ) ? $field['class'] : '',
$field['id'], $field['id'],
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '',
$field['type'],
$this->value( $field )
);
}
private function textarea( $field ) {
printf(
'<textarea class="regular-text" id="%s" name="%s" rows="%d">%s</textarea>',
$field['id'], $field['id'],
isset( $field['rows'] ) ? $field['rows'] : 5,
$this->value( $field )
);
}
private function value( $field ) {
global $post;
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) {
$value = get_post_meta( $post->ID, $field['id'], true );
} else if ( isset( $field['default'] ) ) {
$value = $field['default'];
} else {
return '';
}
return str_replace( 'u0027', "'", $value );
}}new yazı_ozet;
özet kısmına (özet bölümü yapıyorum) eklediğim kod:
<?php echo get_post_meta( get_the_ID(), 'yazı_ozet', false ); ?>