Wordpress is showing paginated pages that doesn't exist - pagination

I have a page template where I run a WP_Query
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
The issue I'm facing is that if you go to the site /page/100 it will show the template and it's not going to a 404 when It should.
My blog page under reading settings is another page and this is a custom template I'm doing.
I have read this post https://wordpress.stackexchange.com/questions/46116/non-existing-blog-pages-are-not-redirected-to-404 and tried all the functions and none of them work.
I also spent 3 hours searching on google without being able to find a workaround.

You are creating a new WP Query. So I think you have to reset the wp_postdata.
WP Codex offers sample code. You could try something like
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged);
if( ($wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// Do here something with the post
endwhile;
wp_reset_postdata(); // At the end reset your query
endif;

While using WP_Query for displaying posts list you need to check by yourself if they are any posts for current page. Below is example code from Wordpress docs
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Replace " _e('Sorry, no posts matched your criteria.'); "
with own error or function displaying 404 error.
Edit: Above code is of course example for Main Loop. In your particular case you need to call $wp_query->have_posts() and make sure it not evaluates to false. If it does display 404 error.
for example:
if(!$wp_query->have_posts()) {
echo "404";
}
Or if you want server respond with HTTP code 404 instead of 200 then you should try something like this:
if(!$wp_query->have_posts()) {
status_header(404);
nocache_headers();
include( get_404_template() );
exit;
}

Related

how to check product has custom options?

I'm trying to check whether product has custom options or not in code (my code runs sales_order_place_after event).
I have try below code but it does not returning anything.
$product->hasCustomOptions()
and
$product->hasOptions()
Please let me know what I'm missing.
I've encountered this error more times than I care to count. Either $_product->hasOptions() or $_product->hasCustomOptions() always returns false. I still don't know why this error occurs.
Anyway, you can get the same result by doing the following. For configurable products:
<?php if ( $_product->getData('has_options') ): ?>
<!-- do something -->
<?php endif; ?>
And to get the same result for simple products with custom options:
<?php if ( $_product->getData('has_options') && ($_product->getTypeID() == 'simple') ): ?>
<!-- do something -->
<?php endif; ?>
I hope that helps a future adventurer!
EDIT
The solution above does not work in loops when the flat category data option is enabled in Magento, and we don't want to reload the product inside the foreach loop!!
Instead, we can check for custom options using the following singleton inside the loop:
$opts = Mage::getSingleton('catalog/product_option')->getProductOptionCollection($_product);
$optsSize = $opts->getSize();
if ( $optsSize ) {
... // go go go
}
use the method $product->getHasOptions()

Pagination page/2 not found for a category but works for tags?

This drives me nuts, i am trying to list post based on a category an on a tag. Now for tag i have got it working. Using archive.php but for the category, using categroy.php, it doesn't work for page/2???.
I have already check the permalinks and indeed if i use the default settings both mydomain.com/blog/page/2 and mydomain.com/tag/green/page/2 work. But turning on /%category%/%postname%/ and the mydomain.com/blog/page/2 gives a page not found???
Here's the main part of category.php (which is the same as archive.php)
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'posts_per_page' => 3,
'paged' => $paged
));
while (have_posts()) : the_post();
// Include the page content template.
get_template_part('partials/content', 'loop');
// End the loop.
endwhile;
?>
<!-- Pagination -->
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php if ($wp_query->max_num_pages > 1) : $paged = intval(get_query_var('paged')); ?>
<div class="older"><?php next_posts_link(__('Older entries', 0)); ?></div>
<!--test this one-->
<?php if ($paged > 1) : ?>
<div class="newer"><?php previous_posts_link(__('Newer entries', 0)); ?></div>
<?php endif;
endif;
?>
<!-- End Pagination -->
The codex contains the following important note on query_posts:
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_postshook, for this purpose.
If you follow that instruction, you should be able to fix your problem yourself

disable layout for particular pages in ZF2

