Support » Plugins » Hacks » putting a custom field value into my get_posts

  • Hello,

    I am making a site for a clothing store, that will post each item under categories, and then pages will have a custom field where you put in the category, and the category is then put into the following code — “get_posts( “category_name=…” . the idea is that there will be one template for pages and when the client makes a new page they pick which category goes in the page.

    Following is my php. At the moment the “<?php echo $value ?>” puts the correct info from the custom field. I just don’t know how to write the php to put it into the get_posts

    <?php $mykey_values = get_post_custom_values('cat'); foreach ( $mykey_values as $key => $value ) ?>
    <?php echo $value ?>
    <?php $posts = get_posts( "category_name=<?php echo $value ?>" ); ?>
    <?php if( $posts ) : ?>

    Obviously the last two lines are dog food, but that is basically the piece of php I can’t write. Can someone help? Just with what I put after category_name=”the php to echo the custom field value here”


Viewing 1 replies (of 1 total)
  • Since you’re using double quotes you can simply do it like this:

    <?php $posts = get_posts( "category_name=$value" ); ?>

    With single quotes it would be like this:

    <?php $posts = get_posts( 'category_name="' . $value . '"' ); ?>

    P.S.: You can not use PHP tags inside PHP tags.

Viewing 1 replies (of 1 total)
  • The topic ‘putting a custom field value into my get_posts’ is closed to new replies.