WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] using get_posts to grab from specific category (8 posts)

  1. RyanColins
    Member
    Posted 6 years ago #

    Now I know you can use get posts like this to get posts from a specific category:
    $posts = get_posts('category=3&numberposts=5');

    My problem is this, I want wordpress to grab posts whose category is the same as the title of the page. So the page is called "Books", it gets all posts from the category "Books". I know theres the php the_title(); that grabs the title of the page, can I use this in get_posts to somehow make this work?

    Is this at all possible? Any help or information would be appreciated.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    You can't do it using get_posts. But you could do it using the WP_Query object.

    $page-title = get_the_title();
    $myposts = new WP_Query('category_name='.$page-title);
    while($myposts->has_posts()) { $myposts->the_post();
    ... do all the normal loop here ...
    }

    Although it sounds to me like you're basically just wanting to make category archive pages, in which case I wonder why not just use the built in ones instead of making custom Pages for this purpose?

  3. RyanColins
    Member
    Posted 6 years ago #

    I'm using it for part of photo blog using yet-another-photoblog plugin.
    I'm using the code I am asking about to generate multiple image gallery pages for the photoblog. Each category the pictures are filled under will be a different gallery, thus I want it to only grab the posts that match the title of the page.

    I tried the code you gave me, its giving me a
    parse error: syntax error, unexpected '=' on line 19.

    Line 19 is the $page-title = get_the_title():

  4. RyanColins
    Member
    Posted 6 years ago #

    here, let me just make this easier and post the whole code:
    I am using a template that posts all the images in one gallery, ordering them by year. As I said before, I want to put make multiple galleries, putting images in separate pages, seperated by what category was.

    This is the original code for the template:

    if (have_posts()) : while (have_posts()) : the_post();
    
    ?>
    
    <div id="pagecontent">
      <h2><?the_title();?></h2>
      <?the_content('');?>
    <?php
    
    $posts = get_posts('numberposts=-1&order=DESC');
    
    $postyear = 0;
    
    foreach ($posts as $post) {
    	$this_postyear = intval(substr($post->post_date, 0, 4));
    
    	if ($this_postyear != $postyear) {
    		$postyear = $this_postyear;
    		echo '<h2 class="mosaicheader">'.$postyear.'</h2>';
    	}
    
    	$image = YapbImage::getInstanceFromDb($post->ID);
    
    	if ($image->width > $image->height) {
    		$thumb_param = array(
    			'sx='.intval(($image->width - $image->height)/2),
    			'sy=0',
    			'sw='.$image->height,
    			'sh='.$image->height
    		);
    	} elseif ($image->width < $image->height) {
    		$thumb_param = array(
    			'sx=0',
    			'sy='.intval(($image->height - $image->width)/2),
    			'sw='.$image->width,
    			'sh='.$image->width
    		);
    	} else {
    		$thumb_param = array();
    	}
    
    	array_push($thumb_param, 'h=100', 'q=70');
    	echo '<a href="'.get_permalink($post->ID).'">';
    	echo '<img class="mosaic" src="'.$image->getThumbnailHref($thumb_param).'" />';
    	echo '</a>';
    }
    
    ?>

    Now I get rid of the code that separates and order it by date, but because I don't want that and add your code and I get:

    if (have_posts()) : while (have_posts()) : the_post();
    
    ?>
    
    <div id="pagecontent">
      <h2><?the_title();?></h2>
      <?the_content('');?>
    <?php
    
    $page-title = get_the_title();
    $myposts = new WP_Query('category_name='.$page-title);
    while($myposts->has_posts()) { $myposts->the_post();
    ... do all the normal loop here ...
    }
    foreach ($posts as $post) {
    
    	$image = YapbImage::getInstanceFromDb($post->ID);
    
    	if ($image->width > $image->height) {
    		$thumb_param = array(
    			'sx='.intval(($image->width - $image->height)/2),
    			'sy=0',
    			'sw='.$image->height,
    			'sh='.$image->height
    		);
    	} elseif ($image->width < $image->height) {
    		$thumb_param = array(
    			'sx=0',
    			'sy='.intval(($image->height - $image->width)/2),
    			'sw='.$image->width,
    			'sh='.$image->width
    		);
    	} else {
    		$thumb_param = array();
    	}
    
    	array_push($thumb_param, 'h=100', 'q=70');
    	echo '<a href="'.get_permalink($post->ID).'">';
    	echo '<img class="mosaic" src="'.$image->getThumbnailHref($thumb_param).'" />';
    	echo '</a>';
    }
    
    ?>

    If I am screwing this up somewhere, and I can assure you I am as I am very new to php/wordpress, please let me know. Not only do I want to be able to get this to work, but I want to learn. Sometimes it all seems to overwhelm me though.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    I'm not going to rewrite your page for you. You'll have to do that yourself.

    And I used a dash instead of an underscore. My bad.

    $page_title = get_the_title();
    $myposts = new WP_Query('category_name='.$page_title);
    while($myposts->has_posts()) { $myposts->the_post();
    ... do all the normal loop here ...
    }
  6. RyanColins
    Member
    Posted 6 years ago #

    I don't expect you to write the page for me, just wanting to know how to implement this correctly.

    I'm getting a "Fatal error: Call to undefined method WP_Query::has_posts()" on line 21, line 21 being the "while($myposts->has_posts()) { $myposts->the_post();" line.
    Anyone mind telling me what this means and how I fix it?

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Dang. See what coding off the cuff gets you? ;)

    Try "have_posts" instead. :)

  8. RyanColins
    Member
    Posted 6 years ago #

    Yep! That fixes it! Thanks for putting up with my nonsense Otto.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags