WordPress自定义字段101:技巧,窍门和技巧

Custom fields are a handy WordPress feature that allows you to add various additional data / information to your WordPress posts and pages.

自定义字段是一种便捷的WordPress功能,可让您向WordPress帖子和页面添加各种其他数据/信息。

A lot of popular WordPress plugins and themes use custom fields to store important data. You can also use custom fields to store your own data and then use it on your website.

许多流行的WordPress插件和主题都使用自定义字段来存储重要数据。 您还可以使用自定义字段存储自己的数据,然后在您的网站上使用它。

In this article, we’ll show you how to use WordPress custom fields with some tips, tricks, and hacks.

在本文中,我们将向您展示如何使用WordPress自定义字段以及一些提示,技巧和技巧。

Using custom fields in WordPress with practical examples

Since this is a lengthy article, we have added a table of contents for easier navigation.

由于这是一篇冗长的文章,因此我们添加了一个目录,以便于导航。

什么是WordPress自定义字段? (What are WordPress Custom Fields?)

WordPress custom fields are metadata that are used to add additional information related to the post or page that you are editing.

WordPress自定义字段是元数据,用于添加与您正在编辑的帖子或页面相关的其他信息。

By default, when you write a new post, page, or any content type, WordPress saves it into two different areas.

默认情况下,当您编写新的帖子,页面或任何内容类型时,WordPress会将其保存到两个不同的区域。

The first part is the body of your content that you add using the post editor.

第一部分是使用帖子编辑器添加的内容主体。

The second part is the information about that particular content. For example, title, author, date, time, and more. This information bit of the post is called metadata.

第二部分是有关该特定内容的信息。 例如,标题,作者,日期,时间等。 帖子的此信息位称为元数据。

WordPress automatically adds all the required metadata to each post or page you create.

WordPress自动将所有必需的元数据添加到您创建的每个帖子或页面中。

You can also create and store your own metadata by using the custom fields.

您还可以通过使用自定义字段来创建和存储自己的元数据。

By default, the custom fields option is hidden on the post edit screen. To view it, you need to click on the three-dot menu at the top-right corner of the screen and select ‘Options’ from the menu.

默认情况下,自定义字段选项在帖子编辑屏幕上隐藏。 要查看它,您需要单击屏幕右上角的三点菜单,然后从菜单中选择“选项”。

Post editor options

This will bring up a popup where you need to check the ‘Custom fields’ option under the Advanced Panels. After that, click on the ‘Enable & Reload’ button to reload the post editor.

这将弹出一个弹出窗口,您需要在其中检查“高级面板”下的“自定义字段”选项。 之后,点击“启用并重新加载”按钮以重新加载帖子编辑器。

Enable and display custom fields panel

The post editor will reload, and you’ll be able to see the custom fields panel below the content editor.

帖子编辑器将重新加载,您将能够看到内容编辑器下方的“自定义字段”面板。

Custom fields metabox below the post editor

Custom fields can be used to add any information related to the post, page, or any content type. This meta-information can be displayed in your theme.

自定义字段可用于添加与帖子,页面或任何内容类型有关的任何信息。 可以在您的主题中显示此元信息。

However, to do that you will need to edit your WordPress theme files.

但是,为此,您将需要编辑WordPress主题文件。

This is why this tutorial is recommended for users familiar with editing theme files. It is also helpful for aspiring WordPress developers who want to learn how to properly use custom fields in their own themes or plugins.

这就是为什么向熟悉主题文件编辑的用户推荐本教程的原因。 对于想学习如何在自己的主题或插件中正确使用自定义字段的有抱负的WordPress开发人员,它也很有帮助。

Having said that, let’s take a look at how to add and use custom fields in WordPress.

话虽如此,让我们看一下如何在WordPress中添加和使用自定义字段。

在WordPress中添加自定义字段 (Adding Custom Fields in WordPress)

First, you need to edit the post or page where you want to add the custom field and go to the custom fields meta box.

