从 wordpress 自定义帖子类型获取标题 [英] Get title from wordpress custom post type
本文介绍了从 wordpress 自定义帖子类型获取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像下面这样的自定义帖子类型(尚未自定义).
I have a custom post type like below (not customized yet).
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 的用户输入进行比较,然后显示最相关的帖子.
I have looked and looked but cant figure out how to get the title from the posts. I need the title in a variable to compare with the users input using levenshtein-distance and later display the most relevant post.
推荐答案
在我的插件自定义帖子类型注册
In My plug-in custom post type Register
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' );
在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;
} ?>
这篇关于从 wordpress 自定义帖子类型获取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文