我的 WordPress 網(wǎng)站上有三種類型的帖子(標(biāo)準(zhǔn)、旁白、視頻)。我需要像$row標(biāo)準(zhǔn)content.php文件中那樣計(jì)算變量,但我無法將其傳遞到content.php文件。我的index.php文件代碼:<?php if (have_posts()) : $$row = 2; while (have_posts()) : the_post(); get_template_part('content', get_post_format()); $row++; endwhile; endif; ?>我的content.php文件代碼:<div> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php the_content(); ?>- <?php echo $row; ?></p></div>但當(dāng)我echo $row;沒有任何表現(xiàn)時(shí)我已經(jīng)使用了stackoverflow 上類似問題的鏈接,但我無法更改我的帖子格式文件,還有其他方法可以做到這一點(diǎn)嗎?
1 回答

開心每一天1111
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
無法準(zhǔn)確地傳遞參數(shù),但您可以做的是使用函數(shù)set_query_var
,get_query_var
這使得變量可以全局訪問。
在主模板中,例如index.php,您可以用來set_query_var
設(shè)置變量。請注意,第一個(gè)參數(shù)是全局變量的名稱,這是您用來檢索它的名稱 - 而不是原始參數(shù)名稱。
set_query_var( "my_global_var_name", $param_vaule); get_template_part('content', get_post_format());
然后在模板部分,您可以使用 獲取 now-global 變量的值get_query_var
,例如
$myvar = set_query_var( "my_global_var_name" );
(值得注意的是,它的最佳實(shí)踐是最小化全局變量,因此您可能會更仔細(xì)地考慮重構(gòu)模板結(jié)構(gòu),看看是否有其他方法可以做到這一點(diǎn),例如使用額外的模板部分。)
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)
0/150
提交
取消