WordPress.org

Ready to get started?Download WordPress

Forums

putting a custom field value into my get_posts (2 posts)

  1. russ.gd
    Member
    Posted 3 years ago #

    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"

    Thankyou.

  2. chaseman
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.