【问题标题】:How to include Featured Posts in the loop in Wordpress Twenty Fourteen如何在 Wordpress 24 的循环中包含精选帖子
【发布时间】:2016-10-17 14:27:45
【问题描述】:

我有一个 Wordpress 24 的儿童主题,我希望在主博客区域上方的网格中显示特色帖子,而不是在博客区域中消失。所以应该出现在网格和博客循环中。

我已经搜索了主题的文件,但我没有找到线索,在哪里过滤了精选帖子。

我还试图摆脱不必要的复杂性并为子主题创建了一个新的 index.php:

    <?php
        if ( have_posts() ) :
            // Start the Loop.
            while ( have_posts() ) : the_post();

                the_title( '<h1 class="entry-title">', '</h1>' );

            endwhile;

        else :
            // If no content, include the "No posts found" template.
            get_template_part( 'content', 'none' );

        endif;
    ?>

但即使有特色帖子也不见了。 (它只显示主循环)。

知道在哪里做这个把戏吗?

提前致谢!

24.10 更新: 在最近两个答案都解决不了问题后,我进一步研究了原来的二十四主题,找到了文件inc/featured-content.php,其中的精选内容被过滤掉了。

ll 中的函数 pre_get_posts。第 231 页负责这个问题 - 故意但没有提示,如何过滤。

我假设,由于 inc/featured-content.php 包含一个类,我可以扩展它并 覆盖 pre_get_posts 方法?

但实际上,我不是 php-guru,我看不到原始类在哪里初始化...?有什么想法吗?

后续更新: 在functions.php,ll。 514 ff.,相关类是必需的:

/*
 * Add Featured Content functionality.
 *
 * To overwrite in a plugin, define your own Featured_Content class on or
 * before the 'setup_theme' hook.
 */
if ( ! class_exists( 'Featured_Content' ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
    require get_template_directory() . '/inc/featured-content.php';
}

这似乎是一个简单的覆盖问题?最好不要失去整个类的功能,而只是改变它......

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    试试这个方法..

    <?php
    
        $fp_arg = array(
                        'posts_per_page' => 3,
                        'post_type' => 'post',
                        'meta_key' => 'featured_product', // the name of the custom field
                        'meta_compare' => '=', // the comparison (e.g. equals, does not equal, etc...)
                        'meta_value' => 1, // the value to which the custom field is compared. In my case, 'featured_product' was a true/false checkbox. If you had a custom field called 'color' and wanted to show only those blue items, then the meta_value would be 'blue'
                        );
    
        $future_post = new wp_query($fp_arg);
    
     ?>
    
    <?php if ( $future_post->have_posts() ){
             while ( $future_post->have_posts() ) : $future_post->the_post();
                     the_title( '<h1 class="entry-title">', '</h1>' );
                     if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                     }
    
            endwhile;
    }
     ?>
    
    <?php wp_reset_query(); ?> 
    

    【讨论】:

    • 感谢您的帮助 - 但我还不能解决我的问题。也许您对我更新的问题有任何想法?
    【解决方案2】:

    最后,我找到了一个可行的解决方案(实际上,在我尝试之前,但使用了错误的钩子...... :-/ ):

    在我孩子的functions.php 中,我添加了这些行以从inc/featured-content.php 中的Featured_Content 类中删除过滤功能

    function do_not_filter_featured_posts() {
     remove_action( 'pre_get_posts', array( 'Featured_Content', 'pre_get_posts' ) );
    }
    add_action( 'wp_loaded', 'do_not_filter_featured_posts' );
    

    这增加了一个函数do_not_filter_featured_posts在加载wordpress后被调用,它删除了在父主题的类Featured_Content中添加的函数pre_get_posts钩子pre_get_posts

    瞧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      相关资源
      最近更新 更多