Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I can't find a solutions in internet or advance custom field website docs about this. I hope wordpress genius can help me.

I use http://wordpress.org/extend/plugins/advanced-custom-fields/ for themes options/function. I want to show category post in homepage using category name. here is my code:

<!-- Start Category Posts -->
<div class="boxtype2">
    <div class="boxtitle1"><?php the_field('cat_1'); ?></div>
    <div class="boxposts">
    <?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?>
    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
        <!-- article -->
        <ul class="boxul">
        <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <br class="clear">
        </li>
        </ul>
        <!-- /article -->
    <?php endwhile; ?>
    <?php else: ?>
        <!-- article -->
        <article>
            <h2><?php _e( 'Sorry, nothing to display.', 'fileketchup' ); ?></h2>
        </article>
        <!-- /article -->
    <?php endif; ?>
    <?php wp_reset_query() ?>
    </div>
    <div class="boxfooter"><a href="<?php echo home_url(); ?>/<?php the_field('view_cat1'); ?>">View more</a></div>
</div>
<!-- End Category Posts -->

Please check <?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?> this...

Here I use advance custom field meta insert of category name.. but its show error... How I solve this?? If I use ('category_name=blog','showposts=5') then its works.

share|improve this question

closed as off-topic by s_ha_dum, G. M., Wyck, t f, toscho Dec 29 '13 at 23:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – s_ha_dum, G. M., t f, toscho
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Your syntax is wrong. Specifically, that is not how you concatenate a string in PHP. –  s_ha_dum Dec 29 '13 at 15:30
    
please give me correct syntax. –  NoDiv_NoClass Dec 29 '13 at 15:50
    
Avoid using query_posts, use WP_Query instead, read this When should you use WP_Query vs query_posts() vs get_posts()? for more information on that. –  ialocin Dec 29 '13 at 16:02

2 Answers 2

up vote 0 down vote accepted

When you do this:

<?php query_posts('category_name=<?php the_field('cat_1_name'); ?>','showposts=5'); ?>

The string argument is literally category_name=<?php the_field('cat_1_name'); ?>','showposts=5. Well, it would be if the string were not also triggering a fatal error.

What you have is (assuming I haven't lost track of the mess):

  • String 1: 'category_name=<?php the_field('cat_1_name'); ?>','showposts=5'
  • An undefined constant: cat_1_name
  • An out of place comma, making what follows a second argument to a function that only takes one argument: ,
  • And String 2: 'showposts=5'

That is never going to work. If you are going to be writing code you need to learn to create strings in PHP. That has got to be in the first five things you should learn to do well.

Secondly, you are not in HTML context so you do not need to be using PHP open and close tags at all, and doing so will trigger further fatal errors.

Third, you are mixing up array syntax with the hard-to-read-and-hard-to-edit-and-prone-to-error query-string-like syntax that is mysteriously popular in WordPress.

Fourth, AFC's the_field echos content. You can't use that to concatenate a string anyway. You need get_field.

Fifth, showposts has been deprecated for a very long time. Use posts_per_page.

Strings that should work:

"category_name=".get_field('cat_1_name')."&posts_per_page=5" 'category_name='.get_field('cat_1_name').'&posts_per_page=5'

But don't use that query-string syntax, create an array.

$args = array(
  'category_name' => get_field('cat_1_name'),
  'posts_per_page' => 5
);

And finally, please don't use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

share|improve this answer
    
Thanks a lot. It's works. –  NoDiv_NoClass Dec 29 '13 at 17:36

As s_ha_dum said, your syntax is wrong. You should work like this:

'key' => 'value',

Try modifying your query_posts loop to:

to:

$args = array(
    'category_name' => 'cat_1_name',
    'posts_per_page' => 5);
$my_query = new WP_Query( $args );
// The Loop
if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
            $my_query->the_post(); ?>
    <!-- article -->
    <ul class="boxul">
        <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <br class="clear">
        </li>
    </ul>
    <!-- /article -->
<?php endwhile;
} else {
    ?>
    <!-- article -->
        <article>
            <h2><?php _e( 'Sorry, nothing to display.', 'fileketchup' ); ?></h2>
        </article>
        <!-- /article -->
    <?php }
    /* Restore original Post Data */
wp_reset_postdata(); ?>

If you need your category name to be from your custom field, you can set it as a variable, and call that variable as the value for 'category_name'. Something like this:

$category = get_field('cat_1_name');

$args = array(
    'category_name' => $category,
    'posts_per_page' => 5);

EDIT: I edited because the above comments are spot on. You've no need to use query_posts. If you need more $args for your WP_Query, go read up on it!

share|improve this answer
    
Thanks for show me in details. You are really great. –  NoDiv_NoClass Dec 29 '13 at 17:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.