亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建一個wordpress函數,以類別ID作為參數,但無法獲取正確的數據

創建一個wordpress函數,以類別ID作為參數,但無法獲取正確的數據

PHP
紅糖糍粑 2023-07-08 20:19:56
我試圖在&ldquo;functions.php&rdquo;中創建一個函數,每次調用它時我只需要告訴我想要哪個類別ID。最終結果應該是這樣的:?Mockup - Posts inside a Carousel如果沒有函數,我可以在&ldquo;front-page.php&rdquo;文件中執行此操作,但如果我開始插入更多類別,代碼將變得過于混亂和重復?,F在這就是我所擁有的(front-page.php):<div id="carouselExampleControls" class="carousel slide" data-ride="carousel"><div class="carousel-inner">? <?php?? ? $args = array(? ? ? 'posts_per_page' => -1,? ? ? 'post_type' => 'post',? ? ? 'orderby' => 'date',? ? ? 'order' => 'ASC',? ? ? 'category__in' => 1515? ? );? ? ? ? ? ? ? ??? ? ? ? ? ? $firstCategory = get_posts( $args );? ? $i = true;? ? foreach (array_chunk($firstCategory, 3, true) as $firstCategory) : ?>? ? ? <div class=" carousel-item <?php echo $i ? ' active' : '' ?>">? ? ? ? <?php foreach( $firstCategory as $post ) : setup_postdata($post); ?>? ? ? ? ? <div class="card col-4">? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? <?php if( has_post_thumbnail() ) :? ?>? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? <div class="card-header">? ? ? ? ? ? ? ? <a href="<?php the_permalink() ?>">? ? ? ? ? ? ? ? ? <?php the_post_thumbnail('thumbnail', array(? ? ? ? ? ? ? ? ? ? 'alt' => trim(strip_tags( $post->post_title )),? ? ? ? ? ? ? ? ? ? 'title' => trim(strip_tags( $post->post_title ))? ? ? ? ? ? ? ? ? )); ?>? ? ? ? ? ? ? ? </a>? ? ? ? ? ? ? </div>? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? <?php endif; ?>? ? ? ? ? ? ? ? <div class="card-body">? ? ? ? ? ? ? ? ? <h3 class="card-title"> <?php the_title(); ?> </h3>? ? ? ? ? ? ? ? ? <p class="card-text"> <?php?? ? ? ? ? ? ? ? ? ? if(has_excerpt()) {? ? ? ? ? ? ? ? ? ? ? echo get_the_excerpt();?? ? ? ? ? ? ? ? ? ? } else {? ? ? ? ? ? ? ? ? ? ? echo wp_trim_words(get_the_content(), 15);? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ?>?? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? </p>? ? ? ? ? ? ? ? ? ? <a href=" <?php the_permalink(); ?>" class="card-link">Ver mais</a>? ? ? ? ? ? ? ? </div>? ? ? ? ? </div>快速解釋一下,此代碼從類別中獲取帖子,并每次在輪播中插入 3 個帖子。當我在&ldquo;functions.php&rdquo;中執行此操作時,我無法檢索所有正確的數據。只有鏈接和標題就可以了。
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

我們可以修復/改進一些事情,但您最緊迫的問題是

  • has_excerpt接受帖子或帖子 ID,更改has_excerpt($post->post_excerpt)has_excerpt($post)

  • trim(strip_tags($post->ID))只是給你 ID,你想要別的東西,也許是 post_title trim(strip_tags($post->post_title))

另外,不要setup_postdata($post)在循環中使用

  • 使用get_the_post_thumbnail($post, 'thumbnail', $args)而不是the_post_thumbnail('thumbnail', $args)

  • 并使用get_the_excerpt($post)而不是the_excerpt()

注意:默認屬性為get_the_post_thumbnail

$default_attr = [

    'src'   => $src,

    'class' => "attachment-$size_class size-$size_class",

    'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),

];

簡而言之,您的主要問題是您混淆了全局 $post 和本地范圍的 $post (及其元數據)。the_post_thumbnail并 the_excerpt檢查全局 $post,但has_post_thumbnail使用has_excerpt本地 $post。Plussetup_postdata($post)也會導致一些連鎖問題。最好的解決方案是完全忽略全局 $post。



查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號