如何为 Next & Previous post 编写 PHP 条件?
How to write PHP condition for Next & Previous post?
不知道这种情况下if条件语句怎么写?需要一些帮助。如果他们没有任何下一个或上一个帖子可显示,我想完全隐藏 div?并用另一个可用的 div 覆盖剩余的 space。希望这是有道理的。请在下面查看我的最终代码。
<div id="post-navigation">
<?php
$prev_post = get_previous_post();
$prev_id = $prev_post->ID ;
$prev_permalink = get_permalink( $prev_id );
$prev_thumbnail = get_the_post_thumbnail_url( $prev_id );
$prev_image_alt = get_post_meta($prev_thumbnail, '_wp_attachment_image_alt', true);
$next_post = get_next_post();
$next_id = $next_post->ID ;
$next_permalink = get_permalink($next_id);
$next_thumbnail = get_the_post_thumbnail_url( $next_id );
$next_image_alt = get_post_meta($next_thumbnail, '_wp_attachment_image_alt', true);
?>
<article class="pexel-previous-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo $prev_permalink; ?>">
<img src="<?php echo $prev_thumbnail; ?>" alt="<?php echo $prev_image_alt; ?>" width="200" height="200">
</a>
</div>
<header class="pexel-previous-post-text">
<div class="entry-meta before-title prev-next-pill">
<span><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?>
</span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline"><a href="<?php echo $prev_permalink; ?>"><?php echo $prev_post->post_title; $short_title; ?></a></h5>
</header>
</article>
<article class="pexel-next-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo $next_permalink; ?>">
<img src="<?php echo $next_thumbnail; ?>" alt="<?php echo $next_image_alt; ?>" width="200" height="200">
</a>
</div>
<header class="pexel-next-post-text">
<div class="entry-meta before-title next-next-pill">
<span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
</span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline"><a href="<?php echo $next_permalink; ?>"><?php echo $next_post->post_title; ?></a></h5>
</header>
</article>
</div>
这里有一个例子,如果没有下一个post,完全隐藏post-navigation
div。你应该能够从这个例子中找出你想要的其余部分。
这使用了 is_a
函数,因为 get_next_post
要么是 returns 一个空字符串,null,要么是一个 post object
所以如果你检查这个值是否是一个WP_POST
对象,您可以根据需要显示或隐藏 div。永远记得也要转义你的输出。
<?php
$next_post = get_next_post();
// check to see if $next_post is something or nothing.
if ( is_a( $next_post, 'WP_POST' ) ) :
$next_id = $next_post->ID;
$next_permalink = get_permalink( $next_id );
$next_thumbnail = get_the_post_thumbnail_url( $next_id );
$next_image_alt = get_post_meta( $next_thumbnail, '_wp_attachment_image_alt', true );
?>
<div id="post-navigation">
<article class="pexel-next-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo esc_url( $next_permalink ); ?>">
<img src="<?php echo esc_url( $next_thumbnail ); ?>" alt="<?php echo esc_attr( $next_image_alt ); ?>" width="200" height="200">
</a>
</div>
<header class="pexel-next-post-text">
<div class="entry-meta before-title next-next-pill">
<span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline">
<a href="<?php echo esc_url( $next_permalink ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
</h5>
</header>
</article>
</div>
<?php endif; ?>
不知道这种情况下if条件语句怎么写?需要一些帮助。如果他们没有任何下一个或上一个帖子可显示,我想完全隐藏 div?并用另一个可用的 div 覆盖剩余的 space。希望这是有道理的。请在下面查看我的最终代码。
<div id="post-navigation">
<?php
$prev_post = get_previous_post();
$prev_id = $prev_post->ID ;
$prev_permalink = get_permalink( $prev_id );
$prev_thumbnail = get_the_post_thumbnail_url( $prev_id );
$prev_image_alt = get_post_meta($prev_thumbnail, '_wp_attachment_image_alt', true);
$next_post = get_next_post();
$next_id = $next_post->ID ;
$next_permalink = get_permalink($next_id);
$next_thumbnail = get_the_post_thumbnail_url( $next_id );
$next_image_alt = get_post_meta($next_thumbnail, '_wp_attachment_image_alt', true);
?>
<article class="pexel-previous-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo $prev_permalink; ?>">
<img src="<?php echo $prev_thumbnail; ?>" alt="<?php echo $prev_image_alt; ?>" width="200" height="200">
</a>
</div>
<header class="pexel-previous-post-text">
<div class="entry-meta before-title prev-next-pill">
<span><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?>
</span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline"><a href="<?php echo $prev_permalink; ?>"><?php echo $prev_post->post_title; $short_title; ?></a></h5>
</header>
</article>
<article class="pexel-next-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo $next_permalink; ?>">
<img src="<?php echo $next_thumbnail; ?>" alt="<?php echo $next_image_alt; ?>" width="200" height="200">
</a>
</div>
<header class="pexel-next-post-text">
<div class="entry-meta before-title next-next-pill">
<span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
</span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline"><a href="<?php echo $next_permalink; ?>"><?php echo $next_post->post_title; ?></a></h5>
</header>
</article>
</div>
这里有一个例子,如果没有下一个post,完全隐藏post-navigation
div。你应该能够从这个例子中找出你想要的其余部分。
这使用了 is_a
函数,因为 get_next_post
要么是 returns 一个空字符串,null,要么是一个 post object
所以如果你检查这个值是否是一个WP_POST
对象,您可以根据需要显示或隐藏 div。永远记得也要转义你的输出。
<?php
$next_post = get_next_post();
// check to see if $next_post is something or nothing.
if ( is_a( $next_post, 'WP_POST' ) ) :
$next_id = $next_post->ID;
$next_permalink = get_permalink( $next_id );
$next_thumbnail = get_the_post_thumbnail_url( $next_id );
$next_image_alt = get_post_meta( $next_thumbnail, '_wp_attachment_image_alt', true );
?>
<div id="post-navigation">
<article class="pexel-next-post-data has-post-thumbnail pexel-box">
<div class="pexel-nextPrev-thumbnail">
<a href="<?php echo esc_url( $next_permalink ); ?>">
<img src="<?php echo esc_url( $next_thumbnail ); ?>" alt="<?php echo esc_attr( $next_image_alt ); ?>" width="200" height="200">
</a>
</div>
<header class="pexel-next-post-text">
<div class="entry-meta before-title next-next-pill">
<span><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></span>
</div>
<h5 class="pxl_head--h5 pxl_heading_anim_underline">
<a href="<?php echo esc_url( $next_permalink ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
</h5>
</header>
</article>
</div>
<?php endif; ?>