簡體   English   中英

Wordpress試圖在循環數組中使用get_title()作為類別名稱

[英]Wordpress Trying to use get_title() in loop array as the category name

在我的wordpress頁面模板上,我試圖在參數數組中使用get_title()進行循環,但是我無法獲取它以返回任何帖子。

這就是我要去的地方;

<?php 
            $args = array( 
                      'category_name' => 'the_title();'
                   );

            $the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

            <div class="entry"><?php the_content(); ?></div>

<?php endwhile; else: ?>

    <p>Sorry, there are no posts to display</p>

<?php endif; ?>

我無法獲取任何要在此頁面上返回的帖子,除非我手動輸入要使用的類別的條目,這並不理想,因為頁面條目將始終與帖子類別條目相同。

我這樣做的邏輯是,我不想為每頁上的每只貓都使用單獨的模板,因為我最終將擁有數百頁的模板。

任何幫助贊賞干杯喬恩

您正在傳遞the_title(); 作為字符串做' ' 同樣, the_title()獲取循環中對象的標題,而不是類別的標題。
您可以執行以下操作:

 global $post; 
 $args = array('category_name' => $post->post_name);

您已通過the_title(); 作為字符串,因此將被視為字符串,而不是函數,因此不會返回標題。 您可以使用全局變量$cat ,因此您的代碼將是

<?php
global $cat;
$args = array('cat' => $cat);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
    <p>Sorry, there are no posts to display</p>
<?php endif; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM