You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Astra子主题content-blog.php中current_post始终为0的问题求助

解决Astra子主题中博客第一篇文章识别失效的问题

问题根源

你直接调用$wp_query->current_post但未声明全局变量,在模板部件文件(content-blog.php)中,$wp_query并非默认全局可用,导致每次获取的值都是初始的0;另外$marginRightPosition未提前初始化,会引发PHP警告。

修复方案

1. 声明全局$wp_query变量

在获取current_post前先声明全局变量,确保调用的是主查询对象:

global $wp_query;
$postID = $wp_query->current_post;

2. 替代方案:用get_query_var()

如果不想声明全局变量,也可以直接用get_query_var('current_post')获取当前文章索引:

$postID = get_query_var('current_post');

3. 初始化未定义变量

在代码开头把$marginRightPosition也初始化,避免PHP警告:

$marginPosition = '';
$marginRightPosition = ''; // 新增这行初始化代码

完整修复后的代码

<?php
/**
 * Template part for displaying posts.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Astra
 * @since 1.0.0
 */

?>
<?php astra_entry_before(); ?>
<?php
global $wp_query; // 声明全局变量
$postID = $wp_query->current_post;
$marginPosition = '';
$marginRightPosition = ''; // 初始化变量
if ($postID != 0 && !is_paged()) :
    if($postID % 2 == 0 || $postID % 3 == 0) {
        $marginPosition = 'left-margin';
    }
    if($postID % 3 == 0){
        $marginRightPosition = "no-right-margin";
    }
    if($postID % 4 == 0){
        $marginPosition = '';
    }
    if($postID % 5 == 0){
        $marginPosition = 'left-margin';
    }
endif;
// echo $postID; // 测试用,确认值正确后可注释
?>
<article
<?php
        echo astra_attr(
            'article-blog',
            array(
                'id'    => 'post-' . get_the_id(),
                'class' => join( ' ', get_post_class() ).' '.$marginPosition. ' '.$marginRightPosition,
            )
        );
        ?>
>
    <div class="article-holder <?php if( $postID == 0 && !is_paged() ) : ?>firstarticle<?php else: ?>standard-article<?php endif; ?>">

        <?php if( $postID == 0 && !is_paged() ) : ?>
            <div class="left">
                <div class="featured-image">
                    <a href="<?php the_permalink();?>">
                    <?php the_post_thumbnail( 'featured-blog-size' ); ?>
                    </a>
                </div>
            </div>
            <div class="right">
                <div class="title"><h2><a href="<?php the_permalink();?>"><?php echo get_the_title( ); ?></a></h2></div>
                <div class="latest-banner"><span>latest article</span></div>
                <?php echo do_shortcode( '[molongui_author_box]' ); ?>

            </div>
        <?php else: ?>
        <div class="top">
            <div class="standard-image">
                <a href="<?php the_permalink();?>">
                    <?php the_post_thumbnail( 'standard-blog-size' ); ?>
                </a>
            </div>
        </div>
        <div class="bottom">
            <div class="title"><h2><a href="<?php the_permalink();?>"><?php echo get_the_title( ); ?></a></h2></div>
            <div class="written-by-content">
                <div class="right-wb">
                    <span>by <a href="#">Josephine Choo</a>, PhD<br></span>
                    <span>Consultant</span>
                    <div class="date"><?php echo get_the_date( 'F dS Y' ); ?></div>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</article><!-- #post-## -->
<?php astra_entry_after(); ?>

额外验证

  1. 可以先保留echo $postID;,刷新博客首页,查看每个文章下方输出的索引值是否正确(从0开始递增)
  2. 确认分页页面(如/page/2/)不会触发第一篇文章的特殊样式,因为!is_paged()会排除分页页面

内容的提问来源于stack exchange,提问作者Peter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 09:48:09