Output blog posts only onto a part of webpage - php

By default, blog posts can be shown on single page.
i want to customize a front page and display the posts only on the part of my page.
If it's possible, how can i output my latest blogposts onto a custom div or element?

Create custom loop, something like this:
http://www.johnmorrisonline.com/how-to-create-a-custom-loop-in-wordpress-using-wp_query/
So, basically use that WP_Query to get list of post and loop trough it:
http://codex.wordpress.org/Class_Reference/WP_Query
Here is an example how I do it:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => 6,
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );
// print_r($posts_array);
foreach($posts_array as $current_post){
print_r($current_post); // here you have current post values
}
http://codex.wordpress.org/Template_Tags/get_posts

Related

Get posts by category and language, using PolyLang

I'm creating a plugin and I already could get the posts by category and by the current language using get_posts() function from WordPress and passing the attribute lang with the pll_current_language() from PolyLang.
$args = array(
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'lang' => pll_current_language()
);
return get_posts($args);
Now, I'm wondering how to get the posts by categories related to the language?
For example, I have the News category for English and Noticias for Spanish. How can I set this automatically?
Something like this:
$args = array(
......
'category' => **current_category_for_this_language**
......
);
return get_posts($args);
Any ideas?
Use pll_get_term and filter by category. In this case '34' is my term ID (gotten by hovering the edit link of the term).
By the way as far as I know get_posts gets only posts in the current page language by default and it gets posts by default sorted by date DESC, so you could omit those from your query I think.
$args = array(
'posts_per_page' => 6,
'category' => pll_get_term(34)
);
return get_posts($args);
Sources
https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
pll_get_term
Returns the category (or post tag) translation
Usage:
pll_get_term($term_id, $slug);
‘$term_id’ => (required) id of the term you want the translation
‘$slug’ => (optional) 2-letters code of the language, defaults to current language
https://codex.wordpress.org/Template_Tags/get_posts
Default Usage
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>

Get permalink of the oldest post in wordpress

I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>
add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);

wp_get_recent_posts not working correctly with appended posts

I want to append some posts to the wp_get_recent_posts function, so they are always retrieved with the normal results, the problem is that I dont get the posts I want to append in the results, am I doing something wrong?
$include = array(1, 2);
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'append' => $include,
);
$entries = wp_get_recent_posts($args, 'ARRAY_A');
If I remove the tag_id argument then the posts I want to append are included in the results but I need to filter the normal results by tag ID, seems that WP also filters the appended posts with the other arguments, is there any workaround?
According to https://codex.wordpress.org/Function_Reference/wp_get_recent_posts there is no 'append' parameter, but there is an 'include' parameter that does what you want.
Try this:
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'include' => $include,
);

Sub post not showing in wordpress admin

I am trying to show all the post (including sub post) in wp-admin. but the post which are having parent are not showing in the list of post although the count is giving like 142 post but showing 42 out of 142 since 100 posts are sub posts.
I installed a plugin wp sub post for this where it gave me an option to add a post as parent but not coming in wp-admin.
what can i do for this?
In database it is stored with post_parent id where as the normal have post_parent= 0
try the following code. make sure value of post_parent=-1 to get all posts
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '-1',
'post_status' => 'publish',
'suppress_filters' => true ); ?>
<?php $post_arr=get_posts($args);?>
<?php echo '<pre>';print_r($post_arr);echo '</pre>'?>

wordpress, how to use get_the_title(ID) to get the title of the last post published in a separate file?

i have a WordPress plugin that i am modifying and i want to echo out the last post title name.
I am not sure if i can use .
How will it know which one is the last one?
any ideas?
Thanks
Use below code:
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish' );
$posts = get_posts( $args );
you can find post's title in $posts.
By changing order in argument list, you can get most recent or most last post.
Ref: http://codex.wordpress.org/Template_Tags/get_posts

Categories