我目前正在学习WordPress插件开发,我正在尝试为我的插件构建一个简短的代码 我的插件基本上是一个名为property的自定义帖子类型,里面有3个元数据框 价格位置和建设日期 和一种称为财产类型的分类,在后端设置租金或销售 所有这一切都有效,如果你把它们放在wordpress中,它们就会起作用 但我的短代码文件确实正常工作WP_Query上的循环应该返回我所做的所有帖子,但它只返回WP_Query中找到的第一个元素
任何人都可以指导我或修复我错误的地方
感谢所有
现在我的短代码文件名是:properties_post_type_shortcode.php
我的插件文件名是:properties_post_type.php
properties_post_type_shortcode.php的代码
<?php
add_shortcode('land_properties',function(){
$loop = new WP_Query(
array(
'post_type' => 'property_post',
'orderby' => 'title'
)
);
if ($loop->have_posts()){
$output = '<ul class="land_properties_list">';
$i=0;
while( $loop->have_posts() ){
$loop->the_post();
$meta=get_post_meta(get_the_id(),'' );
$output= '
<li>
<a href="' .get_permalink() . '">
' . get_the_title() . ' | '.
$meta['property_price'][0]. " " .
$meta['property_location'][0]. " " .
$meta['property_date'][0]. " " .
'
</a>
<div>' . get_the_excerpt() . '</div>
</li>
';
}
}
else {
$output="No lands added";
}
// $loop->wp_reset_postdata();
return $output;
});
properties_post_type.php的代码
<?php
/*
* Plugin Name: properties_post_type
* Plugin URI: Have not be set yet
* Description: this plugin allow you to create custom post type property which you can be modified and edited
* Version: 1.0
* Author: Muhab Alwan
* Author URI: https://www.facebook.com/HaaaB
* License: A "Slug" license name e.g. GPL2
*/
class PROP_POST_TYPE{
//default contructor
public function __construct()
{
$this->register_post_type();
$this->taxonomies();
$this->metaboxes();
}
public function register_post_type()
{
$args= array(
'labels' => array(
'name'=> 'Properties',
'singular_value' => 'Property',
'add_new' => 'Add New Property',
'add_new_item' => 'Add New Property',
'edit_items' => 'Edit_Items',
'new_item' => ' Add New Items',
'view_item'=> 'View Item',
'search_items' => 'Search Items',
'not_found' => 'No Property Found',
'not_found_in_trash' => 'No Property Found In Trash'),
'query_var' =>'properties',
'rewrite' => array(
'slug' => 'property/'),
'public' => true,
'menu_position' => 80, // set postion in the backend menu
'menu_icon' => admin_url(). 'images/media-button-other.gif', // define an image for the prop
'supports' => array(
'title',
//'editor',
'excerpt',
//'custom-fields' when we need user to build their own meta box Not required in project
) // specify what wordpress types are custom post type support
);
register_post_type('property_post', $args );
}
public function taxonomies()
{
$taxonomies = array();
$taxonomies['property_type'] = array(
'hierarchical' => true,
'query_var' => 'movie_genere',
'rewrite' => array('slug' => 'prop/type'
),
'labels' => array(
'name'=> 'Properties Type',
'singular_value' => 'Property Type',
'add_new' => 'Add New Property Type',
'add_new_item' => 'Add New Property Type',
'edit_items' => 'Edit Properties Type',
'new_item' => ' Add New Properties Type',
'view_item'=> 'View Property Type',
'search_items' => 'Search Properties Type',
'popular_items' => 'Popular Properties Type',
'separate_items_with_comments' => 'Separate Property Type With Comments',
'add_or_remove_items' => 'Add Or Remove Properties Type',
'choose_from_most_used' => 'Choose From Most Used Properties Type'
)
);
$this-> register_all_taxonomies($taxonomies); // register all taxonomies build in this plugin
}
public function register_all_taxonomies($taxonomies)
{
// foreach is for registering many taxonomy
foreach ($taxonomies as $name=> $arr)
{
//register ( what the taxonomies name, array of the object type that we register ex post or page
register_taxonomy($name,array('property_post'),$arr );
}
}
public function metaboxes()
{
// FIRST PRICE META BOX
add_action('add_meta_boxes', function(){
//css id, title, cb func, page, priority level, call back func argum
add_meta_box('property_price','Property Price', 'property_price','property_post');
add_meta_box('property_location','Property Location', 'property_location','property_post');
add_meta_box('property_date','Date Of Construction', 'property_date','property_post');
});
//PRICE PROP
function property_price($post){
$price_length = get_post_meta($post->ID,'property_price', true);
?>
<p>
<label for="property_price"> :</label>
<input type="number" pattern="[0-9]+" size="50" class="widfat" name="property_price" id="property_price" value="<?php echo esc_attr($price_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_price']))
{
update_post_meta(
$id,'property_price',
strip_tags($_POST['property_price'])
);
}
});
// LOCATIO PROP
function property_location($post){
$location_length = get_post_meta($post->ID,'property_location', true);
?>
<p>
<label for="property_location"> :</label>
<input type="text" class="widfat" name="property_location" id="property_location" value="<?php echo esc_attr($location_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_location']))
{
update_post_meta(
$id,'property_location',
strip_tags($_POST['property_location'])
);
}
});
function property_date($post){
$dof_length = get_post_meta($post->ID,'property_date', true);
?>
<p>
<label for="property_date"> :</label>
<input type="date" class="widfat" name="property_date" id="property_date" value="<?php echo esc_attr($dof_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_date']))
{
update_post_meta(
$id,'property_date',
strip_tags($_POST['property_date'])
);
}
});
//third date of construction metaboxes
}
}
// initialization essential to build a cust post
add_action('init', function(){
new PROP_POST_TYPE();
include dirname(__FILE__). '/properties_post_type_shortcode.php';
});
答案 0 :(得分:0)
要查询所有帖子,请将以下内容添加到WP_Query
:
'posts_per_page' => -1
您的代码:
$loop = new WP_Query( array(
'post_type' => 'property_post',
'orderby' => 'title',
'posts_per_page' => -1
) );