Apologies in advance for a question, which to an expert, I've no doubt is relatively obvious. I've looked at WordPress Codex for the appropriate information (the_post_thumbnail, the_excerpt, etc) but am not well versed enough in .php yet to implement it properly. Still learning!
I am trying to display, within the of a standard (WP) page, the child-pages, including their Title, Thumbnail and Excerpt. I can get everything to work bar the THUMBNAIL and EXCERPT with the following:
<div class="child-pages">
<?php
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
?>
<!-- loop: child page -->
<div class="child">
<header class="entry-header">
<?php echo '<h3>'.$page->post_title.'</h3>'; ?>
</header><!-- .entry-header -->
<img src="<?php echo the_post_thumbnail_url( $page->ID ); ?>">
<?php echo $page->the_excerpt; ?>
</div>
<?php } ?>
</div>
So far, I can see the links/titles of the correct child-pages, and in the correct order, but not the Thumbnail or Excerpt. Obviously, I am not calling the Thumbnail or the Excerpt properly. Could someone please correct me?
I have also tried these lines, as supported by the twenty-sixteen theme:
<?php twentysixteen_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Any help would be much appreciated!
The thumbnail doesn't show because the function the_post_thumbnail_url(); shows the thumbnail of the current post, the parameter is to specify the size of the image, not the post.
$post->the_excerpt is not necessarily filled. If you look at the add/edit post screen you'll notice two textfields, one for editing the content of the post and one for the excerpt. The excerpt is optional, so the function the_excerpt() shows the content of that field, but when it is empty it will show the first X characters of $post->the_content.
Regarding your second attempt: the functions the_excerpt(), get_permalink() and twentysixteen_post_thumbnail() don't work because you don't set up the post properly.
The easiest way would be to add setup_postdata() to your code:
$pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
foreach( $pageChild as $page ) {
setup_postdata( $page );
// now you can use all the fancy functions like `the_excerpt()`
// add your output here
}
Or you can do it the recommended way, using a WP_Query object:
<?php
$args = array(
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Related
I have a working '$related = get_posts' masonry on a single.php page. I also added a hover overlay, so that when the user hovers on the thumbnail, a transparent overlay appears as well as the descriptions (title name, category and nickname).
The problem I am facing is that when I hover on one related post thumbnail, the overlay appears for every post (the overlay is stretched, it is not appearing individually). I've also attempted to call out the descriptions, but it's only calling the current post I am viewing (e.x. the current single.php header says Snow, when I hover the first related post, it also calls out the description for Snow), however, if you click on the first related post thumbnail, it takes you to a different article (it is not calling the description for the different article).
<div class="related">
<h3>Related</h3>
<div class="js-masonry">
<div class="overlay">
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'orderby' => 'rand', 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<?php the_post_thumbnail(array(300,300)); ?>
<?php } wp_reset_postdata(); ?>
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
</div>
</div>
</div>
As the title says, how do I pull the correct description and overlay for one post via hover for ' $related = get_posts' on the single.php page in WordPress?
I resolved the issue by reorganizing the code correctly.
<div class="js-masonry">
<?php $args = array(
'category__in' => wp_get_post_categories( get_queried_object_id() ),
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( get_queried_object_id() )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item-masonry overlay">
<a href="<?php the_permalink();?>">
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
<?php the_post_thumbnail(array(300,300)); ?>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
Hi i have this wordpress site and the pages will display images on it. Now my problem is when i add post_per_page => '50' then when i refresh the page the number of posts per page is not rendering correctly. Here is my code below.
<?php
query_posts( array(
'post_per_page' => 50,
'cat'=> '7',
'order' => 'ASC'
) );
?>
<?php while(have_posts()) : the_post(); ?>
<div class="single-gallery anim-5-all interoors masonryImage mix span-4">
<div class="img-holder">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<img src="<?=$url; ?>" alt="">
</div>
</div><!-- /.single-gallery -->
<?php endwhile; wp_reset_query(); ?>
Now it will display 9 images on the page. Can someone help me figured this thing out? Any help is muchly appreciated. TIA
For a first, I fond mistake in your query arguments
'post_per_page' => 50,
There is no such argument for wp_query loop use instead it
'posts_per_page' => 50,
For a second there is quote from a wodpress codex about query_posts function
Note: This function will completely override the main query and isn’t
intended for use by plugins or themes. Its overly-simplistic approach
to modifying the main query can be problematic and should be avoided
wherever possible. In most cases, there are better, more performant
options for modifying the main query such as via the ‘pre_get_posts’
action within WP_Query.
I can recomend you to use standard wordpress loop
<?php
$args = [
'posts_per_page' => 50,
'cat' => '7',
'order' => 'ASC',
];
// The Query
$query = new WP_Query( $args );
// The Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="single-gallery anim-5-all interoors masonryImage mix span-4">
<div class="img-holder">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$url = $thumb['0'];
?>
<img src="<?=$url; ?>" alt="">
</div>
</div><!-- /.single-gallery -->
<?php
endwhile;
wp_reset_postdata();
?>
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);
[SOLVED]
I'm trying to integrate Flex Slider (flexslider.woothemes.com) with WordPress. I've included all JS/CSS files needed.
Then, I added this code to show the featured images of a certain category - the famous WP_Query with $args set to a certain category. Then using echo the_post_thumbnail(); I'm showing the posts' images. It's working fine. I just need to make the images linked to post URLs (imgs are hrefed). Please provide help and thanks in advance.
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider ?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php }
// The Slide's Image
echo the_post_thumbnail();
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php
// Reset Post Data
wp_reset_postdata();
?>
New code that's working:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider
?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php }
// Reset Post Data
wp_reset_postdata();
?>
If I understand what you're asking, I think the answer is
echo '<a href="'. the_permalink() .'">';
echo the_post_thumbnail();
echo '</a>';
I want to show some posts in the homepage of my wordpress site..How will I do it??or is there any plugin that can help me do it?or is there shortcodes that could pull out and display those post in my homepage?
If you want to do it the easy way, you can use a plugin like Display Posts Shortcode.
Or, if you want to do it manually, you can use get_posts().
Here's an example you could use:
<?php
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<?php the_title(); ?></h2>
<?php endforeach;
}
}
?>
I hope this helps!
The implemetation varies greatly by theme.
Check if your wordpress theme has a file called index.php.
If you have this file for your current theme, this is the file responsible for displaying your home page. And this is where you will have to put the code snippets to display posts.
Presuming that you know a bit of html and PHP you will have to decide the suitable place within index.php to add the code suggested above by Amal Murali.
If you want to show a specific category post on Homepage you can use category slug or category name. Like the below to show and use the wp_pagenavi() plugin to show pagination and present it.
<?php
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'CATEGORY NAME ',
'posts_per_page' => 5,
'paged' => $paged,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $arr_posts,
)
);
endif;
?>