douyi1899
2015-04-30 13:37
浏览 70
已采纳

在Wordpress上查询页面模板

I have a theme in Wordpress which use a code to show products of Woocommerce. However, I want to use the same function for showing also pages with a certain template. Here is what I have done until now:

<div>
    <?php
        $args = array(
            'post_type' => 'page',
            'meta_query' => array(
                array(
                     'key' => '_wp_page_template',
                     'value' => 'room-template.php'
                )
            )
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <div>
                <div>                       
                    <?php if(has_post_thumbnail()) { the_post_thumbnail(); }?>
                    <div>
                        <div>

                        </div>
                    </div>
                </div>
                <div><a href="<?php the_permalink(); ?>">Buy Now</a></div>
            </div>
        <?php endwhile;
        } else {
            echo __( 'No products found');
        }
        wp_reset_postdata();
    ?>
</div>

Whereas, I have 2 pages with this template, the result is "No products found". P.S. The divs are there because I use them for CSS classes. I've just removed the classed to make it easier to read.

Here is the output of the WP_Query:

WP_Query Object ( [query] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php )))[query_vars] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[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 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( )[category__not_in] => Array ( )[category__and] => Array ( )[post__in] => Array ( )[post__not_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 ( )[suppress_filters] => [ignore_sticky_posts] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC )[tax_query] => WP_Tax_Query Object ( [queries] => Array ( )[relation] => AND )[meta_query] => WP_Meta_Query Object ( [queries] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[relation] => AND )[date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id) WHERE 1=1 AND exitn_posts.post_type = 'page' AND ((exitn_posts.post_status = 'publish')) AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') ) GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10 [posts] => Array ( )[post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [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] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 436de9012210c4e9c717fd5edf527108 [query_vars_changed:WP_Query:private] => 1 [thumbnails_cached] => [stopwords:WP_Query:private] => )
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzou8074 2015-04-30 13:46
    已采纳

    You want to use get_template_part to print a specific template.

    https://developer.wordpress.org/reference/functions/get_template_part/

    If you want to use a specific function() within a template file then you might want to move this function to a common function library or your functions.php file.

    -- EDIT --

    SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID 
    FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id) 
    WHERE 1=1 AND exitn_posts.post_type = 'page' 
    AND ((exitn_posts.post_status = 'publish')) 
    AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') ) 
    GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10
    

    If executing this on the Database doesn't bring up the data it means either it doesn't exist or something within is missing. Its hard to tell this from my hand. I have pretty much the same pattern in one of my personal query and its Data is returned without any issues.

    Example :

    $args = array(
         'post_type' => explode( ',', $post_type ),
         'tag' => $tag,
         'p' => $id,
         'category_name' => $category,
         'posts_per_page' => $posts_per_page,
         'order' => $order,
         'orderby' => 'meta_value',
         'meta_key' => '_my_named_key',
         'meta_query' => array(
            array(
              'key' => '_my_named_key'
            )
         ),
         'no_found_rows' => 1
    );
    

    -- Working Edit --

    Make sure the value is a existing in the Database by looking it via something like phpMyAdmin or making a SQL Query onto it. This case was solved in chat full value had an unknown prefix page-templates/.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题