I want to return the 2 newest posts when in single post mode, but exclude the current post. And I did it. The problem is, it just stopped working. It didn't change the code and it stopped on the server as well as on my localhost.
Here's the code:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
foreach( $recent_posts as $recent ) { ?>
<article class='single_recent'>
<header>
<?php echo $recent["post_title"]; ?>
</header>
<p>
<?php echo get_excerpt_by_id($recent["ID"]); ?>
</p>
</article>
<?php } ?>
</section>
Does anyone have an explanation ?
I tried removing the argument, still nothing. It returns an empty array.
Any suggestions which other function should I use to achieve the same effect ?
EDIT:
<?php
get_header();
get_sidebar();
?>
<?php the_post() ?>
<article class='post-single'>
<header class='post_header'>
<h1><?php the_title(); ?></h1>
<div class='post_header_bottom'>
<strong class='post_category'><?php echo get_the_category_list(', '); ?></strong>
<strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong>
</div>
</header>
<?php if (has_post_thumbnail()) : ?>
<figure class='post_single_image'>
<?php the_post_thumbnail(); ?>
<figcaption>No Will No Skill</figcaption>
</figure>
<?php endif; ?>
<div class='post_perex'>
<?php the_content(); ?>
</div>
<footer class='post_footer'>
<div class='post_footer_top'>
<div class='post_tags'>
<?php the_tags('', '', ''); ?>
</div>
<div class='post_time'>
<time datetime='<?php the_time('Y-m-d'); ?>' pubdate>
<span class='symbol'>P </span>
<?php relative_post_the_date(); ?>
</time>
</div>
</div>
<div class='post_share'>
<div class='share_show'>
<span class='symbol'>f</span> Like
|
<span class='symbol'>g</span> +1
|
<span class='symbol'>t</span> Tweet
<?php
if(function_exists('display_social4i'))
echo display_social4i("large","align-left");
?>
</div>
</div>
</footer>
</article>
<?php comments_template(); ?>
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope';
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
<div class='space'></div>
</div>
<?php
get_footer();
?>
You're missing your loop, or at least an global instance of the current post object. While I prefer to use the loop myself, you can get away with using the latter.
Change:
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
To:
global $post;
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
UPDATE:
The loop can be used in a single view to grab information from the default query_posts object. Even though it's just one post, the loop is most often used to populate the_content and other information.
You're correct that the ID should be irrelevant in this instance, since wp_get_recent_posts() should still return some results without any arguments (unless, of course, you're dealing with custom post types).
An alternate solution would be to try getting your recent posts using WP_Query:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
WP_Query will default to the standard 'orderby'=>'date' and 'order'=>'DESC' parameters, so those don't need to be explicitly stated (though, you can add them if you like).
As mentioned above and in my comment, I'm unsure if you want just the most recent posts, or if you're trying to query recent custom post types. If the recent posts are of type 'post' and that's what you want, then that is the default for WP_Query's 'post_type' parameter as well as wp_get_recent_posts.
Otherwise, you will need to explicitly state the 'post_type' parameter, so that $qargs looks like:
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2,
'post_type' => array('post', 'custom_post_type_slug', ...)
);
Just to check, you can also set 'post_type' to 'all' if you want to ensure SOMETHING is returned. If nothing is returned by either WP_Query or wp_get_recent_posts with 'post_type' set to 'all', then the issue is much larger, and I'll need to see all of the code in your single.php template.
Hope this helps.
NEW UPDATE:
Try commenting out the block of code altogether and substitute for:
wp_get_archives(array('type'=>'postbypost'));
If that doesn't work, then I'm fresh out of ideas. Something may have happened on your filehost's end that could possibly explain something happening out of nothing. Check and see if they have any announcements about this kind of activity. I know that many filehosts are in the process of replacing PHP4 and older versions of MySQL, which can probably cause some unforseen issues.
I would try creating a new subdomain with a clean, separate installation of Wordpress and a copy of your theme. Once that's set, just create a new post and see if the same problem occurs.
If so, then comment out chunks of code (possibly start with commenting out from get_sidebar through your first <article>...</article> block) in your single.php file and work out any errors that might pop up. If the block of code we worked on suddenly begins to populate, then post the block of code that was preventing it from happening and I'll try to work with you further (that is, if you're still having trouble).
Otherwise, if the clean installation fixes your issues, then it's probably some kind of discrepancy in your wp_options table. I would start merging tables over (first wp_posts, then wp_postmeta, then wp_terms....) until the problem pops up again.
Unfortunately, I think exhaustive testing might be in order to get this anomaly straightened out. Sorry I don't have a pure code solution. This is a pretty strange issue you're going through. Keep me posted, I'll do what I can to help.
Related
I've seen plenty of articles in and around how to do this but after hours of trying to achieve this using transients, I don't seem to be any closer to the dream!
Essentially I want to use wordpress transients to get 3 random posts and display them in a 'featured' module for 24hrs on my homepage. The 3 posts need to be a mix of native and custom post types.
The code I have so far is:
<div class="container">
<div class="featured-wrapper">
<div class="section-title">featured</div>
<?php
if ( ( $my_query = get_transient('my_query_cached') ) === false ) :
global $wp_query;
$args = array_merge( $wp_query->query, array(
'post_type' => array('post', 'recipe'),
'posts_per_page' => 3,
'orderby' => 'rand'
)
);
$my_query = new WP_Query($args);
set_transient('my_query_cached', $my_query, 24 * HOUR_IN_SECONDS);
endif;
?>
<?php if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="card-wrapper">
<a href="<?php the_permalink(); ?>">
<div class="card-img">
<img src="<?php the_post_thumbnail(); ?>"/>
</div>
</a>
<div class="card-cut"></div>
<div class="card-content">
<span class="card-category">treats</span>
<h1><?php echo get_the_title(); ?></h1>
</div>
</div><!--END card wrapper 1-->
<?php endwhile; wp_reset_postdata(); wp_reset_query(); ?>
<?php else: ?>
<div>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
<?php endif; ?>
</div><!-- END featured wrapper-->
Currently this code is returning all posts in the database so I believe there's an issue in the first section of this but can't seem to put my finger in it.
Massive thank you in advance for any help.
UPDATE
Fixed. Code above it actually fine but worth keeping in mind that any edits WON'T show once the page has run once as the transient has already been set. Worth setting the refresh rate to 1 * MINUTE_IN_SECONDS for testing.
I pasted your code into one of my sites' frontpage.php and it worked fine straight away. Change your expiry on your transient to 5 or 10 seconds to test it.
I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...
I have the wp-navi plug in working smoothly on any category that uses the global reading config (post per page "3"), but i have one category that i only need to show 1 per page, setting this value manually outside the loop destroys my paged links, it generates a lot of pages (instead of two, since i only have two posts) and they take to the home ( i guess is some sort of 404), i've tried with as many solutions i could find in stackoverflow and google, and no luck yet, i've been using wp for a couple weeks so im not sure if im doing something really stupid here, so please help me out.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//query 1 post
query_posts("posts_per_page=1&paged=$paged");
?>
<?php while(have_posts()) : the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php } ?>
Thanks in advance =).
EDIT:
I found something interesting, if i set the &paged=2 it actually shows the second page, and the page-navi says "page 2 of 2" but only if the url stays like this "mysite.com/category/" if i add "/page/2/" to the end, it redirects me to the broken home page. So even if the pagenavi works, the "/page/#/" is breaking it, of course the get_query_var('paged') or get_query_var('page') will return its default "1" since i'm not using the "/page/" structure, maybe i could do a fix by adding a ?p=# when i click in each number of the pagenavi, not quite sure how to do that in the plugin file, so i hope you guys could help me out based on what i'm saying now, if not, well i'll try to see how i fix this the ugly way. Thanks in advance again.
Ok i fixed this issue using js/jq i would rather find a fix using the wp codex but everything i've tried has been useless and i need it to solve this no matter how, besides i'm not sure how these permalinks work internally, and editing the pluging was kinda disastrous, adding a "&p=2" to the url was making the same damage as the "/page/2" so i created a script with jq to remove the links from the pagenavi, get their target page, adding an onclick to each one and passing the target page number as an argument, to later replace a hidden value on a form and sending the form, then retrieving the page number and adding it to the query_posts, it works like a charm, but i wish i knew how to do a proper fix in wp, or if it is a bug i think i must submit a ticket. Thanks for your help, if anyone have more ideas are welcome, if somebody have the same issue feel free to use this fix.
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" id="change_page">
<input name="target_page" type="hidden" value="1" id="target_page" />
</form>
<script type="text/javascript">
function go_to_page(target_page){
var page = target_page;
$("#target_page").val(page);
$('#change_page').submit()
}
$(".wp-pagenavi").find("a").each(function(){
var page = $(this).attr("href");
page = page.slice(-3);
page = page.replace("/","");
page = page.replace("/","");
// The if below, only apllies to the first link, since it doesn't use "/page/#" structure.
if(isNaN(page)) { page = 1; }
$(this).attr("href","javascript:void(0)");
$(this).attr("OnClick","go_to_page("+page+")");
console.log(page);
});
</script>
And on top of the page i use this:
if(isset($_POST["target_page"])) {
$page = $_POST["target_page"];
} else {
$page = 1;
}
Try this to get one post from one category, and keeping it paged:
query_posts( "category_name=cat_slug_here&posts_per_page=1&paged=$paged");
You should use WP_Query class to build query. And use this object as variable in wp_pagenavi().
So your code should look like.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( $args );
$the_query->query('showposts=1&paged='.$paged);
?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div><!-- fin expert -->
</div><!-- fin entry -->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(array( 'query' => $the_query )); ?>
</div>
<?php } ?>
I also encountered this weird problem of pagination.I dont know if its because of the latest version of wp but I tried different solutions and only this tweak worked for me. Change your get_query_var('paged') to get_query_var('page'). Try this
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; //notice this
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="entry">
<div class="single_entry">
<p>
<?php the_content(); ?>
<div class="clear"></div>
</p>
<?=get_social(); ?>
<div class="clear"></div>
</div><!-- fin expert -->
</div><!-- fin entry -->
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<div id="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php } ?>
<?php $wp_query = null; $wp_query = $temp; ?>
NOTE: I also noticed that you dont have closing bracket in your if(function_exists('wp_pagenavi')) { statement. Correct this as well.
I have a custom loop with posts which are added from the custom meta field checkbox. Only if the checkbox is checked, then the post is being added to the loop. I have a container that holds that loop. What i want to do is to check if that loop got any posts and if it is empty - just hide that container. Because otherwise when the loop is empty the container is remaining on the page:
<div>
<ul>
</ul>
</div>
This is the loop:
<?php
/* Slider ------- */
$slider = new WP_Query('showposts=-1');
if ( $slider->have_posts() ):
?>
<div>
<ul>
<?php while ( $slider->have_posts() ) : $slider->the_post(); ?>
<?php if ( get_post_meta($post->ID, "mf_homeslider", true) == 'slider_on' ){ // Check if post was added to slider ?>
<li>
<?php if (has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('large'); ?>
</a>
<?php } ?>
<div>
<?php get_template_part('includes/post_meta'); ?>
<h2>
<?php the_title(); ?>
</h2>
</div>
</li>
<?php } ?>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_query();
?>
Thank you in advance for your help.
You should filter your query by the custom field (see details here), it could also improve performance (an SQL condition vs. a while-if loop). After that, have_posts() returns whether the loop contains any posts.
So instead of
$slider = new WP_Query('showposts=-1');
use
$slider = new WP_Query(array(
'meta_key' => 'mf_homeslider',
'meta_value' => 'slider_on',
'posts_per_page' => -1)
);
and there's no further need for if ( get_post_meta(...
(showposts is deprecated, use posts_per_page instead).
Note: you are mixing braces and if-endif style, the latter would be much readable when nesting.
im having some trouble with this featured post function that I made. the issue that im having is that when query('showposts=1'); is set the featured post is not picked up. however when I put query(''); the sharethis plugin doesn't work. can any one please help me on what I might be doing wrong.
<div id="block_feature">
<div id="featured_post" class="post">
<div class="post_inner">
<?php
$featured = new WP_Query();
$featured->query('showposts=1');
while($featured->have_posts()) : $featured->the_post();
//$wp_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
$featured_ID = $post->ID; // We'll store this here so that we know to skip this post in the main loop
?>
<?php if(get_post_meta($post -> ID, 'feature', true)) { ?>
<?php if (get_post_meta($post->ID, 'large_preview', true)) { ?>
<div class="post_image">
<img src="<?php echo get_post_meta($post->ID,'large_preview',true);?>" width=150px; height=150px alt="Featured Post"/>
</div>
<?php } ?>
<div class="excerpt">
<h2><?php the_title(); ?></h2>
<small>on <?php the_time('M d'); ?> in <?php the_category(',');?> tagged <?php the_tags(''); ?></small>
<?php the_excerpt();?>
</div>
Read More
<?php } ?>
<?php endwhile; ?>
</div>
</div>
</div>
showposts is been deprecated since version 2.1, so it might be better to use posts_per_page instead. Not sure if it will make any difference, but you could also replace the first two lines of PHP with $featured = new WP_Query('showposts=1');
As to what is causing the problem I am not sure, your query looks fine to me and you didn't mention in what way Share This failed. I'm not familiar with the Share This plugin, but most such plugins add its content to the post using a filter function attached to the the_content filter. That said, it might just be that you are using the_excerpt() and not the_content().