簡體   English   中英

如何顯示帶有特定標簽的帖子

[英]How to display posts with a certain tag

我真的很難確定自己是一個簡單的問題。 我似乎無法獲得屬於某個標簽的帖子要顯示在該標簽頁面上,即:(/ tag / blog /),博客為Tag。

到目前為止,在Wordpress上的Official Tag Templates頁面之后,我似乎仍然無法正常運行。

我不需要繼承關系,因此tag.php可以正常工作。 使用single_tag_title()它確實可以在頁面頂部正確顯示標簽。

其余的正式標簽模板實際上並未提供更多詳細信息,我是否可以使用默認的Loop或自定義的模板 我嘗試使用如下所示的自定義選項,但這不起作用。 (我已將其降至目前不擔心樣式的最低程度。)

<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
    <div id="content" class="clearfix row">
        <div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
            <?php 
                if ( have_posts() ) :
                    while ( have_posts() ) :
                        the_title();
                    endwhile; // end while
                endif; // end if
            ?>
        </div> <!-- end #main -->
    <?php get_sidebar('blog'); // Blog ?>
    </div> <!-- end #content -->
</div>
<?php get_footer(); ?>

因此,此代碼當前確實顯示了Tag的標題,但不顯示我要發布的帖子的標題。

總結問題 “如何顯示屬於特定標簽的帖子。”

您並沒有告訴循環在任何地方尋找該特定標簽……您要做的只是在頁面頂部顯示當前選定的標簽。 在循環中添加一個PHP if語句,以獲取帶有該標簽的帖子:

<?php get_header(); ?>     // Get the header

<p>Tag: <?php single_tag_title(); ?></p>     // Display the tag name

<div class="container">
    <div id="content" class="clearfix row">
        <div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">


            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // Start your loop

                    <?php if(tag_slug( ' THE SLUG ' ) ) : ?> // Look for the tag
                           // Post HTML stuff
                    <?php endif; ?>

                <?php endwhile; endif ?> // finish the loop

        </div> <!-- end #main -->
    <?php get_sidebar('blog'); // Blog ?>
    </div> <!-- end #content -->
</div>
<?php get_footer(); ?>

食品法典委員會的本頁提供了有關如何獲取類別和標簽的示例。

您只需要the_post()函數,您必須在while內調用它,並且它應該是您調用的第一個函數,因為它會加載模板標簽。

<?php 
                if ( have_posts() ) :
                    while ( have_posts() ) : the_post();

                        the_title();
                    endwhile; // end while
                endif; // end if
            ?>

如果標記僅用於自定義帖子類型,則應在您的functions.php中添加以下內容:

add_action( 'pre_get_posts', 'custom_function' );

function custom_function($query){
  if(! is_admin()){
    if(is_tag()){
        $query->set('post_type', array('your_post_type', 'your_other_post_type', 'post'));
    }
  }

}

我發現由於是自定義帖子類型,因此不得不使用WP Query循環將它們引入。

<?php if ( is_tag() ) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms( $taxonomy, $args );} ?>
            <!-- This gets the tags slug  -->

<?php $query = new WP_Query(array( "post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug   ) ); while ($query->have_posts()) : $query->the_post(); ?>

        <?php the_title(); ?>
<?php endwhile; ?>

暫無
暫無

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

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