$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Get current post id witout passing in shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Get current post id witout passing in shortcode

matteradmin8PV0评论

Is that possible to get current post ID without passing in shortcode as parameter. Like [related-post] I want to get the post ID in which post this shortcode will use.

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;
}
}

Is that possible to get current post ID without passing in shortcode as parameter. Like [related-post] I want to get the post ID in which post this shortcode will use.

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;
}
}
Share Improve this question edited Dec 2, 2015 at 9:22 Sisir 7,8718 gold badges61 silver badges99 bronze badges asked Dec 2, 2015 at 9:16 Gorakh ShresthaGorakh Shrestha 1011 gold badge1 silver badge6 bronze badges 2
  • 1 What does get_the_ID() returns? You can always find current post ID on global $post_id – Sisir Commented Dec 2, 2015 at 9:22
  • 2 get_the_ID() return nothing here but global $post; $post_id = $post->ID; work for me. – Gorakh Shrestha Commented Dec 2, 2015 at 11:41
Add a comment  | 

2 Answers 2

Reset to default 6
add_shortcode( 'related-article', 'related_article_title' );
function related_article_title( $atts ) {
    global $post;
    echo $post->ID; // currently viewing 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;
}
}

Just as @gorakh-shrestha was trying, using global $post and then $post->ID, is a good way to proceed, even inside a shortcode

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far