网格中的Wordpress自定义帖子模板

问题描述 投票:0回答:1

所以我刚刚开始学习wordpress,我已经完成了一些关于html / css和javascript / jquery的课程。我无法弄清楚如何使用我为自定义帖子类型制作的html模板,我想要的在某个网格中显示。这是html / css:Service's Cards Post Type中的内容

所以基本上我需要使用这样的模板:

    <div class="card">
    <div class="service">
         <div class="circle"></div>
         <img src="<php field here>" class="service-image">
<div class="card-text">
<h4 class="card-title"><php field here></h4>
<p class="card-info"><php field here></p></div>
        </div></div>

所以每次我从我创建的自定义帖子类型创建一个帖子时,我有那个三个字段的模板和没有输入的其他div。我不确定到底应该找到什么,所以我会很感激帮助,甚至只是解决方案的路径。就像我应该寻找的那些文章和教程一样。谢谢

javascript html css wordpress
1个回答
0
投票

在这里我做了什么,所以如果它有助于somone,那将是伟大的:

1.创建自定义帖子类型(稍后定位帖子类型,但没有必要,您可以按类别,ID等进行):https://codex.wordpress.org/Function_Reference/register_post_type

2.通过我需要的页面中的post loop来对帖子网格进行可视化:

<?php 
$args = array('post_type' => 'cptname'); /* create variable with the post type characteristics you want to display.*/
$loop = new WP_Query($args);/* new variable making the $args into wp_query */
if ($loop->have_posts()):/* if cptname have posts do that */
    while( $loop->have_posts()) :$loop->the_post();?>/* while cptname have posts take the post */
<?php get_template_part('custom','type'); ?>/* the template of the cpt */
<?php endwhile;
endif;
wp_reset_postdata();?>

您可能想要添加更多参数:https://codex.wordpress.org/Template_Tags/get_posts

3.创建cpt的模板。

在这种情况下,我需要一个名为custom-type.php的新文件,我必须存储我希望我的cpt的帖子显示的方式。在我的例子中,我有一个缩略图,一个带有网址的标题和两个自定义帖子字段 - 奖品和信息。

对于缩略图:

<img class="your-image" <?php the_post_thumbnail();?>

标题:

<?php the_title( sprintf('<h1 class="your-title"><a href="%s">', esc_url(get_permalink())),'</a></h1>'); ?>

对于自定义帖子字段:

<?php echo get_post_meta( get_the_ID(), 'Information', true ); ?>
<?php echo get_post_meta( get_the_ID(), 'Prize', true ); ?>

4.创建自定义帖子类型单页。在这里,我不得不创建一个名为single-(cpt的名称).php的新文件,然后调用与以前相同的函数加上内容的函数:

<?php the_content(); ?>

还有很多你可以使用的。这个是你希望这个自定义帖子类型的每个帖子的单页看起来如何。

希望能帮到某个人,如果有什么我做错了或错过了,我就是要倾听并了解更多。谢谢:)

© www.soinside.com 2019 - 2024. All rights reserved.