从wordpress自定义帖子类型获取标题

时间:2015-10-06 12:46:24

标签: php wordpress

我有一个自定义帖子类型如下(尚未自定义)。

function movie_reviews_init() {

    $args = array(
      'label' => 'Movie Reviews',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'movie-reviews'),
        'query_var' => true,
        'menu_icon' => 'dashicons-video-alt',
        'show_in_menu' => 'myplugin',
        'supports' => array(
            'title',
            'editor',
            'page-attributes',)
        );
    register_post_type( 'movie-reviews', $args );
}

我看了看,但不知道如何从帖子中获得标题。我需要变量中的标题与使用levenshtein-distance的用户输入进行比较,然后显示最相关的帖子。

1 个答案:

答案 0 :(得分:1)

在我的插件自定义帖子类型注册

function register_cpt_gallery() {
        $labels = array(
            'name' => _x( 'Gallery', 'gallery' ),
            'singular_name' => _x( 'gallery', 'gallery' ),
            'add_new' => _x( 'Add New Album', 'gallery' ),
            'add_new_item' => _x( 'Add New Album', 'gallery' ),
            'edit_item' => _x( 'Edit gallery', 'gallery' ),
            'new_item' => _x( 'New Album', 'gallery' ),
            'view_item' => _x( 'View Album', 'gallery' ),
            'search_items' => _x( 'Search Album', 'gallery' ),
            'not_found' => _x( 'No Album found', 'gallery' ),
            'not_found_in_trash' => _x( 'No Album found in Trash', 'gallery' ),
            'parent_item_colon' => _x( 'Parent Album:', 'gallery' ),
            'menu_name' => _x( 'Gallery', 'gallery' ),
        ); 
        $args = array(
            'labels' => $labels,
            'hierarchical' => true,
            'description' => 'Gallery filterable by genre',
            'supports' => array( 'title', 'editor', '', 'thumbnail', '', '', '', '', '' ),
            'taxonomies' => array( 'genres' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 5,
            'menu_icon' => 'dashicons-images-alt',
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post'
        ); 
        register_post_type( 'gallery', $args );
    }
    add_action( 'init', 'register_cpt_gallery' );

Post title

中获取php
<?php $gallery_args = array(
                'posts_per_page'   => -1,
                'orderby'=> 'date',
                'order'=> 'DESC',
                'post_type'=> 'gallery',
                'post_status'=> 'publish',
                'suppress_filters' => true 
        );
    $posts_display_gallery = get_posts( $gallery_args ); 
    foreach($posts_display_gallery as $rows){
        $post_title = $rows->post_title;
    } ?>