Basically I am trying to get the gallery of parent page, and I've been playing with query_posts to do that, the following code seems to be getting me closer to what I need but it is actually getting the attachments from other places rather than only parent page of current page, any one?:
<?php
$args = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0,
'order_by' => 'menu_order',
'order' => 'ASC');
$attachments = get_posts($args);
if($attachments)
{
echo '<ul class="imagelist">';
foreach($attachments as $attachment)
{
echo '<li>';
$large = wp_get_attachment_image_src($attachment->ID, 'large');
$thumb = wp_get_attachment_image($attachment->ID, 'thumbnail');
echo '' . $thumb . '';
echo '</li>';
}
echo '</ul>';
}
?>
You should have set post_parent to the current post parent ID:
global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->post_parent,
'order_by' => 'menu_order',
'order' => 'ASC'
);
Related
I trying create a code that will retrieve the first post from a category and echo the link.
But, the code always getting permalink of current post. Can anyone help me to fix it?
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$array = new WP_Query(array(
'category__in' => array($category),
'post_per_page' => 1,
'order' => 'asc',
'orderby' => 'id'
));?>
<?php the_permalink($post->ID); ?>
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'ASC',
'fields' => 'ids',
'category' => 8 //or use further logic to check in list
);
$post_cus = get_posts($args);
$first_post_id = $post_cus[0];
$post_url = get_the_permalink ($first_post_id);
<?php
$pages = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'orderby' => 'meta_value_num',
'posts_per_page' => 4
) );
echo $post_id = $pages->ID ;
echo $post_thumbnail_id = get_post_thumbnail_id( $post_id );
?>
In above, code I want to skip my first post and display rest of.
Try this :
use offset parameter of get_posts() argument.
$pages = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'orderby' => 'meta_value_num',
'posts_per_page' => 4,
'offset'=>'1' //USE OFFSET PARAMETER
) );
//echo '<pre>';
//print_r($pages); //Retrieve post lists
echo $post_id = $pages->ID ;
echo $post_thumbnail_id = get_post_thumbnail_id( $post_id )
Is it possible to get an array of all wordpress child page IDs that are no using a certain template?
I know I can get the template with:
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
and I expect I could use get_pages, but not sure how I can put the query together.
This works, but is there a better way of doing it?
$args = array(
'post_parent' => $parent_ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
$exclude_children = null;
foreach($children as $child) {
$metadata = get_post_meta($child->ID);
if($metadata['_wp_page_template'][0] == 'template.php') {
$args = array(
'post_parent' => $child->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$exclude_array = get_children( $args );
foreach($exclude_array as $exclude) {
$exclude_children .= $exclude->ID.',';
}
}
}
I want to implement a variation of the code below. This code currently shows a post's attachment title.
How I can modify the code so if there is no attachment it shows the post title.?
This is code for pulling first attachment title.
<?php $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => 1, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_title = $attachment->post_title;?>
<?php echo $image_title; ?><?php } } ?>
Something like this?
<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => 1, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_title = $attachment->post_title;
if($image_title) {
echo $image_title;
} else {
the_title();
}
}
}
The best way to do this would be the empty() function and a ternary expression
$image_title = ( ! empty( $attachment->post_title) )?
$attachment->post_title: // use the attachment title if it is not empty
$post->post_title; // otherwise use the post title
How can I get images to have the alt set to "slider"?
This is my code:
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 5,
'post_mime_type' => 'image'
);
if ( $images = get_children( $args ) ) {
echo '<div id="sliderbody" style="width:640px; height:480px;"><div id="slider">';
foreach( $images as $image ) {
echo wp_get_attachment_image( $image->ID, 'trueslider' );
}
echo '</div></div>';
}
I guess you're trying to set the alt attribute to always be "slider" so try this
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 5,
'post_mime_type' => 'image'
);
if ( $images = get_children( $args ) ) {
echo '<div id="sliderbody" style="width:640px; height:480px;"><div id="slider">';
foreach( $images as $image ) {
echo wp_get_attachment_image( $image->ID, 'trueslider', false, array('alt' => 'slider') );
}
echo '</div></div>';
}