首先,您需要编辑要在其中添加自定义字段的帖子或页面,然后转到“自定义字段”元框。

Adding custom field name and value

Next, you need to provide a name for your custom field and then enter its value. Click on the Add Custom Field button to save it.

接下来,您需要为自定义字段提供一个名称,然后输入其值。 单击添加自定义字段按钮将其保存。

The field will be stored and displayed in the custom fields meta box like this:

该字段将被存储并显示在自定义字段元框中,如下所示:

Saved custom field

You can edit this custom field any time you want and then click on the update button to save your changes. You can also delete it as needed.

您可以随时编辑此自定义字段,然后单击更新按钮以保存您的更改。 您也可以根据需要将其删除。

Now you can save your post to store your custom field settings.

现在,您可以保存您的帖子以存储您的自定义字段设置。

在WordPress主题中显示自定义字段 (Displaying Custom Fields in WordPress Themes)

To display your custom field on your website, you will need to edit your WordPress theme files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

要在您的网站上显示您的自定义字段,您将需要编辑WordPress主题文件。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码

First, you will need to find the theme file that you need to edit to display your custom field. Ideally you would want to display it on a single post page. You will need to edit the single.php or content-single.php file.

首先,您将需要找到需要编辑以显示自定义字段的主题文件。 理想情况下,您希望将其显示在单个帖子页面上。 您将需要编辑single.php或content-single.php文件。

You will need to enter your custom fields code inside the WordPress loop. Look for the line that looks like this:

您将需要在WordPress循环内输入自定义字段代码。 查找如下所示的行:

<?php while ( have_posts() ) : the_post(); ?>

You want to make sure that you add your code before the following line:

您要确保在以下行之前添加代码:

<?php endwhile; // end of the loop. ?>

Now you need to add this code to your theme file:

现在,您需要将此代码添加到主题文件中:


<?php echo get_post_meta($post->ID, 'key', true); ?>

Don’t forget to replace key with the name of your custom field. For example, we used this code in our demo theme:

不要忘了用您的自定义字段名称替换键。 例如,我们在演示主题中使用了以下代码:

<p>Today's Mood: <?php echo get_post_meta($post->ID, 'Mood', true); ?></p>

You can now save your changes and visit the post where you added the custom field to see it in action.

现在,您可以保存所做的更改,并访问在其中添加了自定义字段的帖子,以查看其作用。

Custom field data displayed in a WordPress theme

Now you can use this custom field in all your other WordPress posts as well.

现在,您也可以在所有其他WordPress帖子中使用此自定义字段。

Simply create a new post or edit an existing one. Go to the custom fields meta box and select your custom field from the drop down menu and enter its value.

只需创建一个新帖子或编辑一个现有的帖子。 转到自定义字段元框,然后从下拉菜单中选择您的自定义字段,然后输入其值。

Reuse custom field

Click on ‘Add Custom Field’ button to save your changes and then publish or update your post.

单击“添加自定义字段”按钮以保存您的更改,然后发布或更新您的帖子。

在帖子编辑屏幕上的下拉菜单中找不到自定义字段 (Can’t Find Custom Field in Dropdown on Post Edit Screen)

By default, WordPress only loads 30 custom fields in this form.

默认情况下,WordPress仅以这种形式加载30个自定义字段。

If you are using WordPress themes and plugins that already use custom fields, then there is a chance that those will appear first in the drop-down menu and you’ll not be able to see your newly created custom field.

如果您使用的是已经使用自定义字段的WordPress主题和插件,那么这些主题和插件将首先出现在下拉菜单中,并且您将看不到新创建的自定义字段。

To fix this issue, you’ll need to add the following code to your theme’s functions.php file or a site-specific plugin.

要解决此问题,您需要将以下代码添加到主题的functions.php文件或特定站点的插件中



add_filter( 'postmeta_form_limit', 'meta_limit_increase' );
function meta_limit_increase( $limit ) {
    return 50;
}

