我有一个Wordpress 214的子主题,我想有特色的帖子,显示在主博客区域上方的网格中,而不是消失在博客区域。所以应该出现在网格和博客循环中。
我已经搜索了主题的文件,但是我没有得到线索,特色文章被过滤了。
我还试图摆脱不必要的复杂性,并为子主题创建了一个新的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。更新:在最近的两个答案都不能解决我的问题后,我进一步调查了原来的214主题,找到了文件inc/featured-content.php,其中的特色内容被过滤了。
函数pre_get_posts。231 ff。负责这个问题-故意的,但没有提示,如何不过滤器。
我假设,因为inc/featured-content.php包含一个类,我可以扩展它并覆盖pre_get_posts-方法?
但实际上,我不是php专家,我不知道,原始类在哪里初始化…?什么好主意吗?
后续更新:在functions.php中,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';
}
这似乎是一个问题,简单地覆盖它?如果不丢失整个类的功能,而只是更改它,那将是很好的……
试试这个…
<?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(); ?>
最后,我找到了一个有效的解决方案(实际上,在我尝试之前,但使用了错误的钩子…: -/):
在我孩子的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加载后调用,它删除了在钩子pre_get_posts
上父主题的类Featured_Content
中添加的函数pre_get_posts
。
瞧!