How to fetch all WordPress posts with featured image? - wordpress

In WordPress 3 there is Featured Image functionality. How do i fetch all posts that have a featured image with them? Here is my current custom loop:
$loop = new WP_Query( array( 'posts_per_page' => 15 ) );

This should work:
$loop = new WP_Query( array( 'posts_per_page' => -1, 'meta_key' => '_thumbnail_id' ) );
I haven't tested this, though. Also, this will probably fetch all posts and pages. Use 'post_type' => 'post' to limit it to blog posts.

I don't believe you need any special loops defined for that to work.
Though you do need to add some small snippets into your functions.php
like this one:
<?php add_theme_support ( 'post-thumbnails' ); ?>
after apply the above code to functions.php file, your theme will support Featured Images and you will see a new link # the bottom right of your Post Add / Edit interface.
This guide will help you, if you are looking for more info regarding this : How to Use Wordpress Featured Image Feature

Related

wordpress one-page website with different php pages

I'm trying to build a wordpress website. I'd like to have a one-page design. I have built 5 pages
each one has its own php file. How can I do to concatenate the 5 pages in one single page?
Thank you in advance for your help,
Fabiana
You may not need to use multiple pages at all but implement inbuilt function for wordpress like shortcodes, or use posts and make multiple queries to the post. You can then design your page (1) in sections and make sure to make $loop = new WP_Query( $args ); to enable multiple queries on the same page. These queries would/should only query post not pages.
Section 1 Layout
$args1 = shortcode_atts (array(
'tag' => 'post-type',
'post_type'=> 'post',
'cat' => '', // Category ID
'posts_per_page' => 1,
'bgimageurl' => ''), $atts);
$query1 = new WP_Query( $args2 );
// The Loop
while ( $query1->have_posts() ) {
$query1->the_post();
the_content();
wp_reset_postdata();
Section 2 Layout
$args2 = shortcode_atts (array(
'tag' => 'userchoice',
'post_type'=> 'post',
'cat' => '', // Category ID
'posts_per_page' => 1,
'bgimageurl' => ''), $atts);
$query2 = new WP_Query( $args2 );
You can take a look at this plugin for wordpress which provides a way to create full screen scrolling pages by using fullPage.js plugin.

How to get Wordpress attachments to work for existing images inside the Media Library?

I want to display the images from each Wordpress post on a separate page.
When using get_children (or get_posts) the 'post_type' => 'attachment' only works if I've just uploaded an image (via WP's 'ADD MEDIA > UPLOAD FILES') to that particular post.
It does not work if I add an existing image to a post that's already in my WP MEDIA LIBRARY).
Is there anyway for 'attachment' to work for existing (already uploaded) images?
See my test function:
function echo_first_image($postID){
$args = array(
'post_type' => 'attachment',
'post_parent' => $postID
);
$attachments = get_children( $args );
if($attachments){
echo'YES'; // test answer
}else{
echo'NO'; // test answer
}
}
EDIT: each 'post_type' that is an 'attachment' has a single 'post_parent' - does this mean an attachment can ONLY have a single parent?
Currently as of writing, Wordpress 4.1 does not include multi-parent attachments because the database relationship is one-to-one, not one-to-many. You would have to find a plugin, or write your own to get around that.

Wordpress - Own plugin - get all images of the media gallery

i've written a plugin for the Wordpress TinyMCE editor and i want to use images which have been previously uploaded to the media gallery in that plugin. How can i access all uploaded pictures in my own plugin? I can't find it in the wordpress docs.
Thanks.
I am slightly not clear on your question. Are you looking for a method to access the Add Media button?
In attempts to answer your question -> This method allows you to get all attachments that is in your media section.. Currently it displays everything but you can manipulate it the way you want to.
Reference: http://codex.wordpress.org/Template_Tags/get_posts
$args = array( 'post_type' => 'attachment', 'numberposts' => -1);
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}

wordpress - excluding a category from a post list in a custom theme that doesn't look like the codex

I'm hoping someone can help. I'm not a php coder, but I've been tweeking and customising a premium theme for wordpress anyway, and I'm stuck.
I'm trying to exclude a specific category from a page which lists all the categories by default. Ok, no problem. It should be:
<?php query_posts($query_string . '&cat=-134'); ?>
right?
I'm pretty sure the category number is 134, but I could be wrong. The premium theme I'm using is called Risen, and there's a lot of different kinds of posts - so maybe what I think is a category is really a tag in a custom taxonomy - in which case ???
When I hover over it in the category listing I get this:
example.com/wp-admin/edit-tags.php?action=edit&taxonomy=risen_multimedia_category&tag_ID=134&post_type=risen_multimedia
I'm pretty sure I've found where I need to include my argument, and that is here in the template:
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
) );
I've tried adding
'tag' => -134
to this array to no avail.
Being a premium, and apperently tweaked, theme there is a lot of guessing here but I think you have talked yourself into the solution, except for one detail. Use tag__not_in not tag=-134
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
'tag__not_in' => array(134)
) );
tag_id=-134 might work (I'd have to test it) but tag expects the tag slug not an ID.
tag (string) - use tag slug
http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

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