wordpress获取文章分类-WordPress建站教程:如何设置文章置顶效果?(超详细)

继续分享WordPress建站教程。 在之前的WordPress建站项目中,遇到了将文章置顶的需求。 虽然WordPress默认有置顶文章,但不同的主题和插件会有不同的显示效果,并且某些主题可能会屏蔽置顶文章。 因此,置顶功能在实际的wordpress建站项目中经常困扰很多用户。要么置顶不起作用,要么置顶效果不明显。

如上图所示,如果想将置顶的文章显示在最前面,将置顶的标签显示在网站的首页或分类上,如何实现呢? 接下来悦然就给大家分享一下如何搭建WordPress网站。

步骤一:让置顶的文章出现在最前面

首先,我们需要使固定的文章出现在前面。 你可以先检查一下你的主题是否已经有这样的效果。 如果已经存在,则无需阅读此步骤。 如果没有wordpress获取文章分类,则继续执行以下操作。

让置顶文章出现在最前面的方式主要有两种,一种是插件,一种是代码。

使用插件

这里给大家分享两个插件。 第一个是置顶帖 – Switch。 插件安装后,您可以设置置顶文章的显示位置,控制置顶文章在首页、分类页选项卡、自定义文章类型页面的显示。 但是,该插件默认将所有固定的文章显示在前面。 比如B类中有一篇置顶文章,那么B类中的置顶文章也会显示在A类中。如果你觉得这样还可以,那么这个插件的效果是最稳定的。 是的,它也强调简单性。

置顶帖 – 切换

另一个插件是 Category Sticky Post。 这是一个古老的插件,但仍然可用。 可以设置置顶文章在文章编辑页面的显示位置。 例如,如果您只想在A类别中显示A类别的热门文章,那么您可以通过此插件进行设置。 不过该插件仅支持一级分类,子类别可能无法生效。

wordpress分类筛选_wordpress获取文章分类_wordpress分类信息

类别置顶帖

使用代码

您可以直接将以下代码添加到当前WordPress网站主题的functions.php文件中并保存。 它将自动显示当前类别中该类别的热门文章。 [代码来自Pandan即时博客]

//sxxfx.comadd_filter('the_posts',  'putStickyOnTop' );
function putStickyOnTop( $posts ) {
if(is_home() || !is_main_query() || !is_archive())
return $posts;

global $wp_query;

// 获取所有置顶文章
$sticky_posts = get_option('sticky_posts');

if ( $wp_query->query_vars['paged'] <= 1 && !empty($sticky_posts) && is_array($sticky_posts) && !get_query_var('ignore_sticky_posts') ) {
$stickies1 = get_posts( array( 'post__in' => $sticky_posts ) );
foreach ( $stickies1 as $sticky_post1 ) {
// 判断当前是否分类页
if($wp_query->is_category == 1 && !has_category($wp_query->query_vars['cat'], $sticky_post1->ID)) {
// 去除不属于本分类的置顶文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_tag == 1 && !has_tag($wp_query->query_vars['tag'], $sticky_post1->ID)) {
// 去除不属于本标签的文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_year == 1 && date_i18n('Y', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
// 去除不属于本年份的文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_month == 1 && date_i18n('Ym', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
// 去除不属于本月份的文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_day == 1 && date_i18n('Ymd', strtotime($sticky_post1->post_date))!=$wp_query->query['m']) {
// 去除不属于本日期的文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars['author']) {
// 去除不属于本作者的文章
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
}

$num_posts = count($posts);
$sticky_offset = 0;
// Loop over posts and relocate stickies to the front.
for ( $i = 0; $i < $num_posts; $i++ ) {
if ( in_array($posts[$i]->ID, $sticky_posts) ) {
$sticky_post = $posts[$i];
// Remove sticky from current position
array_splice($posts, $i, 1);
// Move to front, after other stickies
array_splice($posts, $sticky_offset, 0, array($sticky_post));
// Increment the sticky offset. The next sticky will be placed at this offset.
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search($sticky_post->ID, $sticky_posts);
unset( $sticky_posts[$offset] );
}
}

// If any posts have been excluded specifically, Ignore those that are sticky.
if ( !empty($sticky_posts) && !empty($wp_query->query_vars['post__not_in'] ) )
$sticky_posts = array_diff($sticky_posts, $wp_query->query_vars['post__not_in']);

// Fetch sticky posts that weren't in the query results
if ( !empty($sticky_posts) ) {
$stickies = get_posts( array(
'post__in' => $sticky_posts,
'post_type' => $wp_query->query_vars['post_type'],
'post_status' => 'publish',
'nopaging' => true
) );

foreach ( $stickies as $sticky_post ) {
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
$sticky_offset++;
}
}
}

return $posts;
}

wordpress分类信息_wordpress分类筛选_wordpress获取文章分类

只需选择上述方法中的任何一种即可。 如果您发现它不起作用,可能是您的主题的模板文件阻止了固定的文章。 您可以使用文件编辑器检查主题的列表模板或首页模板是否有以下代码或类似代码,然后将其删除并尝试。

'ignore_sticky_posts' => 1,

第 2 步:添加粘性标签

设置好置顶文章后,我们需要给置顶文章添加一个更显眼的标记,比如上图2的样式,就是置顶文字+背景色。 接下来,开始吧。

修改模板文件

wordpress分类信息_wordpress获取文章分类_wordpress分类筛选

找到需要显示固定标记的主题模板文件,通常是类别、列表页模板或首页模块模板。 找到后,用文本编辑器打开,然后找到[the_title]这样的代码。

然后在[the_title]段落之前或之后添加以下代码(文本内容可以修改)。 不同的位置可能会添加不同的主题,因此您可能需要尝试更多,直到可以在标题前后显示[Top]。

<?php if( is_sticky() ) echo '置顶'; ?>

现在仅仅显示固定文本还不太明显,所以接下来我们使用 CSS 为固定文本添加背景。 代码参考如下:


.sticky_sign

{
color: #fff;
background: #ff5722;
padding: 3px;
}

最终效果如上图所示。

悦然WordPress网站建设()原创文章,转载时请保留出处和链接。

总结

以上就是我今天分享的内容。 文章固定一般用于博客资源型网站。 不过wordpress获取文章分类,这类网站都有专门的主题,所以你也可以直接使用这类主题来建立网站。 一般pinning功能已经开发出来了,我们就不需要再添加了。 扔了。