show custom posts on front page - php

I have set a special category for posts that I want to show on the front page here: http://aquadiva.it/en/
some of them won't show though. there should be four.
I suspect the problem lies in the fact that two of the four posts are custom posts. The category itself works, because, if I search for category, what it gives is those four posts: http://aquadiva.it/en/category/frontpage/
while on the front page I have only the ones belonging to "regular" posts (articles) and not to custom posts (products).
this is my content-frontpage.php:
<?php
$nirvanas = nirvana_get_theme_options();
foreach ($nirvanas as $key => $value) { ${"$key"} = $value; } ?>
<section id="container" class="one-column <?php //echo nirvana_get_layout_class(); ?>">
<div id="content" role="main">
<?php //cryout_before_content_hook();
$nirvana_old_posts_per_page = get_option( 'posts_per_page' );
if ( have_posts() ) :
/* Start the Loop */
update_option( 'posts_per_page', $nirvanas['nirvana_frontpostscount']);
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
while ( $the_query->have_posts() ) : $the_query->the_post();
global $more; $more=0;
get_template_part( 'content/content', get_post_format() );
endwhile;
if($nirvana_pagination=="Enable") nirvana_pagination($the_query->max_num_pages); else nirvana_content_nav( 'nav-below' );
else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No Posts to Display', 'nirvana' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php printf(
__( 'You currently have no published posts. To hide this message either add some posts or disable displaying posts on the Presentation Page in theme settings.', 'nirvana' ),
esc_url( admin_url()."post-new.php"),
esc_url( admin_url()."themes.php?page=nirvana-page") ); ?>
</p>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php
endif;
update_option( 'posts_per_page', $nirvana_old_posts_per_page);
//cryout_after_content_hook();
?>
</div><!-- #content -->
<?php //nirvana_get_sidebar(); ?>
</section><!-- #container -->
what can I do?

Exchange this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged) );
with this:
$the_query = new WP_Query( array('posts_per_page'=>$nirvanas['nirvana_frontpostscount'],'paged'=> $paged, 'category_name'=>'frontpage', 'post_type' => array('post','product')) );
Let me know if this works. If not, let me know what the name of your custom post type is.

Related

Wordpress Search results separate main posts from Custom Post Type

I have a custom post type named 'Artists'.
My current search results are pulling this post type along with my standard posts in one grouping.
I am having trouble separating the two posts types in my results.
Instead of...
Blog Post
Blog Post
Artist Post
Blog Post
Artist Post
I'd like....
BLOG POSTS:
Blog Post
Blog Post
Blog Post
ARTIST POSTS:
Artist Post
Artist Post
Is that possible? I am not sure if I can solve this with one loop/query or it would be faster to use two queries?
Current code is here...
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php the_date(); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
</article><!-- #post-## -->
Any help appreciated. I realize there are some similar questions on this already, but most have been unhelpful for this particular situation.
Just change the order of the wp_query object
$args = array(
'order' => 'type'
);
$query = new WP_Query( $args );
OP is to lazy to read the Docs so here i provide an example:
$args = array('order' => 'type');
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) {
//
}
EDIT:
Here another possible solution if you want to avoid a new Instance of wp-query and use the global Object:
add_filter('posts_orderby','my_sort_custom',10,2);
function my_sort_custom( $orderby, $query ){
global $wpdb;
if(!is_admin() && is_search())
$orderby = $wpdb->prefix."posts.post_type ASC"
return $orderby;
}

Wordpress Custom Page, posts not displaying

Hi i'm trying to create a custom page (custom-page.php) that contains some of my blog posts, but when I test the loop i'm not seeing any posts. Its only returning the name of my custom page template as the h2.
I have two posts already published but they are not showing up.
I have a front-page.php which users land on when they first come to my site and I have not changed any settings under the reading tab.
I've read over the Wordpress codex and can't find any solutions so far.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Please follow this code.
$newsQuery = newWP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) {
while ($newsQuery->have_posts()) {
$newsQuery->the_post();
echo get_the_title();
echo get_the_excerpt();
}
}
and Your complete template will be like this.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php
$newsQuery = new WP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) : while ( $newsQuery->have_posts() ) : $newsQuery->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
Create a page named "Blog" from wp admin and then create a file in theme folder named page-blog.php and write the following code below.
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE</h1>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'id',
'order' => 'desc'
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'offset' => 0
);
$the_query1 = new WP_Query( $args );
if (count($the_query1->posts)>0) {
while ( $the_query1->have_posts() ) : $the_query1->the_post();
get_template_part( 'loop-archive-template-location' );
endwhile;
}
?>
As we mentioned, WP_Query is a PHP class used by the WordPress database. This particular class can do several things, but primarily it’s used to pull posts from the database.
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
// wp_reset_postdata();
here are two main scenarios you might want to use WP_Query in. The first is to find out what type of request WordPress is currently dealing with. The $is_* properties are designed to hold this information: use the Conditional Tags to interact here. This is the more common scenario to plugin writers (the second normally applies to theme writers).
The second is during The Loop. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show.

Pagination on custom Wordpress template

