I have a single.php template but it shows all blogs posts. How do I prevent it from showing all posts:
Below is my code:
<?php $args = array('post_type' => 'realweddings');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="post-heading entry-title"><span class="left-hanger"><?php the_title(); ?> </span></h1>
<section class="overview">
<?php the_content(); ?>
</section>
<?php endif;?>
<?php comments_template(); ?>
</article>
<?php endwhile;?>
I think the correct question is, why am I using WP_Query to construct my loop in single.php. Frankly, I can't answer both.
Your problem is your custom query and the straight forward answer is, delete your custom query. You should never use a custom query in place of the main query. The main query is very specific on templates as it uses the URL to set the arguments in the main query, which is in fact also just a normal WP_Query
Just use the normal loop, that should fix your issue. A custom query is not the way to solve an issue with the main query
Related
Hi I am trying to making my static wordpress page seen here
Link to the posts that are shown. However they are just linking to the file directory in the browser and not the post.
It was working before where it would just link to the post in a wordpress theme but now that doesn't even work.
I haven't touch the code in a week I left it in the state as mentioned above, where it would link but just to the wordpress theme.
Here is the code I use to get the information to the static page from wordpress -
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<hr>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
Basically what I am trying to do is get it so when I click on the post it takes me to that post. Not to the file directory.
Any help is appreciated. Thank you.
I'm using xubuntu 16.04_LTS incase that has anything to do with compatibility in wordpress.
I have permalinks setup as shown
If all you want is the links to work . . .
http://auroraservers.org/MainSite/?p=38 ---fails
http://auroraservers.org/MainSite/wordpress/?p=38 ---works
That works and could be adjusted in the permalinks custom area. If you want that URL to be different, you're dealing with htaccess changes.
While not beautiful, you could replace the_permalink(); with
$id = get_the_ID();
$url = 'http://auroraservers.org/MainSite/wordpress/?p='.$id;
try to rename Index.php as index.php. It seems like Wordpress can't find index.php as a template for the post and therefore the system redirects you to the file directory since it can't find any of the possible template files.
Addition: Since your "Index.php" seems not to include code for the display of single posts, you might want to set up a file "single.php" as a template for single posts. But nevertheless, Wordpress will try to use index.php in many situations - always when the actual template file for a particular post type can't be found, like archive.php, page.php and many others...
So it might be a good idea to create a "regular", all-purpose index.php, rename your static page to something else, for example to front-page.php and define t as your starting page.
I seem to have fixed the issue I have been having. Thanks for all the replies.
Incase someone needs to know what I have done.
I went into mysql and dropped the database and recreated it. Must of gotten corrupted somehow.
So I hope that title isn't too confusing, let me try to break it down.
I have a content-page.php file which has both the get_content and get_sidebar functions in it. If I navigate to that page on the front end, I see both my content and sidebar.
On the home.php page, I'm loading a series of pages by using $the_query = new WP_Query and then inside that query using the loop and inside that loop, calling the content-page.php.
The problem is that if the page that loads on the home.php page has a sidebar, for some reason, nothing loads after the sidebar ie. get_content() returns nothing, comments_template() returns nothing, etc.
Here's a (very) simplified version of the markup, Homepage:
$the_query = new WP_Query( array(
'post_type' => 'page'));
$x = 0;
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'content', 'page' );
$x++;
endwhile;
wp_reset_query();
content-page.php:
<article <?php post_class(); ?>>
<header>
<h1><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php include( TEMPLATEPATH . '/sidebar.php'); ?>
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
sidebar.php:
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' );
dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary .widget-area -->
Is there something obvious that I'm missing here? Been working on this one for a while. As always, any help is greatly appreciated.
There are two types of general Loop queries in WordPress:
the main query, which is set based on the HTTP request, and
'sub loops' which can change the main query.
Since the main query controls many things like "is there a sidebar?" or "what page template should be used?" you can only submit one of these types. You can't call a separate sidebar (or call a separate page template for that matter) from within a sub loop. To do this, you would have to re-do the basic WordPress sidebar system.
I think the best bet for what you are trying to do, would be to just serve up some kind of dynamic content and call it a sidebar. In other words, WordPress has already decided what's happening with the sidebar the first time it was called. The system doesn't have a default way to handle calling the sidebar over and over. Using a shortcode or a filter after each iteration of the sub loop would be a better approach.
Close ")" open brackets.
$the_query = new WP_Query( array( 'post_type' => 'page') );
Paste this code in content-page.php
<?php get_template_part( 'sidebar' ); ?>
<?php the_content(); ?>
Try removing do_action( 'before_sidebar' );
You could try:
include( locate_template( 'content-page.php', false, false ) );
instead of:
get_template_part('content', 'page');
I have been working on creating a custom loop and setting how many posts per page. I needed to make this loop separate to the blog posts per page - hence the custom loop.
Now the loop does work, however I noticed it is conflicting with the pagniation... pagination is showing but pages are not changing when you click 'next'.
Here is my loop:
<?php
$args = array('posts_per_page'=>12, 'post_type' =>'office');
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="office-item-wrapper">
<div class="office-item">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
</article>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article><!--/ Article -->
<?php endif; ?>
Thanks in advance to anyone who might be able to help me solve this issue :-)
In WP, the pagination is sent by a GET variable under page, just add it to the query args:
$page = get_query_var('paged');
$args = array('posts_per_page'=>12, 'post_type' =>'office', 'paged'=>$page);
I recommend you to use get_query_var('paged') because if it's empty it will be send you to the first page.
Note: In some themes, the page number is sent in the page key, so try with page or paged.
Hope someone can help me, I've been struggling for days on this trying to find the answer...
Basically, I have a wordpress site that has a slider (not a plug-in just open source code) which is called to using a 'get_template' but it displays the same three posts on every single page. I have different posts in different categories and want the slider to correspond on each separate page and echo the posts from each particular category.
<div id="slidorion">
<div id="slider">
<?php
query_posts( 'posts_per_page=3' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="slide">"><?php the_post_thumbnail(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div id="accordion">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="link-header"><?php the_title(); ?></div>
<div class="link-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
here is a link to the site if you need to see it to totally understand what I mean and need to do...
http://www.kaijocreative.co.uk/footballnatter
Thanks!
You should alter your query adding cat or category to your query_posts( 'posts_per_page=3' ); according to what you exactly want
see Query_posts () and also have a look at WP_Query class
you need to use find out the category ids from each post, then use these ids in the
$category = get_the_category();
$post_catid= $category[0]->term_id;
$querystr='cat='.$post_catid.'&posts_per_page=3';
query_posts($querystr);
Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:
<?php
/* Template Name: Archives */
get_header(); ?>
<div id='content'>
<?php get_sidebar('left'); ?>
<div id='column2-wide'>
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<?php if ( in_category(16) ) { ?>
<h2><?php the_title(); ?></h2>
<div class="post">
<?php echo the_content(); ?>
</div>
<?php } ?>
<?php endwhile; endif; ?>
</div><!-- column2 -->
<?php get_footer(); ?>
Then created a page in the admin and chosen the Archives template to be used from the dropdown.
However the posts just dont seem to show. Am i missing something? The very same code works in the index.php file. It seems its just not working when im trying to display posts in a page.
It could well be im missing a file as I started developing the theme using a skeleton theme by Kennethreitz which can be found here:
https://github.com/kennethreitz/wordpress-theme-skeleton/
Any help would be appreciated.
Thanks for reading.
fl3x7
EDIT--> Also ive removed the category check so it should just list all posts but instead what it does is just echo the title of the current page if that helps
I'm assuming that "16" is the category ID? According to the WordPress docs for in_category, if you're querying by ID it should be passed as an integer.
$category
(mixed) (required) One or more categories specified by ID
(integer), name or slug (string), or an array of these
With your current code, the in_category check is failing every time since it is checking for an association with the category named "16." Try this instead:
if ( in_category(16) ) {
...
}
Thanks to the help provided by Hans. This is what I did:
query_posts( array ( 'category_name' => 'foo', 'posts_per_page' => 10 ) );
if (have_posts()): while (have_posts()): the_post(); ?>
<h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
<div class="post">
<?php echo the_excerpt(); ?>
</div>
<?php endwhile; endif; ?>