第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將 post_id 添加到 post_title 僅適用于 post_type 為“post”的帖子

將 post_id 添加到 post_title 僅適用于 post_type 為“post”的帖子

PHP
陪伴而非守候 2023-05-12 16:11:12
我需要能夠在帖子標(biāo)題中插入帖子的 ID。在標(biāo)題出現(xiàn)的任何地方都應(yīng)將 id 添加到標(biāo)題中。它只應(yīng)添加到具有帖子類型的帖子中post,而不應(yīng)添加到頁面、自定義帖子類型等中。我已經(jīng)設(shè)法做到這一點(diǎn):function custom1_shortcode_func() {    global $post;    ob_start();    echo get_the_title($post->ID); echo " ("; echo get_the_ID(); echo ")"    $output = ob_get_clean();    return $output;}add_shortcode('post-id', 'custom1_shortcode_func');在帖子中使用 [post-id] 時(shí)返回帖子標(biāo)題和帖子 ID。但是我需要在我的整個(gè)網(wǎng)站上修改帖子標(biāo)題,所以無論什么地方顯示帖子標(biāo)題,它后面都會(huì)跟著“(post_id)”。我試過了,它確實(shí)在帖子標(biāo)題前顯示了 post_id,但它改變了所有標(biāo)題,包括菜單:add_filter('the_title', 'wpshout_filter_example');function wpshout_filter_example($title) {    return get_the_ID().$title;}
查看完整描述

1 回答

?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的第二次嘗試已經(jīng)接近尾聲,但您發(fā)現(xiàn)這對(duì)每個(gè)標(biāo)題都有效。


您需要做的是先檢查帖子類型是否為 a post,如果是則只添加 id。


add_filter('the_title', 'add_id_to_title', 10, 2);

function add_id_to_title($title, $post_id) {

? ? //use the id to check the post type and only add the id if it has a type of "post"

? ? if(get_post_type($post_id) == "post")

? ? ? ? $title = $post_id.': '.$title;

? ? return $title;

}

這是做什么的:

  • 過濾the_title器可以采用 2 個(gè)參數(shù):標(biāo)題(您正在使用的)以及帖子的 ID。

  • 使用 id,可以使用內(nèi)置get_post_type函數(shù)找出帖子類型。

  • 然后我們可以檢查帖子類型是否是post,如果是我們將 id 添加到標(biāo)題。


查看完整回答
反對(duì) 回復(fù) 2023-05-12
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)