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);
Related
First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) ) {
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post ) {
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
}
echo '</ul>';
}
?>
Any Idea why it is not working?
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
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; ?>
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;
?>
Here is my webpage: http://dastousgroupeconseil.com/faq-2/
Here is the complete code of my page: http://pastebin.com/PQUsYdha
I used this php code to display the excerpt and the hyperlink of my posts, but some of them don't want to display. Is it because I reset the $post variable after each endforeach?
<?php global $post;
$args = array( 'numberposts' => 4, 'offset'=> 0, 'category' => 140 );
$myposts = get_posts( $args );
foreach( $myposts as $post) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" target='_blank' ><?php the_excerpt(); ?></a>
<?php endforeach; wp_reset_postdata(); ?>
I've had issues using setup_postdata a few times myself, I tend to skip it altogether and just use the retrieved $post objects.
So for the permalink:
<?php echo get_permalink($post->ID); ?>
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