The above code will change that limit to 50. If you still can’t see your custom field then try increasing that limit even further.

上面的代码会将限制更改为50。如果仍然看不到自定义字段,请尝试进一步增加该限制。

为自定义字段创建用户界面 (Creating a User Interface for Custom Fields)

As you can see, that once you add a custom field, you will have to select the field and enter its value each time you write a post.

如您所见,添加自定义字段后,每次撰写帖子时都必须选择该字段并输入其值。

If you have many custom fields or multiple users writing on your website, then this is not an ideal solution.

如果您有许多自定义字段或多个用户在您的网站上写作,那么这不是理想的解决方案。

Wouldn’t it be nice if you could create a user interface where users can fill in a form to add values to your custom fields?

如果您可以创建一个用户界面,用户可以在其中填写表格以向您的自定义字段添加值,那不是很好吗?

This is what so many popular WordPress plugins already do. For example, the SEO title and meta description box inside the popular All in One SEO plugin is a custom meta box:

这就是许多流行的WordPress插件已经做的事情。 例如,流行的All in One SEO插件中的SEO标题和元描述框是一个自定义元框:

All in One SEO Pack Meta Box

The easiest way to do this is by using the Advanced Custom Fields plugin.

最简单的方法是使用“ 高级自定义字段”插件。

使用高级自定义字段添加自定义字段 (Adding Custom Fields Using Advanced Custom Fields)

First thing you need to do is install and activate the Advanced Custom Fields plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活“ 高级自定义字段”插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Custom Fields » Field Groups page and click on the add new button.

激活后,您需要访问“ 自定义字段»字段组”页面,然后单击添加新按钮。

Add new field group

A field group is like a container of a set of custom fields. This allows you to add multiple panels of custom fields.

字段组就像一组自定义字段的容器。 这使您可以添加多个自定义字段面板。

Now, you need to provide a title for your field group and then click on the the ‘Add Field’ button.

现在,您需要为字段组提供标题,然后单击“添加字段”按钮。

Add new field

You can now provide a name for your custom field and select a field type. Advanced Custom Fields allows you to create all sorts of fields including text, image upload, number, dropdown, checkboxes, and more.

现在,您可以为自定义字段提供一个名称,然后选择一个字段类型。 高级自定义字段允许您创建各种字段,包括文本,图像上载,数字,下拉列表,复选框等。

Adding a new fielld

Scroll down, and you’ll see other options for that particular field. You can change them to your own requirements.

向下滚动,您将看到该特定字段的其他选项。 您可以将它们更改为自己的要求。

You can add multiple fields to your field group if you want. Once you are finished, click on the publish button to save your changes.

您可以根据需要将多个字段添加到字段组中。 完成后,单击“发布”按钮以保存您的更改。

You can now edit a post or create a new one and you’ll see a new panel for your custom fields below the content editor.

现在,您可以编辑帖子或创建新帖子,然后在内容编辑器下方将看到一个用于自定义字段的新面板。

Custom field panel on the post edit screen

For detailed step by step instructions, see our guide on how to add custom meta boxes in WordPress posts and post types.

有关详细的分步说明,请参阅有关如何在WordPress帖子和帖子类型中添加自定义元框的指南。

使用条件语句隐藏空的自定义字段 (Hide Empty Custom Fields with Conditional Statement)

So far we have covered how to create a custom field and display it in your theme.

到目前为止,我们已经介绍了如何创建自定义字段并将其显示在您的主题中。

Now let’s see how to check if the custom field is not empty before displaying it. To do that, we will modify our code to first check if the field has data in it.

现在,让我们看看如何在显示自定义字段之前检查它是否为空。 为此,我们将修改代码以首先检查该字段中是否包含数据。


<?php 

$mood = get_post_meta($post->ID, 'Mood', true);

if ($mood) { ?>

<p>Today's Mood: <? echo $mood; ?></p>

<?php 

} else { 
// do nothing; 
}

?>

