Wordpress WP_Query - php

For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.
I am using WpAlchemy and Custom Image Sizes plugin.
<section id="new">
<?php $myquery = new WP_Query(array('post_type' => array('post', 'website_gallery'),'showposts' => '2'));
while ($myquery->have_posts()) : $myquery->the_post();
global $custom_metabox;
?>
<div class="latest hentry">
<h2><?php the_title(); ?></h2>
<?php $website_gallery->the_meta(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php $website_gallery->the_value('galleryimage');?>" alt="<?php the_title(); ?>">
</a>
<?php echo wp_get_attachment_image($attachment_id, '220x80'); ?>
</div>
<?php endwhile; wp_reset_query(); ?>
</section>

I think you can get the attachment id by using the get_post() function. get_post() requires an array for it's parameter and that array can be something like this:
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
Since you want attachments, make sure your 'post_type' => 'attachment'.
You can then do:
$attachments = get_post( $args );
if( $attachments ) {
foreach( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, '220x80' );
}
}
Adapt that code for what you need. Hopefully it'll work for you.
Check out: http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

Related

Post text breaks in post with [...] Wordpress development

I am receiving all posts from Wordpress but the text in the posts breaks like this:
Image of the line break
Here is a code snip:
<?php
global $post;
$args = array(
'posts_per_page' => 10,
'orderby' => 'date'
);
$postslist = get_posts( $args );
if($postslist) {
foreach ( $postslist as $post ) :
setup_postdata( $post );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$content = apply_filters('the_content', $post->post_content);?>
<h2><?php the_title(); ?></h2>
<p><?php the_date(); ?></p>
<img id="blogtemplate-image" src="<?php echo $url ;?>" />
<p><?php echo $content; ?></p>
<?php endforeach; }
wp_reset_postdata();
?>
How can I avoid the break and just getting all the content?
Thanks in advance!
I found the answer myself. Instead of getting the_excerpt() I have to use the_content(). The_excerpt return only 55 characters or so:
<?php
global $post;
$args = array(
'posts_per_page' => 10,
'orderby' => 'date'
);
$postslist = get_posts( $args );
if($postslist) {
foreach ( $postslist as $post ) :
setup_postdata( $post );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<h2><?php the_title(); ?></h2>
<p><?php the_date(); ?></p>
<img id="blogtemplate-image" src="<?php echo $url ;?>" />
<p><?php echo the_content(); ?></p>
<?php
endforeach;
}
wp_reset_postdata();
?>

Php with WordPress how to call up permalinks to two separate posts

i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;

Wordpress Get Posts not working as expected

I'm trying to display 6 posts from a specific custom post type in Wordpress. Everything is working, except for when I remove the line "$wp_query = new WP_Query();". When missing that line, 20 posts gets displayed in alphabetical order and I have no idea why. wp_reset_postdata() or wp_reset_query() don't seem to do anything.
<?php $wp_query = new WP_Query( ); ?>
<div class="blog-footer row margin-top">
<?php
global $post;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-2 col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Happy to get any inputs on why this behaves the way it does. Appreciate it!
global $post is a global variable mostly used by wordpress itself and is used to get contents/ attributes of current post or page mostly..
Read details here
https://codex.wordpress.org/Function_Reference/$post
try below code.
<div class="blog-footer row margin-top">
<?php
wp_reset_query() ;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $b_post ) { ?>
<div class="col-md-2 col-sm-4">
<a href="<?php $b_post->post_name; ?>">
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $b_post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0] ?>"/>
<h4><?php $b_post->post_title; ?></h4>
</a>
</div>
<?php }
wp_reset_postdata();?>
</div>

Wordpress Query posts wrap each item in a div rather than an li and show descendants of current page

I am using the following to wrap through a list of posts as I want to display them within divs.
Despite using
global $post;
$currentPage = $post->ID;
and
'post_parent' => $currentPage,
Which has worked fine elsewhere. I can't get the page to only show children and grandchildren of this page.
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'post_parent' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
I am using the code within a custom template.
I have also tried
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'child_of' => $currentPage,
'post_type' => 'page'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
but this lists the pages I want followed by what appears to be 10 other random pages from the site's root though it doesn't list every page from the site.
Originally posted this solution as a comment, because I wasn't sure it was the only change that was necessary. Turned out it is, so here's the solution:
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The function get_pages doesn't seem to have post_parent as a valid argument. So you need to use child_of instead.
Reference to Codex
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of ' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
https://codex.wordpress.org/Function_Reference/get_pages
To list All sub pages of current page
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
only change on code
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
which display all child pages of current page with their Menu order
About Us
Contact Us
Gallery
Testimonial
Blog

Access the_post() contents in a custom sidebar

I have this code in single-legislacion.php page:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php endwhile; ?>
<?php endif; ?>
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $thePostID,
'post_status' => 'inherit',
'numberposts' => 1
);
$attachments = get_posts( $args );
if ( $attachments ) :
foreach ( $attachments as $attachment ) : ?>
<div class="BaseIco">
<a class="IcoDescargas" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
<img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png"><br>
Descargar PDF
</a>
</div>
<?php endforeach;
endif;
?>
Because I am using Table of Content Plus plugin and have not found a way to display the generated TOC outside of the_content() itself, the only solution I have is to display it through a widget on a sidebar. Now, my question is: can I access the_post() content, like attachments for example, in a custom sidebar? How?
Also if any knows any variant to show the TOC outside the content, I'll be graceful if can share it.
No, I don't think so. the_post() only works inside of the loop, sidebars are usually rendered outside of it.
However, you can use get_post with global $post, i.e.:
global $post;
$p = get_post($post->ID);

Categories