【问题标题】:Display Custom Posts in Custom Template in Wordpress在 Wordpress 的自定义模板中显示自定义帖子
【发布时间】:2021-10-13 14:03:32
【问题描述】:

卡住了! 我为我的 WP 后台制作了一个自定义帖子类型:

register_post_type('realisations',[
'label' => 'Réalisations',
'public' => true,
'menu_position' => 3,
'menu_icon' => 'dashicons-format-gallery',
'supports' => ['title', 'editor', 'thumbnail'],

]);

我希望所有文章都显示在我的自定义模板中:

<?php/* Template Name: Réalisations */?>

<?php materialis_get_header();?>


<div class="top-page">
    <div class="title">
        <i class="fas fa-camera"></i>
        <h2>Réalisations</h2>
    </div>

    <div class="description-page">
        <p>Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen</p>
    </div>

    <div>
        
        

    </div>
</div>
<?php get_footer(); ?>

所以我尝试了我找到的这个旧代码,它只显示帖子的标题,而不是里面的文章:

<?php/* Template Name: Réalisations */?>

<?php materialis_get_header();?>

 

<div class="top-page">
    <div class="title">
        <i class="fas fa-camera"></i>
        <h2>Réalisations</h2>
    </div>

    <div class="description-page">
        <p>Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen</p>
    </div>

    <div>

        <?php
        $custom_post_types = get_post_types(
    array(
        // Set to FALSE to return only custom post types
        '_builtin' => false,
        // Set to TRUE to return only public post types
        'public' => true
    ),
    // Set to "objects", so we have the full CPT object
    'objects'
 );

// Add CPT permalinks to CPT objects
foreach ( $custom_post_types as $custom_post_type ) {
    $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
}

// Output CPT archive index permalinks list
echo '<ul>';
foreach ( $custom_post_types as $custom_post_type ) {
    echo '<li>';
    echo '<a href="' . $custom_post_type->permalink . '">' . $custom_post_type->name . '</a>';
    echo '</li>';
}
echo '</ul>';
?>

    </div>
</div>
<?php get_footer(); ?>

很抱歉成为 wordpress 中的菜鸟!并感谢您即将提供的答案!

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    所以 - 有点混淆。此代码(前面的示例)用于在 WordPress 中创建自定义帖子类型...它类似于普通帖子(如博客中使用的帖子),而不是您尝试应用的页面模板。

    “自定义帖子类型”和“页面模板”之间存在一些差异。页面模板仍然是页面,这意味着它们没有类别,通常没有特色图像(缩略图),并且不按时间顺序(默认情况下)以及我们定义它们以用于主题的方式进行查询。

    自定义帖子类型与帖子非常相似 - 您可以为它们创建“类别”结构,添加标签等。

    这是您设置自定义帖子类型的方式
    在你的主题 functions.php 或包含在其中的文件中。

    add_action('init', 'projects_labels');
    function projects_labels() {
        $labels = array(
            'name'                  => __('Projects', THEME_NAME),
            'singular_name'         => __('Project', THEME_NAME),
            'add_new'               => __('Add Project', THEME_NAME),
            'add_new_item'          => __('Add Project', THEME_NAME),
            'edit_item'             => __('Edit Project', THEME_NAME),
            'new_item'              => __('New Project', THEME_NAME),
            'all_items'             => __('Projects', THEME_NAME),
            'view_item'             => __('View Project', THEME_NAME),
            'search_items'          => __('Find a Project', THEME_NAME),
            'not_found'             => __('No Projects Found', THEME_NAME),
            'not_found_in_trash'    => __('No Projects Found In The Trash', THEME_NAME), 
            'parent_item_colon'     => '',
            'menu_name'             => __('Projects', THEME_NAME),
    
        );
        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'publicly_queryable'    => true,
            'show_ui'               => true, 
            'show_in_rest'          => true, 
            'query_var'             => true,
            'capability_type'       => 'post',
            'has_archive'           => false,       // do u want categories?
            'hierarchical'          => true,
            'menu_position'         => 90,
            'menu_icon'             => get_bloginfo('template_url').'/img/projects.png',        // icon image 
            'supports'              => array('title', 'editor', 'author', 'custom-fields'),     // add support & features
        );
        register_post_type('project', $args);
    }
    

    要使用这些,您需要创建一个名为

    的文件
    single-project.php 
    

    名称的结构如下:single-CUSTOM_POSTYPE_NAME.php

    在主题的根文件夹中创建该文件。那么您可以访问您创建的新 postype - 如果您遇到任何问题,请访问永久链接页面(在 wp 后端) - 访问将刷新永久链接结构。

    single-project.php 的示例代码

    <?php 
    get_header();
    
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            
            echo '
            <main class="project-wrap">
                <div class="container">
                    <div class="page-title"> <h1 class="title-tag">'.get_the_title().'</h1> </div>
                    <div class="page-content">'.apply_filters('the_content', get_the_content()).'</div>
                </div>
            </main>
            ';
        }
    }
    
    get_footer();
    ?>
    

    这都是非常基本的 - 我鼓励您使用 youtube 来了解有关自定义 postype 的更多信息,因为在不共享屏幕的情况下很难在简短的回答中解释这一点。

    祝你好运..

    【讨论】:

    • 感谢您花时间解释所有这些细节!我试试看!
    • 当然,如果您需要其他帮助,请告诉我。爱你的名字;)
    【解决方案2】:

    创建 .php 文件并添加此代码并从您的管理面板中选择您的模板实现

    <?php
    /* 
    Template Name: Réalisations 
    */
    ?>
    $args = array(
        'post_type' => 'realisations',
        'post_status' => 'publish',
        'posts_per_page' => 10,
    );
      
    $arr_posts = new WP_Query( $args );
    ?>
    <div class="row">  
    <?php
    if ( $arr_posts->have_posts() ) :
      
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <div class="col-sm-3 col-md-3">
            <div class="productbox">
                <div class="product-image">
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail("full");
                endif;
                ?>
            </div>    
            <h3 class="entry-title"><?php the_title(); ?></h3>
            </div>
            </div>
            <?php
        endwhile;
    endif;
    wp_reset_query();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多