我试图让我的下一个和上一个帖子按钮显示我的自定义字段,但由于某些原因,它没有显示自定义字段,只是空白。这是我的密码。
<div id="next-prev-links">
<?php
$previous_post = previous_post_link(\'%link\', \'\'.$image_prev.\'\', TRUE);
$next_post = next_post_link(\'%link\',\'\'.$image_next.\'\', TRUE);
$image_prev = get_post_meta( $previous_post->ID, \'image\', true);
$image_next = get_post_meta( $next_post->ID, \'image\', true);
?>
<?php if ( $image_prev != \'\' ) : ?>
<img src="<?php echo $image_prev; ?>" alt="" />
<?php endif; ?>
<?php if ( $image_next != \'\' ) : ?>
<img src="<?php echo $image_next; ?>" alt="" />
<?php endif; ?>
<?php previous_post_link(\'%link\', \'Previous post\', TRUE); ?>
<?php next_post_link(\'%link\', \'Next post\', TRUE); ?>
</div>
最合适的回答,由SO网友:kaiser 整理而成
以下是您尝试执行的操作代码(访问post自定义值):
<?php
global $post;
$prev_post = get_adjacent_post();
$next_post = get_adjacent_post( false, \'\', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
// this should, according to your code above, be the http://example.com/your/img.jpg string
$prev_img_path = get_post_custom_values( \'your_key_name\', $prev_post_id );
$next_img_path = get_post_custom_values( \'your_key_name\', $next_post_id );
$prev_def_path = get_bloginfo(\'stylesheet_directory\').\'/default_next_img_inside_theme_folder.jpg\';
$next_def_path = get_bloginfo(\'stylesheet_directory\').\'/default_prev_img_inside_theme_folder.jpg\';
echo \'<img src="\'.!empty($prev_img_path) ? $prev_img_path : $prev_def_path.\'" alt="Permalink to previous post" />\';
echo \'<img src="\'.!empty($next_def_path) ? $next_def_path : $next_img_path.\'" alt="Permalink to next post" />\';
?>