Displaying specific post title on the front page in wordpress - wordpress

I want to display the specific post title/content in the front static page.Remember not all posts just specific. So can anybody guide me how to do that..

Yes you can get specific posts in front page by passing post ids with array in include parameter something like this,
<ul>
<?php
global $post;
$args = array(
'offset'=> 1,
'include' => array(1,2,3) // PASS POST ID IN ARRAY
'post_type' => 'post', );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
<?php the_content(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Hope this works.

<?php
$titles=array();
$contents=array();
$links=array();
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3,
));
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $titles[]=get_the_title(); ?>
<?php $contents[]=get_the_content(); ?>
<?php $links[]=get_the_permalink();?>
<?php endwhile; ?>
and now printed the value in my page wherever i wanted
<?php echo $titles[0]; ?>
<?php echo $titles[1]; ?>
<?php echo $titles[2]; ?>
And same for other declared arrays. :)

Related

Can I get pages from the WP-PostRatings and WP-PostVevs plugins using get_posts()?

I use WP-PostRatings WP-PostViews plugins and was able to display pages using functions
<?php if (function_exists('get_highest_rated')): ?>
<ul><?php get_lowest_rated(); ?></ul>
<?php endif; ?>
<?php if (function_exists('get_most_viewed')): ?>
<ul><?php get_most_viewed(); ?></ul>
<?php endif; ?>
but they are displayed as plain text and I cannot edit them, add a thumbnail of the post and do other things is it possible to not display them as if using get_posts() like this
<?php
global $post;
$args = array(
'post_type' => 'post',
'orderby' => 'comment_count',
'order'=> 'ASC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ){ setup_postdata($post);?>
<li><?php the_title(); ?></li>
<?php } wp_reset_postdata(); ?>
but only for rating and views

How to create single page of advance custom field in wordpress?

I am create post with advance custom field (movie-page.php) I want to create single page(single-movie.php) each post of movie-page.php when I am click on each post it link to full details but I can not do it. Help me please!!!. thanks you.
And here is my code movie-page.php
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'movie'
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<div class="jol">
<?php the_title($post->ID); ?>
<?php get_post_field('descriptionii',$post->ID ); ?>
<?php the_content() ?>
<?php the_field("descriptionii") ?>
</div>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php $args = array(
'posts_per_page' => -1,
'post_type' => 'movie',
'post_status' => 'publish',
);
$posts_array = get_posts($args);
foreach ($posts_array as $key => $value) {
echo "".$value->post_title."</br>";
echo $value->post_content;
}
?>
<?php get_footer();

Wordpress Tags in class

I have added tags to my Custom Post Type.
Now I want to use them to create a isotope portfolio, I can load all tags with this code:
<?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 24;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="all <?php echo $tag->slug; ?>">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</div>
<?php endwhile; ?>
But now I want to add the all tags that from each portfolio item to the class="".
With <div class="<?php $tag->slug; ?>"> I just get the last tag of all the tags that are used.
I know there are already a lot of posts about this problem, but every post I have found does not seem to work for me.
It now works with the following code:
<?php $tags = get_the_tags();
$tag = wp_list_pluck( $tags, 'slug' );
$tagToClass = implode(" ", $tag);
?>
And then use <?php echo $tagToClass ?>

How to show Previous and Next link in WordPress

Below is my example code that is dynamically get the posts.
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
The code above will works correctly but my question is, since this is not a default blog page or a category, how can I use the posts_nav_link() so that I can still access the rest of the pages? I tried to used it but it doesn't work unless if the current page is a category. Hope you guys can help me this.
If you giving you paging in your custom post type. then i think you can do very simple you have to use wordpress plugin like wp-pagenavi after then add your custom post type in this plugin in admin panel after then add
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
Without plugin you can use like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, 'cat' => '-10, -72&paged=' . $paged) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?> <?php the_title(); ?>
<span><?php the_time('d.m.y') ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

Wordpress displays posts of category ID

Can't seem to find the right answer for what I thought would be trivial.
I have some categories arranged like this...
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.
This is what I am doing right now:
<?php
query_posts('cat=2&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).
Any suggestions?
Thanks in advance. (SO Community = Awesome Sauce).
try below code:
<?php
$current_cat_id = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
If you are using category.php, you can omit the query_posts and it will auto fill in the posts for you.
<?php
// Start the loop.
$categories = get_categories('child_of=1');//Parents category id
foreach ($categories as $cat) {
$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;//parents sub category name
$option .= '</a>';
echo $option;
query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; }?>
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => 1 );
// 1 is a cat id.
//
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
try
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );
Try this, this is better solution for this and you can also use it to show Related Post by a category id...
Just call the function that mentioned below by using this line. Put this in your template or page.php/single.php file.
Call by put this line: related_post_title('enter cat id here..');
Here is the Function and put this in function.php file.
Related posts function:
function related_post_title($cat_id){
$cat = $cat_id;
// Check if it is page only
if ( is_page() || is_single()) {
$args=array(
'cat' => $cat,
'order' => DESC,
'orderby' => rand,
'post__not_in' => array($post->ID),
'posts_per_page' => 9999,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ' <ul> ';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
If any query please let me know, i will help you...

Resources