Don’t forget to replace Mood with your own custom field name.

不要忘记用您自己的自定义字段名称替换Mood。

将多个值添加到自定义字段 (Adding Multiple Values to a Custom Field)

Custom fields can be reused in the same post again to add multiple values. You just need to select it again and add another value.

自定义字段可以再次在同一帖子中重复使用以添加多个值。 您只需要再次选择它并添加另一个值即可。

Adding multiple values to a custom field

However, the code we have used in the above examples will only be able to show a single value.

但是,我们在以上示例中使用的代码将只能显示单个值。

To display all values of a custom field, we need to modify the code and make it return the data in an array. You will need to add the following code in your theme file:

要显示自定义字段的所有值,我们需要修改代码并使其返回数组中的数据。 您将需要在主题文件中添加以下代码:


<?php 
$mood = get_post_meta($post->ID, 'Mood', false);
if( count( $mood ) != 0 ) { ?>
<p>Today's Mood:</p>
<ul>
<?php foreach($mood as $mood) {
            echo '<li>'.$mood.'</li>';
            }
            ?>
</ul>
<?php 
} else { 
// do nothing; 
}
?>

Don’t forget to replace Mood with your own custom field name.

不要忘记用您自己的自定义字段名称替换Mood。

In this example, you would notice that we have changed the last parameter of get_post_meta function to false. This parameter defines whether the function should return a single value or not. Setting it to false allows it to return the data as an array, which we then displayed in a foreach loop.

在此示例中,您会注意到我们已将get_post_meta函数的最后一个参数更改为false。 此参数定义函数是否应返回单个值。 将其设置为false使其可以将数据作为数组返回,然后将其显示在foreach循环中。

显示带有特定自定义键的帖子 (Displaying Posts with a Specific Custom Key)

WordPress allows you to display posts with custom keys and their values. For example, if you are trying to create a custom archive page to display all posts with specific custom keys, then you can use WP_Query class to query posts matching those fields.

WordPress允许您显示带有自定义键及其值的帖子。 例如,如果您试图创建一个自定义存档页面以显示具有特定自定义键的所有帖子,则可以使用WP_Query类查询与这些字段匹配的帖子。

You can use the following code as a starting point.

您可以使用以下代码作为起点。


$args = array(
	'meta_key'   => 'Mood',
	'meta_value' => 'Happy'
);
$the_query = new WP_Query( $args );

<?php 
// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

	<!-- the loop -->
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
		<h2><?php the_title(); ?></h2>
		<?php the_content(); ?>

	<?php endwhile; ?>
	<!-- end of the loop -->

	<!-- pagination here -->

	<?php wp_reset_postdata(); ?>

<?php else : ?>
	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


Don’t forget to replace meta_key and meta_value parameters with your own values.

不要忘记用自己的值替换meta_keymeta_value参数。

使用自定义字段添加来宾作者姓名 (Add Guest Author Name Using Custom Fields)

Do you want to add a guest post but don’t want to add a new user profile just to add a single post? An easier way to do that is by adding guest author name as a custom field.

您要添加访客帖子,但不想添加新用户个人资料只是为了添加单个帖子吗? 一种简单的方法是将来宾作者姓名添加为自定义字段。

First, you need to add the following code in your theme’s functions.php file or a site-specific plugin.

首先,您需要在主题的functions.php文件或特定站点的插件中添加以下代码。


add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'guest-author', true );
if ( $author )
$name = $author;
return $name;
}

This code hooks a function to the_author and get_the_author_display_name filters in WordPress.

此代码将函数挂钩到WordPress中的the_authorget_the_author_display_name 过滤器

The function first checks for the guest author name. If it exists, then it replaces the author’s name with the guest author name.

该函数首先检查来宾作者姓名。 如果存在,则将其名称替换为来宾作者名称。

Now you will need to edit the post where you want to display the guest author name. Go to the custom fields meta box and add your guest author name.

