简体   繁体   中英

Wordpress: Query 'category_name' given in page custom field

I'd like to query posts from a 'category_name' indicated in the page's custom fields. In other words, can I populate the query below with a custom field value?

  <?php query_posts( array ( 'category_name' => '[from custom field]', 'posts_per_page' => -1 ) ); ?>
  <?php while (have_posts()) : the_post(); ?>
    ...
  <?php endwhile; ?>

Does anyone know how I'd go about doing this? Any help would be appreciated. Thanks.

If I've understood you correctly, something like this should work:

<?php 
$category_name = get_post_meta( $post->ID, 'custom_field_name', true );
query_posts( array ( 'category_name' => $category_name, 'posts_per_page' => -1 ) );
while (have_posts()) : the_post(); ?>
    ...
<?php endwhile; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM