Masonry style posts on another page

Hi!

I currently have masonry style posts pulling in specific categories on one page in my site. I’d like to duplicate that function on another page but pull in a different category of posts. Below is the code that I’m using in my child theme functions.php:

// =============================================================================
// Pull Specific Categories into Masonry Style Blog Index
// =============================================================================

function my_post_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with id=48
$query->set(‘cat’, 48);
}
}
add_action( ‘pre_get_posts’, ‘my_post_queries’ );

// END
// =============================================================================

I’m not sure what part of this calls out the page that it’s connected to. I’ll post the links where this appears now and to where I also want to use this style for you in a private message.

Would you be able to help me adjust this code to make that happen?

Thanks!

Hello There,

Thanks for writing in! All you need is an additional condition to be able to display the other content. You might use this:

// =============================================================================
// Pull Specific Categories into Masonry Style Blog Index
// =============================================================================

function my_post_queries( $query ) {
	// only homepage and is the main query
	if ($query->is_home() && $query->is_main_query()){
		// display only posts in category with id=48
		$query->set('cat', 48);
	}

	if ( $query->is_page('1') ) {
		// display only posts in category with id=10
		$query->set('cat', 10);
	}
}
add_action( 'pre_get_posts', 'my_post_queries' );

// END
// =============================================================================

Please make sure that you added the correct page ID and the category ID. As this is all custom development, regretfully we won’t be able to assist further. Custom development is outside the scope of our support. We’re happy to provide advice and get you started in the right direction, but you would still be responsible for the implementation.

Thank you for your understanding.

Thank you!

I actually got this code from one of your support team members a while back. Above and beyond as usual!

Thanks again.

You’re most welcome.

I am also trying to essentially create two separate blog pages. The first is not the main site page, but rather a page I’ve designated for my main blog. That works great! Now I’d like to make another page for a separate category of posts. I don’t mind using Wordpress’ standard “Category page” for the second one, and that works also… but those other category blog posts are also showing on my X-designated blog page. I’ve tried code like above in my child functions.php file, but they don’t seem to have any effect… I’d basically like to make it so that only “uncategorized” posts appear on the main blog page.

(It also appears to me that blog posts no longer have a category number, but rather a category name? Is that correct? I haven’t been able to find category numbers as I used to in the URL when selecting a category… )

Any assistance here would be very mych appreciated. I know anything extensive isn’t within the support, but this should be simple (not sure why it isn’t already built in to the X theme…) THANK YOU!

Hi there,

It’s all in the code, you can add more blocks, example

	if ( $query->is_page( 344 ) ) {
		// display only posts in category with id=10
		$query->set('cat', 17);
	}

Where 344 is the ID of your blog designated page, and 17 is the ID for your uncategorized category. If your designated blog page is also your blog main page, then what you need to change is the first one, example

	if ($query->is_home() && $query->is_main_query()){
		$query->set('cat', 17);
	}

The IDs are always there and there is no way it could be removed. Maybe you’re referring to permalinks, but in order to see that ID, you must edit your category or posts in admin then you’ll find the ID in the URL of the browser’s address bar. Example

wp-admin/term.php?taxonomy=category&tag_ID=231&post_type=post

Where 231 is the category ID.

Thanks.

So what am I doing wrong??

I have one post w/ category 23, all else are 1. #23 shows on the main blog page (268) and not on the alternate page (1256) … now the alternate page (1256) I understand is NOT a “blog” page, as it appears there can only be one in the X theme… and it is my designated,ed page “268”. I could change the “alternate” page to be a regular WordPress “Category” page, and then the new blogs will show up there, although not in masonry form (which is ok)… Problem is, those category 23 posts are ALSO showing up on my main blog page!! How can I screen them out? I must be missing something simple…

Hi,

Would you mind providing us with login credentials so we can take a closer look? Please provide following information:

Set it as Secure Note

  • Link to your site
  • WordPress Admin username / password
  • FTP credentials

All the best!

OK… here

Hello There,

Thanks for updating in! To exclude the posts under “Photo Tips” category, you will need to have this code:

function custom_x_post_queries( $query ) {
  
  // exclude Photo Tips category in the blog index
  if ( $query->is_home() ) {
    $query->set('cat', '-23');
  }
}
add_action( 'pre_get_posts', 'custom_x_post_queries' );

Please let us know if this works out for you.

Thank you very much!

Just for my understanding, how come it uses “is_home()” when I’m using a non-home page for my blog? Just curious!

ALSO… Is there any way in this theme to designate my Photo-Tips blog as an actual PAGE rather than a CATEGORY page? I couldn’t find a way to direct blog posts to any regular “second” page… I can live with the category page method, just would prefer to use an actual page so I can include a brief page description header, etc. . Thanks again!

Hello There,

Thanks for updating in! is_home() determines if the query is for the blog homepage. You can find out for more information from the codex:


https://codex.wordpress.org/Function_Reference/is_front_page

By default, the blog index will display all the recent post items regardless of the category in chronological order. Even if you exclude other categories, this is still your blog index and this is not “Photo Tips” category page. The Photo Tips category page can be found here: http://belkinphoto.com/category/photo-tips/

Hope this helps.