Wordpress: url to all posts for home.php - php

I'm develop a theme for WordPress and I have created index.php for the original loop, and home.php for 4 latest posts, now I need to put a link for show all posts, how to I do that?

Instead of a link to show all posts, I would use WordPress's have_posts() function inside a while loop (first check to see if there are posts to display, if there are do so, if not exit loop and display other content). You could potentially look at WP's documentation for the function and maybe even glance at file used for the default page template (page.php).
In order to display only the latest 4 posts, you could simply add a counter variable and end each while loop iteration with an if statement checking to see if count == 4.
Also, just so you are aware, home.php sometimes takes precedence over index.php so either nest home.php inside an internal directory or call your '4 latest posts' page 'posts.php' or something similar.
Wordpress documentation: https://codex.wordpress.org/Function_Reference/have_posts

To get latest post with url:
<?php
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo 'Latest Post';`enter code here`
}
?>
Hope this helps .

Related

Custom Post Type Inside Regular Post Type

I am working on a website in Wordpress where I need to use custom posts (which I already created with the help of a plugin).
The problem is that the theme that I use allows me to display the post on the page organized according to categories, but when I create a custom post and put it into a category it is not displayed on the web (as if I had never created the post) but if I create the same post from the normal page of Wordpress entries (a standard Wordpress post type) and I put it in the same category this is shown on the page. Also, when I enter the custom post page the entry I created appears but when I enter the normal entries page it does not appear.
I went to a portal where they said how to add the custom post to the Wordpress categories by writing some lines of code in the functions.php file but this did not work, now I see the custom post within the category page but I still do not see them inside of the Wordpress entries page and also still not shown on the web.
You need to create a custom query. This page has good explanations and examples: https://codex.wordpress.org/Class_Reference/WP_Query
The most important thing in your case is to include this in your arguments array, which selects posts and your CPT:
'post_type' => array('post', 'your-custom-posttype'),
and also this which filters by category:
'category_name' => 'your_category_name'
So a typical simple custom query would look like this:
$args = array(
'post_type' => array('post', 'your-custom-posttype'),
'category_name' => 'your_category_name',
'post_status' => 'publish',
'posts_per_page' => 12
);
$query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<li>' . get_the_title() . '</li>';
// Other stuff echoing content etc. to be added here.....
}
wp_reset_postdata();
}

WordPress Sticky Posts

I have a site where on the home page there are teasers for the 3 most recent posts.
If I make a post sticky then it actually adds a fourth post display and so on.
After searching I found this script which seem to initially work:
<?php
$sticky = count(get_option('sticky_posts'));
$the_query = new WP_Query( 'posts_per_page='. ( 3 - $sticky));
?>
This seemed to do the job perfectly. However I found if one of the posts was a recent post and was also set to sticky then this would affect the amount displyed.
For example, it the second most recent post was made sticky then it would result in only two posts displaying.
Any ideas on how the above code can be altered to always show three posts, sticky or not?
Thanks
$args = array(
'posts_per_page' => 3,
'ignore_sticky_posts' => 1
);
query_posts($args);
-- use while loop here ---
and after that use
wp_reset_query();

wordpress. show shortcodes in lightbox plugin

