Wordpress twitter bootstrap image carousel from specific category - wordpress

I'm using the Bootstrap theme from 320press and currently struggling with the image slider carousel. It displays the posts of every featured image on the site, when I just want a specific category. I guess it's the following code I have to tweak som way...?
<?php
global $post;
$tmp_post = $post;
$show_posts = of_get_option('slider_options');
$args = array( '2' => $show_posts ); // set this to how many posts you want in the carousel
$myposts = get_posts( $args );
$post_num = 0;
foreach( $myposts as $post ) : setup_postdata($post);
$post_num++;
$post_thumbnail_id = get_post_thumbnail_id();
$featured_src = wp_get_attachment_image_src( $post_thumbnail_id, 'wpbs-featured-carousel' );
?>

Try replacing the $args for get_posts() with the following. You'll need to put in the slug of the category that you want to show. Since I don't know exactly how this plugin works, I can't guarantee it'll work as expected.
$args = array(
'posts_per_page' => $show_posts,
'category_name' => 'your_category_slug'
);
You can see a complete list of the arguments available for get_posts() on the WP_Query page of the WordPress codex: http://codex.wordpress.org/Class_Reference/WP_Query.
I'm a little confused by the counter that your code is setting. It seems like setting the number of posts to fetch as an argument to get_posts() would make a lot more sense.

Related

Custom WP_Query for search

I'm trying to make a custom WP_Query for a search results page.
With the following code, the page always displayed all posts regardless:
<?php
$args = array(
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
?>
So I've added the search Query to the $args, but this always returns no results - where is this going wrong?
<?php
$search_query = get_search_query();
echo $search_query;
$args = array(
'posts_per_page' => '-1',
's' => $search_query
);
$query = new WP_Query( $args );
?>
1) You can use the templatesearch.php and searchform.php as your starting points. Creating a Search Page Codex
2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts
There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!
I have solved this using pre_get_posts - I'm still intrigued as to why using the WP_Query method doesn't work, but here's what I'm now using:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', '-1');
}
}
}
add_action('pre_get_posts','search_filter');

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

How to get latest post id in functions.php Wordpress

Can anyone help me to get latest post id in Wordpress functions.php file.
I think it must be something like
global $wp_query;
$thePostID = $wp_query->post->ID;
But cant figure out what next.
Thanks
Using wp_get_recent_posts:
$recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
$thePostID = $recent_posts[0]['ID'];

WordPress foreach loop pulling 3 videos by tag

I am trying to get 3 of my videos I tagged in WordPress with the tag "video."
The code in the theme I got doesn't work, I tried to edit it and all but I can't get any further.
global $post;
$tag = get_term_by( 'name', 'video', 'post_tag', 'ARRAY_A' );
$videos = get_posts(array('tag__in' => $tag, 'showposts' => '3'));
foreach($videos as $post) :
setup_postdata($post);
echo "<div class=\"videoframe\" id=\"videoframe\">".woo_get_embed('embed', '520', '293')."</div>";
endforeach;
So, I'm filling up $tag with the "video" tag (post_tag) and make it an array_a. Then, I'm filling up $videos with the posts that have the tag I've put in $tag and I would like the 3 latest.
Then, I foreach the post I got out of $videos I want to embed by using the woo_get_embed function.
Unfortunately, somehow the tag comparison doesn't go well, because it echoes posts without the video tag and because of that it doesn't echo anything at all.
tag__in expects an array with ID's
You are giving it an array with all info about the video.
You want 'tag_id' => $tag['term_id']

Wordpress get_posts (show all attachments) pagination outside of the loop

On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.
Here is the code I am using to dump all the attachments:
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_attachment_link($post->ID, true);
the_excerpt();
}
}
?>
Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?
I've done something similar using the 'offset' parameter for get posts.
Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.
Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.

Resources