Display latest articles inside of a post on wordpress - php

i was browsing some themes on wordpress, and i found one that is kind of a clone from 9gag.tv
You can see the demo theme here
http://codecanyon.net/item/youtube-viral-videos-9gag-tv-clone/full_screen_preview/6770578
If you see that page, you see the "latest videos" area below, that has the posts arranged by date, but when you open a video, that area changes and it gives you random videos.
Is there a way to arrange the videos like in the home page and allow user to browse to older videos?

I guess its easy to achieve it using the get_posts() loop. You'll also need to register new custom post_type for whatever posts you're going to post and fetch it through a loop like the example below.
I'm sure someone else will give a full answer but this will get you started.
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'post_type' => 'custom_post_type', 'posts_per_page' => 10, 'orderby' => 'rand' );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_attachment_link( $post->ID, false );
the_content();
}
wp_reset_postdata();
}
?>

You can use wordpress php function get_posts() to fetch the posts, then echo the data you want using a foreach. The function also has the offset argument which you can use for the navigation.

Related

How to move all the posts to move to trash

I'm facing the issue in route change by click the apply button, while selecting the move to trash.
NOTE: move to trash and apply process is working by selecting some posts. Bulkly, I trying to move to trash is facing the issue.
url : https://abcd.com/blog/probs/wp-admin/edit.php
After selecting move to trash without selecting any posts and click the apply button url changes to
Incase of delete all posts, I got a route like this
changed url : https://abcd.com/probs//wp-admin/edit.php?paged=1
For this you can try it programmatically by creating your code.
for trashing all the post you try this code:
function move_all_posts_to_trash() {
$args = array(
'post_type' => 'post', // Change this to the post type you want to move to trash
'posts_per_page' => -1, // Get all posts
'post_status' => 'publish' // Only get posts that are published
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
wp_trash_post( $post->ID ); // Move post to trash
}
}
// If you want to move single post or page you can used by default wordpress function
Basic Example
Trash the default WordPress Post, “Hello World,” which has an ID of ‘1’.
<?php wp_trash_post( $post_id = 1 ); ?>
// For Multiple Posts using WP_Query using while loop.
You can used as per requirement like create shortcode or fire hooks.
function trash_custom_posts() {
$args = array (
'post_type' => 'custom_post_type_slug',
'post_status' => 'publish',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
wp_trash_post( $post_id );
}
wp_reset_postdata();
}
add_action( 'init', 'trash_custom_posts' );

How to display all posts with the same title in wordpress single.php file

