Search code examples
phpwordpresscustom-fieldsposts

How do I add an echo inside a query?


I'm trying to show posts from a certain category on a page like his:

<?php query_posts('category_name=category1'); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

This works. However, I'd like 'category1' to be taken from a custom field of the page. Something like this (incorrect code incoming):

<?php query_posts('category_name=get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>'); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

How could I make this possible?


Solution

  • Use it in this way:

    $custom_cat_name = get_post_meta(get_the_ID(), 'custom_cat_name', TRUE);
    query_posts('category_name=' . $custom_cat_name);
    

    Hope this helps!