【问题标题】:Get Current Page Title and Id after post loop发布循环后获取当前页面标题和 ID
【发布时间】:2012-03-30 14:23:25
【问题描述】:

我正在尝试获取当前页面 id 和标题以供参考,但在输出它们之前,我会覆盖标题中的全局 $post。

这是我的代码:

<?php 
    global $post;
    $current_page_id = $post->ID;
    echo $current_page_id;
?>

这是我的标题代码:

<div id="slider" class="nivoSlider">
    <?php
        $slides = get_posts( array( 'post_type' => 'slide' ) );
        foreach( $slides as $post ): setup_postdata( $post );
            $slide_url = get_slide_url();
            $slide_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "slide" );
            ?><a href="<?php echo $slide_url; ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo $slide_thumbnail[0]; ?>" /></a><?php
        endforeach;
    ?>
</div>

它返回最后使用的帖子,但我想要我当前的页面帖子。

我为帖子类型产品创建了一个自定义元框,以选择要显示的页面。我正在尝试将产品类型和当前页面 ID 的帖子输出到屏幕上。

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在为此检索帖子 ID 之前使用 'wp_reset_query' 函数:

    <?php 
    wp_reset_query();
    global $post;
    $current_page_id = $post->ID;
     echo $current_page_id;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2011-03-08
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多