WP_Query正在获取帖子,但_post()未呈现任何帖子

时间:2017-09-17 作者:Hridoy Mahmud

问题描述:

下面的代码来自一篇特色文章。php文件。将自定义帖子类型“特色文章”的此自定义模板从我的插件复制到活动主题文件夹。模板呈现良好。甚至是显示数组中数据的查询。

参见:print\\r($featured\\u articles)的结果在第二部分

但“the\\u post()”不显示任何帖子。代码有什么问题?

<?php

$featured_articles = new WP_Query(array(\'post_type\' => \'featured-article\'));

echo \'<pre>\';
print_r($featured_articles);
echo \'</pre>\';

if ($featured_articles->have_posts()):
    while ($featured_articles->have_posts()) : $featured_articles->the_post();

        // Include the single post content template.
        get_template_part(\'template-parts/post/content\', get_post_format());

        // If comments are open or we have at least one comment, load up the comment template.
        if (comments_open() || get_comments_number()) {
            comments_template();
        }

        if (is_singular(\'attachment\')) {
            // Parent post navigation.
            the_post_navigation(array(
                \'prev_text\' => _x(\'<span class="meta-nav">Published in</span><span class="post-title">%title</span>\', \'Parent post link\', \'twentysixteen\'),
            ));
        } elseif (is_singular(\'featured-article\')) {
            // Previous/next post navigation.
            the_post_navigation(array(
                \'next_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __(\'Next\', \'twentysixteen\') . \'</span> \' .
                    \'<span class="screen-reader-text">\' . __(\'Next post:\', \'twentysixteen\') . \'</span> \' .
                    \'<span class="post-title">%title</span>\',
                \'prev_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __(\'Previous\', \'twentysixteen\') . \'</span> \' .
                    \'<span class="screen-reader-text">\' . __(\'Previous post:\', \'twentysixteen\') . \'</span> \' .
                    \'<span class="post-title">%title</span>\',
            ));
        }

        // End of the loop.
    endwhile;
endif;
?>
print\\u r(特色文章)结果:

WP_Query Object
(
    [query] => Array
        (
            [post_type] => featured-article
        )

    [query_vars] => Array
        (
            [post_type] => featured-article
            [error] => 
            [m] => 
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [title] => 
            [fields] => 
            [menu_order] => 
            [embed] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [post_name__in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

            [author__in] => Array
                (
                )

            [author__not_in] => Array
                (
                )

            [ignore_sticky_posts] => 
            [suppress_filters] => 
            [cache_results] => 1
            [update_post_term_cache] => 1
            [lazy_load_term_meta] => 1
            [update_post_meta_cache] => 1
            [posts_per_page] => 2
            [nopaging] => 
            [comments_per_page] => 50
            [no_found_rows] => 
            [order] => DESC
        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => AND
            [table_aliases:protected] => Array
                (
                )

            [queried_terms] => Array
                (
                )

            [primary_table] => wp_posts
            [primary_id_column] => ID
        )

    [meta_query] => WP_Meta_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => 
            [meta_table] => 
            [meta_id_column] => 
            [primary_table] => 
            [primary_id_column] => 
            [table_aliases:protected] => Array
                (
                )

            [clauses:protected] => Array
                (
                )

            [has_or_relation:protected] => 
        )

    [date_query] => 
    [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = \'featured-article\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\')  ORDER BY wp_posts.post_date DESC LIMIT 0, 2
    [posts] => Array
        (
            [0] => WP_Post Object
                (
                    [ID] => 433
                    [post_author] => 1
                    [post_date] => 2017-09-16 19:13:24
                    [post_date_gmt] => 2017-09-16 13:13:24
                    [post_content] => dsfsf fsf werfwe frewgtv r j tut ttyuytu u tyu
                    [post_title] => DCV featured article
                    [post_excerpt] => 
                    [post_status] => publish
                    [comment_status] => closed
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => dcv-featured-article
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2017-09-16 19:13:24
                    [post_modified_gmt] => 2017-09-16 13:13:24
                    [post_content_filtered] => 
                    [post_parent] => 0
                    [guid] => http://rs-wordpress.com/?post_type=featured-article&p=433
                    [menu_order] => 0
                    [post_type] => featured-article
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [1] => WP_Post Object
                (
                    [ID] => 432
                    [post_author] => 1
                    [post_date] => 2017-09-16 18:49:05
                    [post_date_gmt] => 2017-09-16 12:49:05
                    [post_content] => chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon chloro-fluro carbon
                    [post_title] => cfc feature article
                    [post_excerpt] => 
                    [post_status] => publish
                    [comment_status] => closed
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => cfc-feature-article
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2017-09-16 18:49:05
                    [post_modified_gmt] => 2017-09-16 12:49:05
                    [post_content_filtered] => 
                    [post_parent] => 0
                    [guid] => http://rs-wordpress.com/?post_type=featured-article&p=432
                    [menu_order] => 0
                    [post_type] => featured-article
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

        )

    [post_count] => 2
    [current_post] => -1
    [in_the_loop] => 
    [post] => WP_Post Object
        (
            [ID] => 433
            [post_author] => 1
            [post_date] => 2017-09-16 19:13:24
            [post_date_gmt] => 2017-09-16 13:13:24
            [post_content] => dsfsf fsf werfwe frewgtv r j tut ttyuytu u tyu
            [post_title] => DCV featured article
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => dcv-featured-article
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2017-09-16 19:13:24
            [post_modified_gmt] => 2017-09-16 13:13:24
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://rs-wordpress.com/?post_type=featured-article&p=433
            [menu_order] => 0
            [post_type] => featured-article
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 3
    [max_num_pages] => 2
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 1
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_embed] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 1
    [query_vars_hash:WP_Query:private] => 8c6cb592fd6eb096f5db534a95e5b42c
    [query_vars_changed:WP_Query:private] => 
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
    [compat_fields:WP_Query:private] => Array
        (
            [0] => query_vars_hash
            [1] => query_vars_changed
        )

    [compat_methods:WP_Query:private] => Array
        (
            [0] => init_query_flags
            [1] => parse_tax_query
        )

)

2 个回复
最合适的回答,由SO网友:keramzyt 整理而成

您在语法上出错:

if ($featured_articles > have_posts()): while ($featured_articles > have_posts()) : $featured_articles->the_post();

ifwhile 条件应为-> 相反>.

SO网友:Jacob Peattie

the_post() 不渲染任何内容。阅读the documentation:

检索下一篇文章,设置文章,将“循环中”属性设置为true。

所以它所做的就是设置它the_title()the_content() 在查询中循环使用当前帖子的标题和内容。

如果帖子没有呈现,可能是因为get_template_part(\'template-parts/post/content\', get_post_format()); 引用的模板不存在。您需要确保模板存在并使用正确的函数,如the_title(), 输出内容。the_post() 不输出任何内容。

编辑:或者更可能是这样:

$featured_articles > have_posts()
这不是正确的语法。have_posts() 是上的方法$featured_articles 所以需要这样写:

$featured_articles->have_posts()

结束

相关推荐

插件放置在/wp-content/plugins内的文件夹中时不保存值

我得到了WordPRess插件的以下代码,它在每个页面/后期编辑屏幕上添加了两个自定义输入。然后将这些值保存并输出到前端页面的标题中。如果代码位于内部,则可以正常工作。php文件并直接放入“wp内容/插件”。然而,如果我把它放在插件(如“wp-content/plugins/myplugin”)中自己的文件夹中,那么在通过编辑屏幕保存帖子/页面时,输入字段不会保存。此外,它不会向前端页面html标题部分输出任何内容。这似乎是一个被放弃的项目,所以我无法与原始开发人员一起制定解决方案。然而,代码中的某些内容