现在,您将需要编辑要显示来宾作者姓名的帖子。 转到自定义字段元框,然后添加您的来宾作者姓名。

Adding guest author custom field

For details, see our article on how to rewrite guest author name with custom fields in WordPress.

有关详细信息,请参阅有关如何使用WordPress中的自定义字段重写来宾作者姓名的文章。

使用自定义字段显示文章的撰稿人 (Display Contributors to an Article Using Custom Fields)

On many popular blogs and news sites, multiple authors contribute to write an article. However, WordPress only allows a single author to be associated with a post.

在许多流行的博客和新闻网站上,多位作者撰写了一篇文章。 但是,WordPress仅允许将单个作者与帖子关联。

One way to solve this problem is by using Co-Authors Plus plugin. To learn more, see our guide on how to add multiple authors on a WordPress post.

解决此问题的一种方法是使用Co-Authors Plus插件。 要了解更多信息,请参阅有关如何在WordPress帖子上添加多个作者的指南。

Another way to do that is by adding contributors as a custom field.

另一种方法是通过将贡献者添加为自定义字段。

First you need to edit the post where you want to display co-authors or contributors. Scroll down to custom fields meta box and add author names as co-author custom field.

首先,您需要编辑要显示共同作者或贡献者的帖子。 向下滚动到“自定义字段”元框,然后将作者姓名添加为共同作者自定义字段。

Adding co-author custom field

Now add this code to your theme files where you want to show co-authors.

现在,将此代码添加到您要显示共同作者的主题文件中。



<?php 

$coauthors = get_post_meta($post->ID, 'co-author', false);
if( count( $coauthors ) != 0 ) { ?>
<ul class="coauthors">
<li>Contributors</li>
<?php foreach($coauthors as $coauthors) { ?>
           <?php echo '<li>'.$coauthors.'</li>' ;
            }
            ?>
</ul>
<?php 
} else { 
// do nothing; 
}
?>

To display author names separated by commas, you can add the following custom CSS.

要显示用逗号分隔的作者姓名,您可以添加以下自定义CSS


.coauthors ul { 
display:inline;
}
.coauthors li { 
display:inline;
list-style:none;
}
.coauthors li:after { 
content:","
}
.coauthors li:last-child:after {
    content: "";
}
.coauthors li:first-child:after {
    content: ":";
}

This is how it looked on our demo site.

这就是在我们的演示站点上的外观。

Co-authors displayed using custom fields
在WordPress循环外显示自定义字段 (Display Custom Fields Outside the Loop in WordPress)

So far we have shown you all the examples where custom fields are displayed inside the WordPress loop. What if you needed to show them outside the loop? For example, in the sidebar of a single post.

到目前为止,我们已经向您展示了在WordPress循环内显示自定义字段的所有示例。 如果需要在循环外显示它们怎么办? 例如,在单个帖子的边栏中。

To display the custom fields outside the WordPress loop add the following code:

要在WordPress循环外显示自定义字段,请添加以下代码:


<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'key', true);
wp_reset_query();
?>

Don’t forget to replace the key with your custom field name.

不要忘记用您的自定义字段名称替换密钥。

使用自定义字段显示自定义页眉,页脚,侧边栏 (Display Custom Header, Footer, Sidebar using Custom Fields)

Usually most WordPress themes use the same header, footer, and sidebar on all pages. There are multiple ways to show different sidebars, header, or footer for different pages on your website. See our guide on how to display different sidebar for each WordPress post or page.

通常,大多数WordPress主题在所有页面上都使用相同的页眉,页脚和侧边栏。 有多种方法可以显示网站上不同页面的不同侧边栏,页眉或页脚。 请参阅我们的指南,了解如何为每个WordPress帖子或页面显示不同的侧边栏

One way to do this is by using custom fields. Edit the post or page where you want to show a different sidebar and then add the sidebar as custom field.

一种方法是使用自定义字段。 编辑要在其中显示其他侧边栏的帖子或页面,然后将侧边栏添加为自定义字段。

Adding custom sidebar to a post using custom field

Now you need to edit your WordPress theme files like single.php where you want to display custom sidebar. You will be looking for the following code:

现在,您需要编辑WordPress主题文件(例如single.php),以在其中显示自定义边栏。 您将寻找以下代码:


<?php get_sidebar(); ?>

Replace this line with the following code:

用以下代码替换此行:


<?php 
global $wp_query;
$postid = $wp_query->post->ID;
$sidebar = get_post_meta($postid, "sidebar", true);
get_sidebar($sidebar);
wp_reset_query();
?>

This code simply looks for the sidebar custom field and then displays it in your theme. For example, if you add wpbpage as your sidebar custom field, then the code will look for sidebar-wpbpage.php file to display.

此代码仅查找侧边栏自定义字段,然后将其显示在主题中。 例如,如果将wpbpage添加为侧边栏自定义字段,则代码将查找sidebar-wpbpage.php文件以显示。

You will need to create sidebar-wpbpage.php file in your theme folder. You can copy the code from your theme’s sidebar.php file as a starting point.

您将需要在主题文件夹中创建sidebar-wpbpage.php文件。 您可以从主题的sidebar.php文件中复制代码作为起点。

使用自定义字段处理RSS feed内容 (Manipulating RSS feed Content with Custom Fields)

Want to display additional meta data or content to your RSS feed users? Using custom fields you can manipulate your WordPress RSS feed and add custom content into your feeds.

是否想向您的RSS feed用户显示其他元数据或内容? 使用自定义字段,您可以操纵WordPress RSS feed并将自定义内容添加到feed中。

First you need to add the following code in your theme’s functions.php file or a site-specific plugin.

首先,您需要在主题的functions.php文件或特定站点的插件中添加以下代码。


function wpbeginner_postrss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$coolcustom = get_post_meta($postid, 'coolcustom', true);
if(is_feed()) {
if($coolcustom !== '') {
$content = $content."<br /><br /><div>".$coolcustom."</div>
";
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');

Now just create a custom field called “coolcustom” and add any value you like. You can use it to display advertisements, images, text, or anything you want.

现在,只需创建一个名为“ coolcustom”的自定义字段,然后添加您喜欢的任何值即可。 您可以使用它来显示广告,图像,文本或任何您想要的东西。

使用自定义字段处理RSS Feed标题 (Manipulate RSS Feed Title with Custom Fields)

Sometimes you may want to add extra text to a post title for RSS feed users. For example, if you are publishing a sponsored post or a guest post.

有时,您可能想为RSS feed用户在帖子标题中添加其他文本。 例如,如果您要发布赞助帖子或来宾帖子。

First you add the following code in your theme’s functions.php file or a site-specific plugin.

首先,在主题的functions.php文件或特定站点的插件中添加以下代码。


function wpbeginner_titlerss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$gpost = get_post_meta($postid, 'guest_post', true);
$spost = get_post_meta($postid, 'sponsored_post', true);

if($gpost !== '') {
$content = 'Guest Post: '.$content;
}
elseif ($spost !== ''){
$content = 'Sponsored Post: '.$content;
}
else {
$content = $content;
}
return $content;
}
add_filter('the_title_rss', 'wpbeginner_titlerss');

Next, you need to edit the post where you want to display the extra text in the title field and add guest_post and sponsored_post in custom fields.

接下来,您需要编辑要在标题字段中显示额外文本的帖子,并在自定义字段中添加guest_post和Sponsored_post。

Sponsored and guest post custom fields

If any of these two custom fields are found with a value “true”, then it will add the appropriate text before the title. This technique can be utilized in various ways to fit whatever you like.

如果发现这两个自定义字段中的任何一个的值为“ true”,则它将在标题之前添加适当的文本。 可以通过各种方式利用此技术以适合您的任何需求。

Want to learn more cool RSS feed hacks? See our guide on how to add content and manipulate your WordPress RSS feeds.

想了解更多酷的RSS feed技巧吗? 请参阅我们的指南,了解如何添加内容和操作WordPress RSS feed

使用自定义字段设置WordPress中帖子的过期日期 (Set Expiration Date for Posts in WordPress Using Custom Fields)

Want to set an expiration date for some posts on your WordPress site? This comes handy in situations when you want to publish content only for a specific period like running surveys or limited time offers.

是否想为您的WordPress网站上的某些帖子设置过期日期? 当您只想在特定时期内发布内容(例如进行调查或限时优惠)时,这非常方便。

One way to do this is by manually removing the post content or by using a plugin like Post Expirator plugin.

一种方法是手动删除帖子内容或使用类似Post Expirator插件的插件。

Another way to do this is by using custom fields to automatically expire posts after a specific time.

另一种方法是使用自定义字段在特定时间后自动使帖子过期。

You will need to edit your theme files and add modify the WordPress loop like this:

您将需要编辑主题文件并添加修改WordPress循环的方法,如下所示:


<?php
if (have_posts()) :
while (have_posts()) : the_post(); 
$expirationtime = get_post_meta($post->ID, "expiration", false);
if( count( $expirationtime ) != '' ) { 
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween >= 0 ) {
echo 'This post will expire on ' .$expirestring.'';
the_content();
} else { 
echo "Sorry this post expired!"
}
} else { 
the_content();
} 
endwhile;
endif;
?>

