简体   繁体   English

如何在 wordpress 中设置首页自定义帖子类型帖子

[英]How to set front page in wordpress a custom post type post

I have created a custom post type by CPT UI plugin in WordPress, now I need to show a custom post type page on front page of WordPress by setting >> reading in admin panel.我已经通过 WordPress 中的 CPT UI 插件创建了自定义帖子类型,现在我需要通过在管理面板中设置 >> 阅读在 WordPress 的首页上显示自定义帖子类型页面。 but this type of posts isn't there?但是这种类型的帖子不存在吗? how can I display them?!我怎样才能显示它们?!

You're not going to be able to do that because CPT is not a page, it is a post.您将无法做到这一点,因为 CPT 不是页面,而是帖子。 What you can do is create a page template for the homepage which makes a query and gets the post data.您可以做的是为主页创建一个页面模板,该模板进行查询并获取帖子数据。 This is how you would do that你会这样做

  1. First create a page template with this code and place it in the root of your theme首先使用此代码创建一个页面模板并将其放在主题的根目录中

    <?php /* Template Name: Your template name */?>

  2. Then go into the pages tab in the dashboard and open the homepage.然后 go 进入仪表板中的页面选项卡并打开主页。 Set the page template option (dropdown menu) to the template you just created.将页面模板选项(下拉菜单)设置为您刚刚创建的模板。

  3. Edit the template file and include this code (replace "123" with the post ID):编辑模板文件并包含此代码(将“123”替换为帖子 ID):

    <?php $the_post = get_post(123); ?>

  4. That will give you a Wordpress post object you can work with.这将为您提供 Wordpress 后 object 您可以使用。

**** UPDATE **** 更新

When you have your post in a variable you can do things like this:当您将帖子放在变量中时,您可以执行以下操作:

<?php echo get_the_title($the_post); ?>
<?php echo $the_post->post_content; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM