首页 > 解决方案 > 仅在主页上显示来自特定类别的最近帖子

问题描述

我想显示特定类别的 3 个最新帖子,并且效果很好。但是只要我想限制只在主页上显示,我的功能就不再起作用了。

add_action( 'um_theme_before_content', 'extranet_header' );

function extranet_header() {
    // Get the posts form the "Communiques" category
    $the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
    
    if ( $the_query->have_posts() ) {
        // Render posts link and title in an ordered list
        echo '<div id="communiques">'.
             '<h4>Les derniers communiqués</h4>'.
             '<ul class="administration-notice">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>'.
                 '<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
                 '<p>'.get_the_excerpt().'</p>'.
                 '<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
                 '</li>';
        }
        echo '</ul>' .
             '<div class="communiques-footer">'.
             '<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
             '</div><!--/.communiques-footer-->'.
             '</div><!--/#communiques-->';
    } else { // No post found
        echo 'Aucun communiqué';
    }
    //  Reset the original main query
    wp_reset_postdata();
} 

当我添加 is_home() 函数的结果不返回任何帖子:

add_action( 'um_theme_before_content', 'extranet_header' );

function extranet_header() {
    // Get the posts form the "Communiques" category
    $the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
    
    if ( $the_query->have_posts() && $the_query->is_home() ) {
        // Render posts link and title in an ordered list
        echo '<div id="communiques">'.
             '<h4>Les derniers communiqués</h4>'.
             '<ul class="administration-notice">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>'.
                 '<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
                 '<p>'.get_the_excerpt().'</p>'.
                 '<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
                 '</li>';
        }
        echo '</ul>' .
             '<div class="communiques-footer">'.
             '<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
             '</div><!--/.communiques-footer-->'.
             '</div><!--/#communiques-->';
    } else { // No post found
        echo 'Aucun communiqué';
    }
    //  Reset the original main query
    wp_reset_postdata();
} 

标签: wordpress

解决方案


您可以尝试将查询包装在有条件的首页,而不是运行查询并查看它是否是“家”。这样查询不会运行,除非它是主页而不是每次运行它然后检查页面。

另外,is_home()指的是帖子页面,而不是您可能认为的“主页”页面,因此您的设置可能会错位。检查设置->阅读。

试试这个:

add_action( 'um_theme_before_content', 'extranet_header' );

function extranet_header() {

    // Check for the page that is set as the "Static home page" in settings.
    if ( is_front_page() ) {

    // Get the posts form the "Communiques" category
    $the_query = new WP_Query( 'category_name=communiques&posts_per_page=3' );
    
    if ( $the_query->have_posts()) {
        // Render posts link and title in an ordered list
        echo '<div id="communiques">'.
             '<h4>Les derniers communiqués</h4>'.
             '<ul class="administration-notice">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>'.
                 '<h4><a href="'.get_permalink().'" title="Cliquez pour en savoir plus...">'.get_the_title().'</a></h4>'.
                 '<p>'.get_the_excerpt().'</p>'.
                 '<a class="admin-notice-button" href="'.get_permalink().'" title="Cliquez pour en savoir plus...">Lire la suite</a>'.
                 '</li>';
        }
        echo '</ul>' .
             '<div class="communiques-footer">'.
             '<a class="communiques-button" href="/communiques/">Voir les anciens communiqués</a>'.
             '</div><!--/.communiques-footer-->'.
             '</div><!--/#communiques-->';
    } else { // No post found
        echo 'Aucun communiqué';
    }
    //  Reset the original main query
    wp_reset_postdata();
    
    }

} 

推荐阅读