• Resolved Ali Dark

    (@ali-dark)


    Hi everyone.

    I’m trying to load up the category in the loop using post meta data.

    As I understand it (I’m a hack mind you) the get_post_meta function below is set to return a single value, not an array (the boolean “true”).


    <?php $cat = get_post_meta($post->ID, $catslug, true); ?>
    <?php query_posts('category_name=' . $cat ); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile;?>

    But when I print the variables I get “array” as the result for both, and no loop output. (this is how I printed the variables, right after the loop).

    <p>catslug = <?php echo get_post_meta($post->ID, $catslug, true ); ?></p>
    <p>cat = <?php echo $cat; ?></p>

    All this is happening on a page, after the normal page loop. In this case there are two meta datas:
    catslug: “mtgravatt”
    menu: “Mount Gravatt”

    Funny thing was, I swear this was working. Then I went and changed the category slugs and updated the custom fields, and I started getting the array results.

    Cache cleared, still the same.

    Ali

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Ali Dark

    (@ali-dark)

    By the way, I should mention that I know the loops working as it should – if I replace
    <?php query_posts('category_name=' . $cat ); ?>
    with
    <?php query_posts('category_name=mtgravatt' ); ?>
    It outputs the titles of posts in the category it should.

    Thread Starter Ali Dark

    (@ali-dark)

    Thanks to Paul Robinson of return-true.com who helped me work out the following working code. It turns out the variables were not properly defined.

    <?php
    $catslug = get_post_meta($post->ID, 'catslug', TRUE);
    $cat = get_post_meta($post->ID, catslug, TRUE); ?>

    <?php query_posts( 'category_name=' . $cat ); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h3><?php the_title(); ?></h3>
    <?php endwhile;?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘trouble with array returns from get_post_meta’ is closed to new replies.