I am working on a WordPress theme. I use a plugin to display pictures on a page (for each post 1 picture), Whenever one of this pictures is clicked the following code registers this and opens a lightbox with the content of the post in it:
<?php
if($_REQUEST['popup']!=''){
$postObj = get_post( $_REQUEST['pid'] );
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
exit;
?>
This all works fine.
Now the problem is that all content get displayed nicely. but for some reason shortcodes don't work. and also when I use a widget in post plugin to display a widget in the post, it doesn't get displayed.
First I tought I needed to enable shortcodes. So I changed this:
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
with this:
echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
But still nothing. So now I have no idea what to change to make the lightbox show widgets
Hope anyone knows the solution!
EDIT: when I open the post outside the lightbox (by just going to the single page) the shortcode get used like it should be. so somehow the code above don't recognize the shortcode or...
According to a sample here: http://codex.wordpress.org/Function_Reference/do_shortcode
It looks like you need to change: echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
to:
echo '<div class="ostContent">'.do_shortcode([shortcode_in_brackets]).'</div>';
This should actually display the code. I am assuming that you have defined the actual text in the widget in which the shortcode applies.
Otherwise in the way you currently do it the PHP fires before the post_content even has a value.
I think I understand your problem.
The following should work, assuming the posts in question have a post_type of post:
<?php
// Check for existence of 'popup' & 'pid' query vars
if ( $_REQUEST['popup'] && $_REQUEST['pid'] ) {
// Select single post by ID (using value of the 'pid' query var)
$query = new WP_Query( array ( 'p' => $_REQUEST['pid'] ) );
// Check that the query has returned something
if ($query->have_posts()) {
/* Loop through query until we run out of posts
(should only happen once in this case!) */
while ($query->have_posts()) {
// Setup post, so we can use the_content() and stuff
the_post();
echo '<div class="ostContent">';
/* The part we've been waiting for! the_content() will
display your post content as expected */
the_content();
echo '</div>';
}
}
}
?>
WP_Query is the way to go the majority of the time when needing to retrieve posts: http://codex.wordpress.org/Class_Reference/WP_Query
Your code was retrieving and displaying data almost directly from the WordPress posts table, giving WordPress no chance to apply any of its internal actions and filters (e.g. automatic paragraphs from double line breaks, shortcode execution).

PHP Script to Get Latest Post Featured Image and Link form WP blogs

I have 2 (Wordpress) blogs on the same server and a landing page with 2 images.
One goes to first blog, the next one to the second blog.
Is there any way I can get in the first image, the post-thumbnail and link from the first post from the first blog and same thing from the second blog for the other image?
Thank you!
Let's say, you have blog at:
http://example.com/ which resides on /home/username/example.com/
http://example12.com/ which resides on /home/username/example12.com/
and you have landing page at:
http://example31.com/ which resides on /home/username/example31.com/index.php
You will need to create 2 files:
/home/username/example31.com/index.php
/home/username/example31.com/index_test_wordpress.php
And both will have code, something like this:
on your /home/username/example31.com/index.php, write:
<?php
echo file_get_contents('http://example31.com/index_test_wordpress.php');
require_once("/home/username/example12.com/wp-load.php");
echo str_repeat("<br />", 10);
$posts = wp_get_recent_posts( array('numberposts'=>1, 'post_status'=>'publish') );
print_r($posts);
?>
on your /home/username/example31.com/index_test_wordpress.php, write:
<?php
require_once("/home/username/example.com/wp-load.php");
$posts = wp_get_recent_posts( array('numberposts'=>1, 'post_status'=>'publish') );
print_r($posts);
?>
Note:
I use file_get_contents to that file because I can't figure out a way to do two require() to wp-load.php, without conflicting each functions.
For simplicity's sake, I only put print_r($posts). But, you should get the idea.

Using Wordpress LOOP with pages instead of posts?

Is there a way to use THE LOOP in Wordpress to load pages instead of posts?
I would like to be able to query a set of child pages, and then use THE LOOP function calls on it - things like the_permalink() and the_title().
Is there a way to do this? I didn't see anything in query_posts() documentation.
Yes, that's possible. You can create a new WP_Query object. Do something like this:
query_posts(array('showposts' => <number_of_pages_to_show>, 'post_parent' => <ID of the parent page>, 'post_type' => 'page'));
while (have_posts()) { the_post();
/* Do whatever you want to do for every page... */
}
wp_reset_query(); // Restore global post data
Addition: There are a lot of other parameters that can be used with query_posts. Some, but unfortunately not all, are listed here: http://codex.wordpress.org/Template_Tags/query_posts. At least post_parent and more important post_type are not listed there. I dug through the sources of ./wp-include/query.php to find out about these.
Given the age of this question I wanted to provide an updated answer for anyone who stumbles upon it.
I would suggest avoiding query_posts. Here's the alternative I prefer:
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => <ID of the parent page>, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
) );
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
// Do whatever you want to do for every page. the_title(), the_permalink(), etc...
endwhile; endif;
wp_reset_postdata();
Another alternative would be to use the pre_get_posts filter however this only applies in this case if you need to modify the primary loop. The above example is better when used as a secondary loop.
Further reading: http://codex.wordpress.org/Class_Reference/WP_Query

Categories