由于许多不同的原因,我被迫停用了 WordPress 的置顶帖功能。我仍然需要这个功能。这意味着我需要一个解决方法。我需要在提要的顶部显示一个帖子,并且我需要为撰写帖子的用户尽可能轻松地制作它。
注意:我正在使用 Visual Composer。
我认为解决此问题的方法是通过 Visual Composer 或侧边栏添加一个新容器并调用一个类别。这个新的侧边栏/容器只有在该类别中有任何帖子时才可见。我一直在寻找功能、查询、插件等来做到这一点,但没有成功。
由于许多不同的原因,我被迫停用了 WordPress 的置顶帖功能。我仍然需要这个功能。这意味着我需要一个解决方法。我需要在提要的顶部显示一个帖子,并且我需要为撰写帖子的用户尽可能轻松地制作它。
注意:我正在使用 Visual Composer。
我认为解决此问题的方法是通过 Visual Composer 或侧边栏添加一个新容器并调用一个类别。这个新的侧边栏/容器只有在该类别中有任何帖子时才可见。我一直在寻找功能、查询、插件等来做到这一点,但没有成功。
只有当它有帖子时,Hook withget_terms
才会显示术语/类别
在 WP 主题中添加此代码functions.php
例如(domain.com/wp-content/themes/yourThemeName/functions.php)
add_filter('get_terms', 'get_terms_filter', 10, 3);
function get_terms_filter( $terms, $taxonomies, $args )
{
global $wpdb;
$taxonomy = $taxonomies[0];
if ( ! is_array($terms) && count($terms) < 1 )
return $terms;
$filtered_terms = array();
foreach ( $terms as $term )
{
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status = 'publish' LIMIT 1");
if ( intval($result) > 0 )
$filtered_terms[] = $term;
}
return $filtered_terms;
}
对于在主查询中设置ignore_sticky_posts
为的前端忽略粘性帖子true
add_action('pre_get_posts', '_ignore_sticky');
function _ignore_sticky($query)
{
// Only for Front end
if (!is_admin() && $query->is_main_query())
$query->set('ignore_sticky_posts', true);
}