How to disable particular layout(example:menus.phtml) for particular pages in controller in ZF2?? In the below example menus.phtml should be disable for specific pages. Remaining pages must contain menus.phtml like header and footer.
<div>
header.phtml
</div>
<div>
menus.phtml
</div>
<div>
<?php echo $this->content; ?>
</div>
<div>
footer.phtml
</div>
There are various aproaches to this. Also modules.zendframework has quite a few modules here that may help you out.
If you are still keen on writing that yourself you could add variables to your layout within your controllers like so:
<?php
//YourController.php
public function someAction()
{
...
$this->layout()->footer = 'default';
...
}
//layout.phtml
<?php if ($this->footer === 'default') : ?>
//show the footer
<?php endif; ?>
Doing this is pretty inefficient though. Just imagine you'd need to do this to every action in all the controllers... I sure would not like to do that.
Now zf2 has a service and event layer that could help us out quite a bit here. This is a pretty nice read and introduction to it. You'd just write a service and trigger a event on your controllers/routes/whatever. Now you would also probably like to configure what is shown and what is hidden right? Thats pretty easy, too. Just write yourself a config file and merge it with the global.config like so:
<?php
//CustomModule/module.php
public function getConfig() {
$config = array();
$configFiles = array(
include __DIR__ . '/config/module.config.php',
include __DIR__ . '/config/module.customconfig.php',
);
foreach ($configFiles as $file) {
$config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
}
return $config;
}
Source: Where to put custom settings in Zend Framework 2?
First, get the controller or action name:
$controllerName =$this->params('controller');
$actionName = $this->params('action');
then in your layout/view script add a simple logic.
<?php if ($actionName != 'action that you want to disable the layout/menu'): ?>
echo $this->render('menus.phtml');
<?php endif; ?>

How to display content from a child page within a hierarchial custom post type?

I have a hierarchical custom post type. It has 6 pages, and each page has 3 child pages.
When viewing one of the 6 pages, I need to display content (a title and excerpt) from each of its 3 child/descendent pages.
Here is my current loop:
<?php if(have_posts()):?>
<?php query_posts('&post_type=how-we-do-it&post_parent=0');?>
<?php while(have_posts()):the_post();?>
<?php $color = get_post_meta( get_the_ID(), 'pointb_how-we-do-it-color', true ); ?>
<div class="section">
<div class="title">
<h1 style="background:<?php echo $color;?> !important;">
<?php the_title();?>
</h1>
</div>
<div class="content">
<div class="how-<?php the_slug();?>">the summary here. and here is child content:
<div class="child">child content should be here.</div>
</div>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php endif;?>
I have tried numerous different approaches to try and accomplish what I need, but none of them work within the custom post type. Here are some of the various methods I have tried:
I tried the suggested code on this page: http://wordpress.org/support/topic/display-child-pages-title-amp-content-on-parent-page
I also tried the following code:
$pageChildren = get_pages('child_of='.$post->ID');
if ( $pageChildren ) {
foreach ( $pageChildren as $pageChild ) {
echo '<h2>'. $pageChild->post_title.'</h2>
';
if ($pageChild->post_excerpt){
echo ''.$pageChild->post_excerpt.'
';
}
}
}
I've tried a number of other methods that I didn't bother saving, so I can't show them.
I'm at the point where I am getting frustrated with this and thought I'd throw it out here to get some fresh perspectives.
The issue with your first sample is that you call if(have_posts()) before you reconstruct the query.
The second sample has a dangling ' after $post->ID.
Try this:
$pageChildren = get_posts( 'post_type=how-we-do-it&post_parent='.$post->ID );
Based on some comments from MarZab above, I got thinking about the post ID.
I made the following tweak to the block of code I originally posted above, and now it works perfectly:
$pageChildren = get_pages('child_of='.$post->ID');
Is now:
$post_id = get_the_ID();
$pageChildren = get_posts( 'post_type=how-we-do-it&echo=0&post_parent='.$post_id );

wordpress pagination links problems

I'm trying to implement pagination in a custom WP-Query. The pagination is done correctly, but the links are wrong. If I have, for example, 10 posts and 8 posts per page, I have a page 1 with 8 posts (OK for now), a page 2 with 2 posts (still OK), but I can access a page 3 with 0 posts, and so on...
I'm trying also to implement wp_navi an I'm having an infinite navigation menu for only 10 posts.
The code used is the following:
<?php next_posts_link('« Previous') ?>
<?php previous_posts_link('Next') ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$featuredPosts = new WP_Query("category_name=Destacadas&paged=".get_query_var('paged'));
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<div class="post_container">
<?php include ('post-info-reduced.php'); ?>
</div>
<?php endwhile; ?>
Have you had this problem?
I've found the solution for using it with WP-PageNavi:
http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

Resources