Closed pixelmostaza closed 11 years ago
What theme are you using?
WordPress automatically creates archive pages for categories, but if you want to display such posts wherever you can use a custom query:
$category_posts = new WP_query( array(
'posts_per_page' => 10, // How many posts to return
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'category-slug' // Or whatever your actual category slug is.
)
)
) );
if ( $category_posts->have_posts() :
while ( $category_posts->have_posts() ) : $category_posts->the_post();
// do post stuff
endwhile;
endif;
Hi, thank you for the reply. I'm using Inkblot as the base theme, but working with a child theme. So far the loop for the webcomic-homepage template seems to work differently than the single webcomic template.
I can tell because for some reason the widgets that show on a single webcomic page don't show up on the webcomic-homepage template.
If you just want to remove the webcomic's "blog," you can remove line 40 from Inkblot's webcomic/home.php
template. If you want to remove all posts from the homepage, you'll want to remove lines 50 - 65.
Alright, I'm almost done with this for the day! I got this template working, but there's a few things I would correct and then we'll have ourselves a custom template with a query that only pulls text posts from a specific category!
Please please pleaaaaaaaase!!!!!!! I beg youuuu! heeeelp!
So my code is this:
<?php
/**
* Template Name: Blog Page
*
* Custom query goodness.
*
* @package Inkblot
*/
get_header(); ?>
<?php $category_posts = new WP_query( array(
'posts_per_page' => 1, // How many posts to return
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'blog' // Or whatever your actual category slug is.
)
)
) ); ?>
<main role="main">
<?php if ( $category_posts->have_posts() and !is_paged() ) : ?>
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php if ( get_theme_mod( 'webcomic_content' ) ) : ?>
<div id="webcomic" class="post-webcomic" data-webcomic-shortcuts data-webcomic-gestures data-webcomic-container>
<?php get_template_part( 'webcomic/webcomic', get_post_type() ); ?>
</div><!-- .post-webcomic -->
<?php endif; ?>
<?php get_template_part( 'webcomic/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php elseif ( $webcomics and current_user_can( 'edit_posts' ) ) : ?>
<header class="page-header">
<h1><?php _e( 'No Webcomics', 'inkblot' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php printf( __( 'Ready to publish your first webcomic? <a href="%s">Start here »</a>', 'inkblot' ), add_query_arg( array( 'post_type' => get_theme_mod( 'webcomic_collection', 'webcomic1' ) ), admin_url( 'post-new.php' ) ) ); ?></p>
</div><!-- .page-content -->
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php inkblot_posts_nav( 'above' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php inkblot_posts_nav( 'below' ); ?>
<?php elseif ( current_user_can( 'edit_posts' ) ) : ?>
<header class="page-header">
<h1><?php _e( 'No Posts', 'inkblot' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php printf( __( 'Ready to publish your first post? <a href="%s">Start here »</a>', 'inkblot' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .page-content -->
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Awesome, I managed to correct my code into a working page with a custom loop! The widgets on the #content-header required me to simply make a custom header in my child theme and add the div per template.
The only thing this is missing is an actual navigation at the bottom. The nav it has was taken from the single.php file, meaning it only moves you one post up or down. Any help correcting that is much appreciated :)
/**
* Template Name: Blog Page
*
* Useful for creating a dedicated landing page for specific
* Webcomic collections.
*
* @package Inkblot
*/
get_header(); ?>
<?php $category_posts = new WP_query( array(
'posts_per_page' => 2, // How many posts to return
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'blog'
)
)
) ); ?>
<main role="main">
<?php if ( $category_posts->have_posts() and !is_paged() ) : ?>
<?php inkblot_posts_nav( 'above' ); ?>
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php inkblot_posts_nav( 'below' ); ?>
<?php elseif ( current_user_can( 'edit_posts' ) ) : ?>
<header class="page-header">
<h1><?php _e( 'No Posts', 'inkblot' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php printf( __( 'Ready to publish your first post? <a href="%s">Start here »</a>', 'inkblot' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .page-content -->
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<!-- Page Navigation -->
<nav class="posts"> //navigation only moves up and down one post at a time
<?php previous_post_link(); ?>
<?php next_post_link(); ?>
</nav>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>```
ALRIIIIGHT! Now I got it XD This template works if the page you put it on is used as the Posts page in Settings > Reading
So if you add the home template for comic posts you can have two separate loop templates: one for comics only and another for text posts :dancer:
I'll get working on theming my site and I'll show you how it looks like when it's done. Can't waiiiit!
/**
* Template Name: Blog Page
*
* Useful for creating a dedicated landing page for specific
* Webcomic collections.
*
* @package Inkblot
*/
get_header(); ?>
<?php $category_posts = new WP_query( array(
'posts_per_page' => 2, // How many posts to return
'orderby' => 'post_date',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'blog'
)
)
) ); ?>
<main role="main">
<?php if ( $category_posts->have_posts() and !is_paged() ) : ?>
<?php inkblot_posts_nav( 'above' ); ?>
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php inkblot_posts_nav( 'below' ); ?>
<?php elseif ( current_user_can( 'edit_posts' ) ) : ?>
<header class="page-header">
<h1><?php _e( 'No Posts', 'inkblot' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php printf( __( 'Ready to publish your first post? <a href="%s">Start here »</a>', 'inkblot' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .page-content -->
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>```
Congrats! I look forward to seeing it.
Hi, I'm almost about to finish styling everything, but on the last test I came up with this issue.
Basically, the code for the loop on this template pulls posts from all categories as opposed to only one. What could be causing this?
This is what I have so far:
'orderby' => 'post_date',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'blog'
)
)
) ); ?>
<main role="main">
<?php if ( $category_posts->have_posts()) : ?>
<?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>```
Any ideas on why it wouldn't just filter the query to posts under the "blog" category?
Update: Alright, problem solved. Basically, if I put the template on a page, and then set that page to show blog posts in the Settings > Reading options, the custom query is ignored by Wordpress and it shows posts from all categories.
I removed the page from the Reading settings and now the custom query is respected.
This is the weirdest set of things I've seen working with Wordpress. Ever.
Hey there, me again. XD
I'm looking to have a homepage and single webcomic pages that don't have any blog info. So far I've managed to do this by removing some code from the webcomic-content template, however this only affects single webcomic pages and the homepage template is a bit trickier for me. Is there a way to have that homepage template show no blog entries?
Additionally, how would I go about creating a template that only queries posts from a specific category (i.e. only text posts for the blog)?
Any assistance you can provide will be awesome!
Luis