我有此自定义帖子类型,但无法弄清楚如何在我的网站上查看它

时间:2019-06-27 09:01:25

标签: php wordpress function custom-post-type

这是我在functions.php中的自定义帖子类型。我要完成的工作是:在子主题中进行选项卡翻译和对模板的拉式标题。但不是通过例如:translation-test.php,而是

(而不是通过acf)

add_action( 'init', function() {
    $label = 'Translations';
    $type = 'translation';
    register_post_type( $type, [ 'public' => true, 'label'  => $label ] );
});

不能找到简单的解释,每个人都为ug,子猫写信

2 个答案:

答案 0 :(得分:0)

要在前端显示帖子,您必须像这样在模板页面上放置代码:

if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$query = new WP_Query( array( 'post_type' => 'public', 'paged' => $paged ) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- Show some message here -->
<?php endif; ?> 

答案 1 :(得分:0)

首先,您需要以这种方式将帖子类型注册到您的function.php中。

add_action( 'init', 'reg_post_your_fn' );

function reg_post_your_fn() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}

在上面的代码中,“ book”是帖子类型。

现在,如果打开wordpress admin,则会在左侧菜单中看到Book post类型。现在,在其中添加一些帖子。

然后,您必须制作一个模板,或者在index.php中,可以在下面的代码中添加此代码以显示帖子。

    $args = array('post_type' => 'book');

    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
echo "No post found."
     }