get_posts doesn't work but query_posts does (Wordpress) - wordpress

I'm using the following code to get posts with different types and categories assigned to them. The problem is that the main post of the page disappeared (the one you write in the Page section of the administrator menu).
I was reading the Wordpress documentation and they said that I should use get_post so that it wouldn't interfere with the main post of the page.
But everytime I change the all the query_posts to get_posts the posts don't appear:
<?php get_posts('category_name=Events&showposts=5'); ?>
page-events.php:
<?php
/**
* Template Name: Events Template
* #package WordPress
* #subpackage Twenty_Ten
* #since Twenty Ten 1.0
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php // find all content that has the category of Events and then to loop through them. ?>
<?php query_posts('category_name=Events&showposts=5'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<div id="container">
<div id="content" role="main">
<?php // find all content that has the type of video and then to loop through them. ?>
<?php query_posts(array('post_type'=>'video')); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

The main difference about query_posts() and get_posts() is that the first is intended to be used to modify the main page loop only and the latter is used for creating multiple custom loops.
So, in order to show the posts you may use get_posts() with its own custom loop. Example:
<?php
$customposts = get_posts('category_name=Events&showposts=5' ); // note: you assign your query to a custom post object ($customposts)
foreach( $customposts as $post ) : // start you custom loop
setup_postdata($post); ?>
// do your things...
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php the_content() ?>
....
<?php endforeach; ?> // end the custom loop
To preserve your original post (the one you inserted in the Edit panel for that page), you can code, after the main loop, two custom query loops with get_posts() just like the example above (you only have to change the query arguments for the latter).
Hope it helps.

Related

Wordpress search shows no results

I have one of those "wtf" problems again. I can't figure why my search form shows no result. See this webpage: http://sindlar.cz/exemplum/?lang=en
There is get_search_form() in the header. I also created searchform.php and search.php. See the search.php:
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav(); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
The searchpage is opened after clicking on the search button but there are no results. Maybe is there any problem with database or anything? Do you have any ideas? Should I add something into function.php?
I also noticed that there is difference between searching actual content of the websites and made-up words. For example if you type "About us" (which is actual page in my websites), it will show the search.php but no results on it. On the other hand if you type "Big truck" or "fsdgwdhs" (which I made up), it will send back other page than search.php. So I think that wordpress maybe know that there are some results but it does not show them. I do not know.
Could anyone help me?
Thanks!
Here is how to fix the search problem:
Via FTP/Cpanel go to wp-content - themes - theme name you are using - functions.
Open theme-functions.php in a text editor.
Replace with the code below
function gt_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','gt_search_filter');
*/
<section id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header>
<?php twentytwelve_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
</section><!-- #primary -->
put this code in search.php file with your class and CSS

How to properly use the <more> tag on pages in WordPress

According to the forum in this article, fixing the read more tag problem, Mark Pasay has the same difficulty as I do in using this code snippet as a solution. <?php global $more; $more = 0; ?> But the author does not elaborate on how to use this properly. I am using a page template that is called in the WP admin. The <!--more--> is added in the text editor. Here is the code for the page template.
/*
Template Name: new template
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="entry-content">
<?php global $more; $more = 0; ?>
<?php the_content('read more'); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #container -->
The tag shows up in the browser but when clicked, it does not show the full post.
On click, the URL changes from localhost/domain.com/page/ to localhost/domain.com/page/#more-481.
The author explains that this is not being used properly because it is calling content of pageX from pageX. "This trick will only work when you are trying to display excerpts of pageX on pageY." Fair enough, but how is this done?

Need help adding featured thumbnail to search.php in wordpress twenty-eleven theme

Hi I need help adding my thumbnail to the search results of my search.php page on WordPress as seen below.
I am kind of a newbie and do not want to mess up my code. I have already set up a child theme for this page so have no fear that I would alter the main theme.
Thumbnail support should already be turned on for this theme I just I want to add the thumbnail centered under the page title. Thanks
<section id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header>
<?php twentytwelve_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
</section><!-- #primary -->

Creating multiple pages with a loop of 1 category in wordpress

i need to make a loop for a single category.
Everything works fine, exept that i'm unable to split the page in multiple parts.
This is the LOOP of my page.
Output is fine, exept that navigation bar doesn't show, and simply adding to url a /page/2 it shows the first 2 posts.
<?php query_posts('cat=179&posts_per_page=2'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><?php the_title(); ?></h2>
<div class="entry-meta">
<?php morlottiTabs_posted_on(); ?>
</div>
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
<div class="entry-utility">
<?php morlottiTabs_posted_in(); ?>
<?php edit_post_link( __( 'Edit', 'morlottiTabs' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-utility -->
</div>
<?php endwhile; endif; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'morlottiTabs' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'morlottiTabs' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>
You can try to do it this way. There are some other methods listed for pagination as well.
Reference: http://digwp.com/2009/08/wordpress-page-navigation/
Reference: http://codex.wordpress.org/Class_Reference/WP_Query
Edit:
Would you want to try a different method?
Wordpress has their own method of pagination
Reference: http://codex.wordpress.org/Function_Reference/paginate_links
<?php echo paginate_links( $args ) ?>

the content function is ignoring more tag

I'm doing "page of post" in twenty eleven theme. Problem is that the_content function ignores more tag and show the whole post. It work just fine in index page.
My code:
/*
Template Name: Page Of Posts
*/
?>
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
$args= array(
'category_name' => '');
query_posts($args);
if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
Please help!
Can you list a file you are requesting with get_template_part('content', get_post_format() ) it should be in the theme folder named like content-gallery.php(or any other post_format).
Anyway, you can try the good old:
<?php global $more; $more = 0; ?>
Place it before the call for the content.

Resources