Display custom post using ID - wordpress

In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.
Now what I want to do is, displaying a slider using its ID.
I did like following but no result
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
the_post();
endwhile;
}
This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.
Why is this weird ?

According to Codex the_post(); :
the_post(); iterate the post index in The Loop. Retrieves the next
post, sets up the post, sets the 'in the loop' property to true.
This function does not return any values.
Example for how we use this function:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
he_title();
the_content();
}
}
?>
You didn't explain well you question. anyway to display your metabox just use API function
echo get_post_meta( $id, 'metabox_name', true );

Related

wordpress - get forum list in a dropdown in bbpress

I am using bbpress plugin in Wordpress for forum. I need to display the forum list in a dropdown and on click of 'apply' button it will show topic results according to the selected forum.
Below is a reference site, similar to which I want to do in my project.
http://manhattanbeachpageant.com/forum/active?forum=All
Also I need to know what arguments to pass in the below function to get the results ?
<?php if ( bbp_has_topics( $bbp_loop_args ) ) : ?>
<?php while ( bbp_topics() ) : bbp_the_topic(); ?>
<?php bbp_get_template_part( 'loop', 'single-topic' ); ?>
<?php endwhile; ?>
<?php endif;?>
where
$bbp_loop_args = array();
So how I will display the forum list in a dropdown & what arguments I need to pass in the above array ?

Wordpress random post inside while loop

I am using a while loop
while (have_posts()) : the_post();
to get all the post and displaying a page. How to randomise the post. But the problem is, in the page I am working on there is no sign of 'query'. Also I tried to use some solutions mentioned but no result.
You can alter the main query with query_posts($args) this way:
// Alter the query
query_posts( 'orderby=RAND' );
while ( have_posts() ) : the_post();
// Do your stuff inside the loop
endwhile;
// Reset the query
wp_reset_query();

Display specific post format with limit number of post

I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;

Wordpress select posts which have the category name the same as the title

So,
i've created a post type named slider for which i've used this code in order to retrieve it's posts:
query_posts(array('post_type'=>'slider',
'posts_per_page'=>'20'
));
//loop
if(have_posts()) :
while (have_posts()) : the_post();
i've found somewhere, that this code would work to display all posts that come from a category which has the same name as the page title:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
How can i combine this two?
I need the post slider type, this slides i've divided in categories, so i need to get only slider post types that have the category name as the title.
Thanks
query_posts(array('post_type'=>'slider',
'posts_per_page'=>'20',
'category_name='.get_the_title()
));
And check the full documentation:
Function Reference/query posts

Different number of posts (WordPress)

We have selected to show 5 posts per page in admin panel.
And we want to show 10 posts per page in specific category (for example, "projects" width id=2).
How would we do it?
Change the normal loop to a query post. Like
if ( is_category(2) ){
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else:
echo'Nothing here...';
endif;
//Reset Query
wp_reset_query();
}
This should be used in the main loop only. . If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead.
Cheers
Pali Madra
I had the same problem.
This worked for me:
if ( is_category(2) ){
global $query_string;
query_posts( $query_string . "&posts_per_page=500" );

Resources