是否可以在不传入短代码作为参数的情况下获取当前帖子ID。像[相关帖子]一样,我想获取此短代码将在其中使用的帖子ID。
add_shortcode( \'related-article\', \'related_article_title\' );
function related_article_title( $atts ) {
$post_id = get_the_ID();
echo $post_id;
ob_start();
$query = new WP_Query( array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'order\' => \'DESC\',
)
);
if ( $query->have_posts() ) { ?>
<div class="menu-row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
Leggi anche: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}