I have an anime website in WordPress where I add episodes as posts.
I'm using a theme that i created by myself, what i want is to display the list of episodes of the anime that the user is watching by looping through the posts with the same title to display them all using PHP. I tried a lot of solutions but none of them worked.
In brief, I want to display posts with the same title to make a list of episodes
This is one of the solutions that i tried and didn't work.
<div class="episodes">
<?php
$EpisodesList = new WP_Query('post_title='the_title()'');
if ($EpisodesList->have_posts()) {
while(have_posts()) {
the_post();
echo the_title( );
}
}
?>
</div>
You must get the post type 'EpisodesList' in your WP Query. WP_Query is a class used in WordPress theming that accepts a variety of parameters to request and fetch posts around those parameters. The example below allows you to set a list of parameters, fetch the posts matching those parameters, and display the title and excerpt of the post on the website.
Code goes in your custom template file where you want to render the posts.
/**
* Setup query to show the ‘EpisodesList’ post type with ‘8’ posts.
* Output the title with an excerpt.
*/
$args = array(
'post_type' => 'EpisodesList',
'post_status' => 'publish',
'posts_per_page' => 8,
'orderby’ => 'title',
'order’ => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();

Get Buddypress style to display posts in user's profile tab

I really hope you can help me here because it's literally driving me crazy and keeping me from sleeping for 3 days now.
I've been looking everywhere to find a solution to my questioning and everytime I think I'm finally getting somewhere the answers are either too old or the links down, or when someone is facing the exact same issue they never get an answer.
SO. Here is the thing.
In my function.php I created a CPT so my users can create posts with WP User Frontend without polluting my blogs. Now, since the posts are in a CPT, they no longer show up in the built-in Posts tab that Buddypress provides. So, I then added a custom tab to display their posts on their profile.
And now it's getting messy.
I managed to get the posts from the CPT, everything works fine (I know it because if I echo the post thumbnail and title I see them). But I can't figure out how to get the freaking Buddypress template to style the posts list. I think I've read something like more than a hundred threads, I went through Wordpress and Buddypress codex, I even dug into my Buddypress plugin files to find the templates and blog loops and try to understand how it works. But still, nothing.
Here is the code I have so far
function bp_costumes_tab_setup_nav() {
global $bp;
$parent_slug = 'profil-costumes';
bp_core_new_nav_item( array(
'name' => 'Costumes tab',
'slug' => $parent_slug,
'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/',
'screen_function' => 'costumes_tab_show_screen',
'position' => 3,
'default_subnav_slug' => 'profil-costumes'
) );
}
add_action( 'bp_setup_nav', 'bp_costumes_tab_setup_nav' );
function costumes_tab_show_screen() {
add_action( 'bp_template_content', 'bptab_costumes_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function bptab_costumes_content() {
$myposts = get_posts( array(
'posts_per_page' => -1, // set the number of post to show, -1 if all
'author' => bp_displayed_user_id(),
'post_type' => 'cptgalerie',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
));
if( ! empty( $myposts ) ) {
foreach($myposts as $post) {
setup_postdata( $post );
**HERE I WANT THE POSTS FROM CPT TO BE DISPLAYED LIKE IN THE BUILT-IN POSTS TAB**
}
wp_reset_postdata();
} else {
echo '<div class="info" id="message">
<p>Aucun album publié.</p>
</div>';
}
}
As I'm using Elementor Pro I also tried creating a loop and add the shortcode in my php with
echo do_shortcode ("[elementor-template ID='xxx']");
but it gets the template without the post info. It just keeps repeating the user's name (which is technically the "post title" since I used the "post title" widget in my template, I know, but it's not the title I want). Here too I tried to understand how it works by digging into another plugin I use that gets Elementor templates to display posts.
I found other answers where "if ( ! empty...)" is replaced by "if (have posts)... while" but it doesn't seem to work. I've read also that now the "bp_core_load_template" should be replaced by something else... many tests but no results. I also found that it has something to do with some archive template.
I'm lost.
Please, pleeeeeaaaase, can someone help me?
I need to sort it out, get it out of my mind and SLEEP.
Thank you!

Displaying posts of a category wordpress

I am developing a wordpress theme from scratch. I have completed the design and am stuck at the listing of posts.
I am currently using:
$args = array( 'posts_per_page' => 10 );
$myposts = get_posts( $args );
It does display all the posts.
How can i display the posts in a category which is clicked?
I have no clue about how to achieve this. I tried to get the category name from URL and the pass it to the get_posts(). But i dont think this will be efficient because using different URL rewrite the URL can be changed.
If you write your code in archive.php wordpress understand is a category and automatically return related posts otherwise by code you need to pass category id.
Example:
$args = array( 'posts_per_page' => 10,'cat'=>1 );
$myposts = get_posts( $args );

Wordpress show multiple post types in loop

I'm creating a Wordpress site were I would like to show "tiles" with content from the site on the front page. These tiles are custom post types from the site like "our services", "consultants", "blog posts" and so on.
I know how to show one custom post type in Wordpress, but the problem is that I need to pull multiple post types in the same loop as I want them to be displayed in a matrix. Another problem is that I need to shuffle all the items in a random order, so that for example not all blogs just show in one place but all objects show after different items in random.
The third problem is that I need to show all items for a certain post type and just the latest for another. For example do I need to show all "our services" tiles, but only a couple of the "blog" tiles.
Is this possible to do, or can you not pull out records in this way using Wordpress?
Thank you for the help!
I suggest reading up on custom wordpress queries https://codex.wordpress.org/Class_Reference/WP_Query
For the first question you just need to specify
'post_type' => array( 'tiles', 'consultants', 'post' )
for the second question
'orderby' => 'rand'
so you will have something like
$args = array(
'post_type' => array( 'tiles', 'consultants', 'post' ),
'orderby' => 'rand'
);
$query = new WP_Query( $args );
For the third question - I'm not sure if it is possible to achieve with one query.
you can customise the things like this ,
$posttypes = array('post_typ1','post_typ2','post_typ3');
$randompost_typs = shuffle($posttypes);
$counter = count($posttypes);
for($i=0; $i<$counter;$i++) {
// suppose you want to show all posts from post_type1 then
if($randompost_typs[$i]=='post_typ1') {
$posts_per_page = -1;
} elseif($randompost_typs[$i]=='post_typ2') { // will work for 2nd post type
$post_per_page = 5; // show 5 posts from this post type
} else {
$post_per_page = 3; // show 3 posts from last post type
}
// here you will use the WP_Query class from wordpress
$args = array(
'post_type' => $posttypes[$i],
'orderby' => 'rand',
'posts_per_page' => $post_per_page
);
$query = new WP_Query( $args );
if($query->have_posts()) : while($query->have_posts()): $query->the_post();
// all the remaining wp loop content for example
the_title();
the_excerpt();
endwhile;
else:
echo 'no posts';
endif;
}
hope this will help, let me know if it has any issue.

Categories