WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_title as category in query (8 posts)

  1. cerstrand_mace
    Member
    Posted 10 months ago #

    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()
    
             ) );
    ?>
  2. esmi
    Forum Moderator
    Posted 10 months ago #

    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);
    ?>
  3. cerstrand_mace
    Member
    Posted 10 months ago #

    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
  4. esmi
    Forum Moderator
    Posted 10 months ago #

    What is it that you are trying to do?.

  5. wspencer
    Member
    Posted 10 months ago #

    try using get_the_title()

  6. bcworkz
    Member
    Posted 10 months ago #

    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.

  7. esmi
    Forum Moderator
    Posted 10 months ago #

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

  8. cerstrand_mace
    Member
    Posted 10 months ago #

    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!

Reply

You must log in to post.

About this Topic