Note: You will need to edit this code to match your theme.

注意:您将需要编辑此代码以匹配您的主题。

After adding this code, you can add the expiration custom field to the post you want to expire. Make sure you add the time in this format mm/dd/yyyy 00:00:00.

添加此代码后,可以将过期自定义字段添加到要过期的帖子中。 确保以以下格式添加时间mm / dd / yyyy 00:00:00

Adding an expiration date using custom field
使用自定义字段为单个帖子设置样式 (Style Individual Posts Using Custom Fields)

Want to change the look of an individual post using CSS? WordPress automatically assigns each post its own class which you can use to add custom CSS.

是否要使用CSS更改单个帖子的外观? WordPress自动为每个帖子分配自己的类,您可以使用该类添加自定义CSS。

However, using custom fields you can add your own custom classes and then use them to style posts differently.

但是,使用自定义字段可以添加自己的自定义类,然后使用它们对帖子进行不同的样式设置。

First you need to edit a post that you would like to style differently. Go to custom fields box and the post-class custom field.

首先,您需要编辑要更改样式的帖子。 转到自定义字段框和课后自定义字段。

Post class custom field

Next, you need to edit your WordPress theme files and add this code at the beginning of WordPress loop.

接下来,您需要编辑WordPress主题文件,并在WordPress循环开始时添加此代码。

<?php $custom_values = get_post_meta($post->ID, 'post-class'); ?>

Now you need to find a line with the post_class() function. Here is how it looked in our demo theme:

现在,您需要使用post_class()函数查找一行。 这是在我们的演示主题中的外观:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

Change this line to include your custom field value, like this:

更改此行以包括您的自定义字段值,如下所示:

<article id="post-<?php the_ID(); ?>" <?php post_class($custom_values); ?>>

Now if you examine the post’s source code using Inspect tool, then you will see your custom field CSS class added to the post class.

现在,如果您使用Inspect工具检查帖子的源代码,那么您将看到自定义字段CSS类已添加到帖子类中。

Custom field post class

Now you can use this CSS class to add custom CSS and style your post differently.

现在,您可以使用此CSS类添加自定义CSS,并以不同的方式设置帖子样式。

That’s all, we hope this article helped you learn more about WordPress custom fields. You may also want to see our ultimate step by step guide to boost WordPress speed and performance for beginners.

仅此而已,我们希望本文能帮助您了解有关WordPress自定义字段的更多信息。 您可能还希望查看我们的最终分步指南,以提高初学者的WordPress速度和性能

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值