Support » Plugins » Hacks » the_title as category in query

  • Resolved cerstrand_mace

    (@cerstrand_mace)


    My question is rather simple. Is it possible to use the title of the current page as the category in a query array?

    I guess it would look somewhat like this but I haven’t got it to work:

    <?php
    		// The Query
    			query_posts(array( 
    
    				'post_type' => 'product',
    				'cat' => the_title()
    
             ) );
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi

    (@esmi)

    Forum Moderator

    That should be possible if you’re inside the main Loop when you run the second query. Something like:

    <?php
    $args= array(
    	'category_name' => $post->post_name,
    	'post_type' => 'product'
    );
    query_posts($args);
    ?>

    When I put the query inside the main loop the content won’t show at all. This is what I did, and it still won’t work:

    <?php
    					if ( have_posts() ) {
    						while ( have_posts() ) {
    
    					// The Query
    
    						$args= array(
    						'category_name' => $post->post_name,
    						'post_type' => 'product'
    					);
    					query_posts($args);
    
    				} // end while
    					} // end if
    esmi

    (@esmi)

    Forum Moderator

    What is it that you are trying to do?.

    try using get_the_title()

    Moderator bcworkz

    (@bcworkz)

    You are better off using $post->post_name (the post slug) instead of the actual title. The slug will always make a valid category term, the actual title could contain invalid characters for category terms.

    Like esmi, I’m not sure where you’re going with this, but to see anything from a call to query_posts() you need to run a second loop that outputs what you want to see. After running the loop, you must also call wp_reset_query() to restore the main query.

    esmi

    (@esmi)

    Forum Moderator

    Ideally, you should be using WP_Query() for a secondary loop – not query_posts().

    I realised that I went better of using the category system in wordpress. In this way the specific category page automaticly gets the name of the category which it is. I thank you all for the help anyway!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘the_title as category in query’ is closed to new replies.