小工具      在线工具  汉语词典  dos游戏  css  js  c++  java

如何在WordPress自定义帖子类型档案中添加粘性帖子

php,java,python,wordpress,laravel 额外说明

收录于:17天前

Recently one of our users asked us if it was possible to add sticky posts to custom post type archives. By default, WordPress has the sticky functionality available for posts, but not for other post types. In this article we will show you how to add sticky posts in WordPress custom post type archives. Before we move forward, you would probably want to learn how to 创建自定义帖子类型 in WordPress.

最近,我们的一位用户问我们是否可以在自定义帖子类型档案中添加粘性帖子。 默认情况下,WordPress具有可用于帖子的粘性功能,但不适用于其他帖子类型。 在本文中,我们将向您展示如何在WordPress自定义帖子类型档案中添加粘性帖子。 在继续进行之前,您可能需要学习如何在WordPress中创建自定义帖子类型

在自定义帖子类型中添加粘性帖子 Adding Sticky Posts in Custom Post Types

First thing you need to do is install and activate the 粘性自定义帖子类型 plugin. After activating the plugin, go to 设置 » 阅读 and scroll down to the section Sticky Custom Post Types. Next, you need to choose the custom post types where you want Stick This option to be enabled.

您需要做的第一件事是安装并激活粘性自定义帖子类型插件。 激活插件后,转到“设置”»“阅读”,然后向下滚动至“粘性自定义帖子类型”部分。 接下来,您需要选择要启用此选项的自定义帖子类型。

Enabling sticky posts for custom post types

现在我们在这里所做的是,我们向自定义帖子类型添加了粘性帖子功能。自定义帖子类型中的置顶帖子将像常规置顶帖子一样显示在首页上。

现在,我们在这里所做的是将粘性帖子功能添加到自定义帖子类型中。与常规卡片一样,自定义帖子类型的卡片也会显示在主页上。

问题是默认情况下 WordPress 仅在主页上显示置顶帖子。它不会在存档页面上显示置顶帖子。

问题是,默认情况下,WordPress 仅在主页上显示置顶帖子。它不会在存档页面上显示置顶帖子。

在自定义帖子类型档案中显示粘性帖子 Displaying Sticky Posts in Custom Post Type Archives

让我们假设您有一个电影评论的自定义帖子类型,并使用我们上面提到的插件启用了粘性帖子。现在,您希望电影评论帖子类型中的粘性帖子以不同的方式显示在非粘性常规电影评论之上。像这样:

假设您有一个“电影评论”的自定义帖子类型,并使用我们上面提到的插件启用了粘性帖子。现在,您希望电影评论帖子类型中的粘性帖子以不同的方式显示在非粘性常规电影评论之上。像这样:

Showing a sticky post on a custom post type archive page

To achieve this goal, first thing you need is an archive template for your custom post type like this: archive-post-type.php. Learn how to 创建自定义帖子类型存档页面. For example, if you have a custom post type movie-reviews then your archive page template should be archive-movie-reviews.php. If you do not have a template, create one. Simply copy the contents of archive.php in your theme’s directory and paste them into a new file archive-your-post-type.php.

为了实现此目标,您需要的第一件事是为您的自定义帖子类型提供一个存档模板,例如: archive-post-type.php 。 了解如何创建自定义帖子类型存档页面 。 例如,如果您有一个自定义帖子类型movie-reviews那么您的存档页面模板应该是archive-movie-reviews.php 。 如果您没有模板,请创建一个。 只需将archive.php的内容复制到主题目录中,然后将其粘贴到新文件archive-your-post-type.php

The next step is to add this code in your theme’s functions.php file:

下一步是将此代码添加到主题的functions.php文件中:


function wpb_cpt_sticky_at_top( $posts ) {
 
    // apply it on the archives only
    if ( is_main_query() && is_post_type_archive() ) {
        global $wp_query;
 
        $sticky_posts = get_option( 'sticky_posts' );
        $num_posts = count( $posts );
        $sticky_offset = 0;
 
        // Find the sticky posts
        for ($i = 0; $i < $num_posts; $i++) {
 
            // Put sticky posts at the top of the posts array
            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) );
                $sticky_offset++;
 
                // Remove post from sticky posts array
                $offset = array_search($sticky_post->ID, $sticky_posts);
                unset( $sticky_posts[$offset] );
            }
        }
 
        // Look for more sticky posts if needed
        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;
}
 
add_filter( 'the_posts', 'wpb_cpt_sticky_at_top' );

// Add sticky class in article title to style sticky posts differently

function cpt_sticky_class($classes) {
			if ( is_sticky() ) : 
			$classes[] = 'sticky';
	        return $classes;
		endif; 
		return $classes;
				}
	add_filter('post_class', 'cpt_sticky_class');


The above code would move your sticky posts to the top, and if your theme is using post_class() function, then it would add sticky in the post class.

上面的代码会将您的粘性帖子移到顶部,如果您的主题使用的是post_class()函数,那么它将在post类中添加粘性。

You can style your sticky posts by using .sticky class in your stylesheet. Example:

您可以通过在样式表中使用.sticky类来.sticky粘性帖子的样式。 例:


.sticky { 
background-color:#ededed;
background-image:url('http://example.com/wp-content/uploads/featured.png');
background-repeat:no-repeat;
background-position:right top;
}

Styling sticky posts

我们希望本文可以帮助您在自定义帖子类型存档中添加置顶帖子。如有问题和反馈,请在下面发表评论。

我们希望本文可以帮助您将置顶帖子添加到自定义帖子类型存档中。如有问题和反馈,请在下面发表评论。

Source: 塔里克·哈桑

资料来源: 塔里克·哈桑

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-sticky-posts-in-custom-post-type-archives/

. . .

相关推荐

额外说明

解决Vue中IOS端微信内置浏览器底部前进后退栏问题

对于急性子先讲一下解决方案 this.$router.replace('/path') 这样写完。你就会发现跳转后底部不会出现那个该死的白条了 // 后面讲一下原理 讲一下。微信为什么会出现这样的问题 微信内置浏览器自己会监听他会产生历史记录。一开始。我

额外说明

IDEA中SVN的使用(提交,同步)

本人新手,不足之处,请谅解。 有不足之处,欢迎提出。 下面我来大家介绍下在idea中使用svn的步骤: 这样就打开了svn的应用 点击加号 输入您的svn的地址 我来给大家简单说下控制人的功能: 此处可以看到同事们提交的代码内容,时间,提交人等信息。 这

额外说明

Python 第七节 第四课

[toc] 面向对象编程     面向对象 ( Object oriented Programming, OOP ) 编程的思想主要是针对大型软件设计而来的. 面向对象编程使程序的扩展性更强, 可读性更好, 使的编程可以像搭积木一样简单     面向对象

额外说明

mybais逆向工程快速生成实体和基本xml

在ssm或者使用mybatis的项目中,如果有大量的表需要建设实体到数据库的表映射关系,在程序中一个个手动创建对象是很费时间的事情,mybatis提供了一个很好用的插件可以帮助我们快速生成基本的实体对象到数据库的表映射以及xml文件,下面就看看具体的配置

额外说明

KMeans算法全面解析与应用案例

目录 一、聚类与KMeans介绍 聚类的基础概念 KMeans算法的重要性 二、KMeans算法原理 数据集和特征空间 距离度量 算法步骤 三、KMeans案例实战 案例背景:客户细分 数据集说明 Python实现代码 输出与解释 四、KMeans的优缺

额外说明

win10 --docker

关于 docker desktop 闪退问题: https://www.cnblogs.com/zhengyuanyuan/p/14412651.html 提示下载 wsl2 Linux kernel; https://learn.microsoft.c

额外说明

Java学习笔记2.1.3 Java基本语法 - Java关键字与标识符

文章目录 零、本讲学习目标 一、Java关键字 (一)关键字概念 (二)JDK8关键字 二、Java标识符 (一)标识符定义 (二)标识符构成规则 1、规则说明 2、案例演示 (1)合法标识符示例 (2)非法标识符示例 (三)Java标识符命名规范 1、

额外说明

无心剑英译北岛诗9首

文章目录 1、回答 2、冷酷的希望 3、一切 4、纪念日 5、迷途 6、那时我们有梦 7、无题 8、宣告 9、结局或开始——献给遇罗克 1、回答 回答 Reply 北岛 By Bei Dao 卑鄙是卑鄙者的通行证, 高尚是高尚者的墓志铭, 看吧,在那镀金

额外说明

用Golang/Go搭建大型电商系统,要用到前后台的技术和框架

学习目标: 了解使用Golang搭建大型电商系统所需的前后台技术和框架 了解事务的最终一致性和事务并发性 了解微服务 对比各个框架和第三方开源框架,给出具体的建议 学习内容: 1. 什么是微服务? 微服务是一种软件架构风格,它将一个大型的应用程序拆分为一

额外说明

Windows系统因缺少ntdsapi.dll文件导致系统异常的问题

其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个ntdsapi.dll文件(

ads via 小工具