我正在尝试显示具有特定值的自定义字段的所有页面。我想做的是使用当前页面标题作为自定义字段的值,并获取定义了该值的所有页面。
例如:
当前页面为“Surgery“自定义字段名为”Section“自定义字段值为”Surgery“我已经能够使用以下代码对post进行处理,但我似乎无法解决如何处理页面。
<?php woo_loop_before(); ?>
<?php
    if ( get_query_var( \'paged\') ) { 
        $paged = get_query_var( \'paged\' ); 
    } elseif ( get_query_var( \'page\') ) { 
        $paged = get_query_var( \'page\' ); 
    } else { 
        $paged = 1; 
    }
    $query_args = array(
                        \'post_type\' => \'post\', 
                        \'paged\' => $paged
                    );
    // Do not remove. Used to exclude categories from displaying here.
    $query_args = apply_filters( \'\', $query_args ); 
    remove_filter( \'pre_get_posts\', \'woo_exclude_categories_homepage\' );
    query_posts( $query_args );
query_posts(\'category_name=\'.get_the_title().\'&post_status=publish,future\');    
    if ( have_posts() ) {
        $count = 0;
        while ( have_posts() ) { the_post(); $count++;
            /* Include the Post-Format-specific template for the content.
             * If you want to overload this in a child theme then include a file
             * called content-___.php (where ___ is the Post Format name) 
             * and that will be used instead.
             */
            get_template_part( \'content\', get_post_format() );
        } // End WHILE Loop
    } else {
?>
    <article <?php post_class(); ?>>
        <p><?php _e( \'Sorry, no posts matched your criteria.\', \'woothemes\' ); ?></p>
    </article><!-- /.post -->
<?php } // End IF Statement ?> 
<?php woo_loop_after(); ?>
 
                SO网友:Mike Madern
                您可以使用get_pages(), 在参数中,可以提供搜索查询post_meta, 例如:
$args = array(
    \'post_type\'  => \'page\',
    \'meta_key\'   => \'section\',
    \'meta_query\' => array( array(
        \'key\'     => \'section\'
        \'value\'   => \'surgery\' // here you could place get_the_title()
        \'compare\' => \'=\'
    ) )
);
$pages = get_pages( $args );
 这将返回中的所有页面(或任何其他帖子类型
post_type) 使用meta\\u key section和meta\\u value手术。
如果使用这种方式,则必须更改循环,请按以下方式构建循环:
foreach( $pages as $page ) : setup_postdata( $page );
...
endforeach; wp_reset_postdata();
 希望这对你有帮助。