I am adding post thumbnail jquery slider to header part but it's resulting weird issue. It is somehow not ending while or stop query and so if I will go to single post or page it is keep displaying loop instead of page or post content.
I have tried two different query but none of them stopping to this weird issue.
First Tried
<?php
query_posts( 'post_status=publish&orderby=rand' );
while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; ?>
Than Second Tried
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_status=publish&orderby=rand');
// The Loop
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query();?>
None of these stopping to display loop ( all post like index page) in to single post or page.
I will go to single post or page it is keep displaying loop instead of page or post content...
That is because you are not passing it any parameters that would limit the query for single posts. Your query ( $wp_query->query('post_status=publish&orderby=rand'); ) pulls all posts, all the time, and in random order. For single post display you need to pass it a post or page parameter. You probably need to use get_query_var() to check for 'p', 'page_id', or both. Something like this:
$pid = get_query_var('p');
if (!empty($pid)) {
$qry = 'p='.$pid;
} else {
$qry = 'post_status=publish&orderby=rand';
}
$wp_query->query($qry);
There are other possible solutions as well, like is_single().
Also, WordPress uses the variable $wp_query so you should really pick another one instead of clobbering that one.
I found the solution :)
I have added if (have_posts()) before while and end loop with wp_reset_query() and all good now. :)
So here is final code if anyone having same problem..
<?php
query_posts( 'post_status=publish&orderby=rand' );
if ( have_posts()): while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query(); ?>
Related
I'm facing an issue with my website.
I would like to display all my custom post type named "Projets", and for each item, I want to get several term names to put in my data element.
Displaying all my post is not an issue, it's working well. I manage to display one term name using "get_terms()", but I don't know how to display several terms and put them in the right place.
I have 3 different custom taxonomy : city, typo and statut.
There is my code :
<?php
$args = array(
'post_type' => 'projets',
'posts_per_page'=>'99',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="content_item active" data-city="CITY_NAME_HERE" data-typo="TYPO_NAME_HERE" data-statut="STATUT_NAME_HERE">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('custom-size', ['class' => 'content_item_img', 'title' => 'Image du projet']); ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile;
wp_reset_postdata();
?>
Thanks for the help!
Try this:
Inside the loop, you get the taxonomy like this (eq. "city"):
$cities = get_terms( array(
'taxonomy' => 'city',
'hide_empty' => false,
) );
You get an array of all the terms of the Taxo.
Where you need them, you have to loop them, here to give you an idea.
if(!empty($cities)){
foreach($cities as $city){
echo $city->name.', ';
}
}else{
echo 'No Cities.';
}
Comma Separated custom categories inside the loop.
<?php echo get_the_term_list( $post->ID, 'cutom_category', '', ', ', '' ) ?>
I thought this was going to be simple, but after hours of trying I still can't get it to work.
All I want to do is create a list of links to posts within a specific category and put it in the footer of my site.
Note: I do not want to use yet another plugin!
Try this:
<?php
// The Query
query_posts( array ( 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) );// -1 will display all posts, change to limit posts
// The Loop
while ( have_posts() ) : the_post();
echo '<li>', '<a href="<?php echo get_permalink( 268 ); ?>">';
the_title();
echo '</a>', '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
OK, so after getting Dungey140's solution to work this is how I did. This returns the correct URL for the posts too.
<?php
query_posts ( array ( 'post_type' => 'the-post-type', 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) );
while ( have_posts() ) : the_post(); { ?>
<li><?php echo the_title(); ?></li>
<?php }
endwhile;
wp_reset_query();
?>
I have an events page displaying an 'events' custom post type. At the top of the page I have 2 upcoming posts in an upcoming event list and then at the bottom I have 3 past events in a past events list. Posts are displayed in each section based on if an 'event date' custom field is in the past or not. The past events uses WP page navi plugin to paginate the posts. 'posts_per_page' is set at one as a test.
There should be 3 pages that have 1 past post displayed on them, respectively. Unfortunately, the pagination shows 5 pages, the first two have no posts displayed on them, as if the posts from the upcoming posts lists are behaving as ghosts. I have removed the upcoming posts list as a test but this makes no difference.
Does anyone know any reason for this? I have no idea. Thanks.
<?php
$temp_post = $post; // Store the Page the Post
// Get the current date
$current_date = date('M d, Y', time());
$current_date = strtotime( $current_date );
$wp_query= null;
$wp_query = new WP_Query();
$args = array( 'post_type' => 'events','posts_per_page' => 1,'paged' => $paged,'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'DESC');
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
// Get the date custom field
$post_date = get_field('event_date');
$post_date = strtotime( $post_date );
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
MY POST CONTENT GOES HERE
<?php endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php else: ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>
<?php $post = $temp_post; // Reset the Post ?>
It's acting as ghost because you are not showing it. You are showing the navigation in the bottom of page. The query returns 5 page, so navigation would be for 5 page.
But you aren't showing the post, if it doesn't meet this condition
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
That's why those posts are not showing. Remove that condition and you will see that all the posts are showing as it should.
If you want to display pagination this way, you would have to limit the number of returned post from query itself.
Thanks a lot, I managed to work it out with a push in the right direction.
// Get the current date
$current_date = date('Ymd', time());
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'events',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'event_date',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $current_date,
'compare' => '<',
'type' => 'CHAR'
)
)
);
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
MY POST CONTENT HERE
<?php //endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>
How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
I have a template named "Foo" in "foo.php", I would like to be able to select all pages that are using that template. I have searched for awhile now but have not been able to find a successful way to do this... Can somebody enlighten me on the proper/only way to do this?
You can get this by using following code
$query = new WP_Query( array( 'meta_key' => '_wp_page_template', 'meta_value' => 'foo.php' ) );
if ( have_posts() ) while ( have_posts() ) : the_post();
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
Robot's answer is good, but I thought I'd clarify a few things.
First, You should use the variable for the query you created, so it would be $query->have_posts() etc.
Second, you should specify post_type. I used any, so that it will pull any post types except for revisions.
Last, if this is in a page with any other WP loops, you may want to use wp_reset_query. I added one below and one above just in case, but you only really need this if you have another loop above or below. Remove it if you don't.
Here is the code:
wp_reset_query();
$query = new WP_Query( array(
'post_type' => 'any',
'meta_key' => '_wp_page_template',
'meta_value' => 'foo.php'
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post(); // WP loop
the_title();
endwhile; // end of the loop.
} else { // in case there are no pages with this template
echo 'No Pages with this template';
}
wp_reset_query();
Hope that helps someone!! Happy coding!
This also works
$pages = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
)
);
foreach($pages as $page){
echo $page->post_title.'<br />';
}
http://jorgepedret.com/old/web-development/get-pages-by-template-name-in-wordpress/