I have problem to change content on page when for ex. url change from
/?page_id=55&paged=1 to /?page_id=55&paged=2...
So I have problem to display next/previous page content, it just show first 4 post on page and when I click next nothing hapend with content just url change.
Here is my blog template:
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div class="row main-part">
<?php
$args = array(
'category_name' => 'blog',
'paged' => $paged,
'posts_per_page' => 4
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="col-xs-12 col-md-3">
<h1><?php the_title(); ?></h1>
<p>
<?php the_excerpt(); ?>
</p>
<p><a class="btn btn-primary" href="<?php the_permalink() ?>" role="button">View details »</a></p>
</div>
<?php endwhile;endif; ?>
<?php
wp_reset_postdata();
wp_reset_query();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
<?php get_footer(); ?>

Next post link going to not found on search page template

I m using wordpress 4.0 and I have create a custom search page template successfully but the problem is that the pagination link next is going to not found what should i do Please Guide Me....
here is my code
<div class="blog-post">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'ignore_sticky_posts' => 1, 'paged' => $paged );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog-span"><?php /* Start the Loop */ ?>
<?php get_template_part( 'content-search', get_post_format() ); ?>
<div class="space-sep20"></div>
</div>
<?php endwhile; ?>
<div class="nav-previous alignleft my-link"><?php previous_posts_link('« Previous');?></div>
<div class="nav-next alignright my-link"><?php next_posts_link( 'Next »', $the_query->max_num_pages ); ?></div>
<?php else : ?>
<div class="blog-span">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'weblizar' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'weblizar' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
<?php endif; ?>
</div>
Preserving Search Page Results and Pagination
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
?>
for more help please refer this link
Create search template
and serch for this title "Preserving Search Page Results and Pagination"

Wordpress query_posts('category_name='.get_the_title(). won't return posts for category with spaces

I have manipulated a wordpress "blog" template to show category specific posts by using the page title as reference. That way I can create a 'News' page and it auto populates the page with all the 'News' Category posts.
This code works perfectly as written on single word Titles, however it doesn't call any posts on multi word titles. ie: 'In The News'. The page doesn't break, it just says "Sorry, no posts matched your criteria".
Any thoughts?
<?php
/**
* Template Name: Category Posts Ultimate Template
*
* This template is the default page template. It is used to display content when someone is viewing a
* singular view of a page ('page' post_type) unless another page template overrules this one.
* #link http://codex.wordpress.org/Pages
*
* #package WooFramework
* #subpackage Template
*/
get_header();
global $woo_options;
/**
* The Variables
*
* Setup default variables, overriding them if the "Theme Options" have been saved.
*/
$settings = array(
'thumb_w' => 505,
'thumb_h' => 284,
'thumb_align' => 'alignleft',
'post_content' => 'excerpt'
);
$settings = woo_get_dynamic_values( $settings );
?>
<!-- #content Starts -->
<div id="content" class="page col-full">
<?php woo_main_before(); ?>
<section id="main" class="col-left">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php woo_loop_before(); ?>
<!-- Does the blog need pagination? -->
<?php
if ( get_query_var( 'paged') ) { $paged = get_query_var( 'paged' ); } elseif ( get_query_var( 'page') ) { $paged = get_query_var( 'page' ); } else { $paged = 1; }
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
remove_filter( 'pre_get_posts', 'woo_exclude_categories_homepage' );
// The Query
query_posts('category_name='.get_the_title().'&post_status=publish,future&paged=' . get_query_var('paged'));
if (have_posts()) {
$count = 0;
while (have_posts()) { the_post(); $count++;
?>
<!-- Post Starts -->
<article <?php post_class(); ?>>
<header>
<h1><?php the_title(); ?></h1>
</header>
<?php woo_post_meta(); ?>
<section class="entry fix">
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] != 'content' ) { woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); } ?>
<?php global $more; $more = 0; ?>
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'content' ) { the_content(__( 'Read More...', 'woothemes' ) ); } else { the_excerpt(); } ?>
</section>
<footer class="post-more">
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'excerpt' ) { ?>
<span class="read-more"><?php _e( 'Continue Reading', 'woothemes' ); ?></span>
<?php } ?>
</footer>
</article><!-- /.post -->
<?php
} // End WHILE Loop
} else {
?>
<article <?php post_class(); ?>>
<p><?php _e( 'Sorry, no posts matched your criteria.', 'woothemes' ); ?></p>
</article><!-- /.post -->
<?php } // End IF Statement ?>
<?php woo_loop_after(); ?>
<?php woo_pagenav(); ?>
<?php wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>
</section><!-- /#main -->
<?php woo_main_after(); ?>
<?php get_sidebar(); ?>
</div><!-- /#content -->
<?php get_footer(); ?>
It's not working because you can't have a space in your query. Instead of getting posts by category name, it would be better to use something like the category slug. This would not have any spaces in it and for something like 'In The News' it would probably be 'in-the-news'.
This will definitely depend on how your category slugs are named in comparison to the titles, but it's something that I have used with success in the past. So before executing your query we need to take the title and convert it to a new 'slug' that matches a category:
// Replace any spaces in the title with dashes
$title_slug = strtolower(str_replace( " ", "-" , the_title_attribute('echo=0') ) );
// Get the category object by using our new title slug
$id_obj = get_category_by_slug($title_slug);
// Get the category id so that we can use that in the query
$cat_id = $id_obj->term_id;
Then you would just change your query_posts to use the category id instead of the name:
query_posts( 'cat='.$cat_id.'&post_status=publish,future&paged='.get_query_var('paged') );
Again, this may or may not require a little tweaking of your title names and/or category slugs, but should work for you!

Categories