Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops WordPress WP_查询仅显示数组中的第一个元素_Loops_Wordpress - Fatal编程技术网

Loops WordPress WP_查询仅显示数组中的第一个元素

Loops WordPress WP_查询仅显示数组中的第一个元素,loops,wordpress,Loops,Wordpress,我目前正在学习WordPress插件开发,我正在尝试为我的插件构建一个简短的代码 我的插件基本上是一个名为property的自定义帖子类型,其中包含3个元数据框 价格、地点和施工日期 还有一种称为property type的分类法,在后端设置为rent或sale 所有这些都是有效的,如果你把它们放在wordpress中,它们就会起作用 但是我的shortcode文件确实工作正常,WP_查询上的循环应该返回我写的所有文章,但是它只返回WP_查询中找到的第一个元素 有谁能给我指点方向,或纠正我弄错的

我目前正在学习WordPress插件开发,我正在尝试为我的插件构建一个简短的代码 我的插件基本上是一个名为property的自定义帖子类型,其中包含3个元数据框 价格、地点和施工日期 还有一种称为property type的分类法,在后端设置为rent或sale 所有这些都是有效的,如果你把它们放在wordpress中,它们就会起作用 但是我的shortcode文件确实工作正常,WP_查询上的循环应该返回我写的所有文章,但是它只返回WP_查询中找到的第一个元素

有谁能给我指点方向,或纠正我弄错的地方吗

谢谢大家

现在我的短代码文件名是:properties\u post\u type\u 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\u post\u 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';
 });
属性\u post\u type\u 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\u post\u 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';
 });

要查询所有帖子,请将以下内容添加到WP_query的参数中:

您的代码:

$loop = new WP_Query( array(
    'post_type' =>  'property_post',
    'orderby' => 'title',
    'posts_per_page' => -1
) );