Good morning, I been trying to get an excerpt of a post to show random post excerpts on the homepage with no luck. My code looks like this:
<?php
$spotlight = new WP_Query( array( 'post_type' => 'success-spotlight', 'posts_per_page' => 1, 'orderby' => 'rand' ) );
while ( $spotlight->have_posts() ) : $spotlight->the_post();
$spotlight_title = get_the_title();
$spotlight_excerpt = types_render_field("story-headline", array("raw"=>"true"));
$spotlight_link = get_permalink();
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'spiffy-success-spotlight-`home');
$spotlight_image = $thumb[0]; // thumbnail url
}
endwhile;
wp_reset_postdata();
?>
<?php
?>
Any suggestions would be greatly appreciated and thank you in advance
How about:
query_posts('orderby=rand&showposts=1&post_type=success-spotlight');
Related
I used this code to my category.php and I want it to convert for my page template, this code is fully functional and working on category.php.
<?php
if (in_category('interior')) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=false&order=ASC&orderby=title&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>') {
echo '<ul class="ul-menu">'.$this_category.'</ul>';
}
}
?>
Image Attached Sample
Use this code into your page template.
$args = array (
'cat' => array(2,6,9,13),//use category id
'posts_per_page' => -1, //showposts is deprecated
'orderby' => 'date' //You can specify more filters to get the data
);
$cat_posts = new WP_query($args);
if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post();
get_template_part( 'content', 'page' );
endwhile; endif;
I think its work for you. check and let me know
Hellow everyone!
I am showing blog of posts in additional wp template and everything works fine, but I've made some kind of gallery from it, and I don't need to show anything except gallery and title.
Inside posts I have this:
[gallery ids="1618,...,1634"]
<h2>...</h2>
<p>...</p>
text without format, etc.
As you can see, I am using a gallery shortcode. I need it to be shown, but all other content to be excluded from the loop.
Really appreciate your help in this question...
My template code:
<?php
/*
* Template name: Блог
*/
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $current_page,
'cat' => 8
);
query_posts( $args );
$wp_query->is_archive = true;
$wp_query->is_home = false;
while(have_posts()): the_post();
?>
<div class="foto_posts">
<?php the_content() ?>
<?php echo '' . get_the_title() . '';?>
</div>
<?php
endwhile;
if(function_exists('page_navi_slider')) page_navi_slider();
try to replace <?php the_content() ?> to <?php the_title() ?>
if you just want to show gallery shortcode try this
replace
<?php the_content() ?>
to
$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $post->post_content, $matches);
if (is_array($matches) && $matches[2] == 'gallery') {
$shortcode = $matches[0];
echo do_shortcode($shortcode);
}
Try this for getting galleries and videos use the get_post_meta_key()
<?php get_post_meta($post_id,'key_name',1);?>
If it returns an array then traverse that array to get the images link and video links
add_shortcode('gallery', 'gallery_shortcode_fancybox');
function gallery_shortcode_fancybox($attr) {
$attachment_ids = explode(',',$attr['ids']);
$args = array(
'post__in' => $attachment_ids,
//'cat' => 8 if category needs to be shown pass it in shortcode , same as ids
);
$gallery_posts = query_posts( $args );
foreach ($gallery_posts as $key => $value) {
//do the stuff here
echo '<h2>'. $value->post_title;'</h2>';
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($value->ID);
echo '<p><img src="'.$feat_image.'" ></p>';
}
}
I'm using the following PHP script to find the most recent post on the Team area of my site.
I also use a very similar one to find the most recent news entry on my home page.
To reduce the amount of repeated code (DRY), is there a way I can use a function and just pull in a specific custom post type e.g. most_recent('team'); would show the most recent post from my Team CPT.
Here's my existing code:
<?php
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => 'team',
'posts_per_page' => 1,
"post_status"=>"publish"
));
?>
<?php if ( $new_loop->have_posts() ) : ?>
<?php while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile;?>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php
function most_recent($type) {
$new_loop = new WP_Query( array(
'post_type' => $type,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) {
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo '<h2>'.the_title().'</h2>';
the_content();
endwhile;
}
wp_reset_query();
}
?>
Yes, It is indeed possible.
First what you need to do is ,
Add below code to your theme's functions.php file:
function most_recent($name){
// find most recent post
$new_loop = new WP_Query( array(
'post_type' => $name,
'posts_per_page' => 1,
"post_status"=>"publish"
));
if ( $new_loop->have_posts() ) :
while ( $new_loop->have_posts() ) : $new_loop->the_post();
echo "<h2>".the_title()."</h2>";
the_content();
endwhile;
else:
endif;
wp_reset_query();
}
Now you can use it any where in your theme folder template like below:
$most_recent = most_recent('product');
echo $most_recent;
So in your case, it would be most_recent('team') or even you can use for other as well just like I did for product.
Tell me if you have any doubt.
I have a custom post type called press.
What I want to achieve is generate a list of the titles (with it's corresponding link to it) of all press custom post types.
I tried with this code with no luck, any ideas?
<?php function all_posts_custom_posts( $query ) {
$post_type = $query->query_vars['post_type'];
if ( 'press' == $post_type ){
$query->query_vars['posts_per_page'] = -1;
return;
}
}
add_action('pre_get_posts', 'all_posts_custom_posts',1); ?>
And also highlight the current post from the list by adding a class to it.
I had a similar issue sometime ago. The following code example will display how I resolved my issue.
<?php
$type = 'products';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
I am working on a wordpress site wich has a custom post type called, let's say 'plops'. I only want to use this custom post type in the website. So in the index.php, I am looping through those with a WP_Query, here is the code :
<?php
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'orderby' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_meta($post->ID);
?>
// my template for the post...
<?php endwhile; ?>
The thing is I want to implement an infinite scroll to this page, and for that I need to have pagination links. I tried to implement those with the next_posts_link() function, but can't make it work !
It just won't display any link, and I tried a lot of stuff, nothing seems to make it work...
if i go to mysite.com/worpress/page/2, My posts do display, but I get a 404 in firebug... weird...
Any ideas ? Would really appreciate help ! Thanks a lot in advance !
The fix below might help you - this worked for me. It combines Chris Coyier's solution here with some help found in the comments. CSS Tricks Article
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array('post_type' => 'custom_post_type', 'paged' => $paged );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query( $args );
while($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
It has been a long time since this question is posted, but for anyone who is looking for a way to achieve this in his/her own code:
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'order' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
$meta = get_post_meta($post->ID);
// my template for the post...
}
previous_posts_link('« Newer');
next_posts_link('Older »', $loop->max_num_pages);
wp_reset_postdata();
Note: You have to pass the max number of pages to the next_posts_link function only.