Im trying to show a list of posts including their Title & Image on a post. Im using the get_posts() function with an array to filter the posts. I know that $template_loader->get_template_part( 'content-listing-image'); get the post image, but when I use it on the output it shows me the image of the current post, not the filtered ones ($evento)
This is my code:
<?php
$args = array(
'author' => $post->post_author,
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_listing_type',
'value' => 'event'
)
)
);
$eventos = get_posts( $args );
foreach ( $eventos as $evento ) {
$template_loader = new Listeo_Core_Template_Loader;
$output .= '<div> '.$template_loader->get_template_part( 'content-listing-image').' '.$evento->post_title.'</div>';
}
if(!empty($eventos)) : ?>
<div id="listing-eventosartista" class="listing-section">
<h3 class="listing-desc-headline margin-top-60 margin-bottom-30"><?php esc_html_e('Eventos','listeo_core'); ?></h3>
<div class="single-item">
<?php echo $output; ?>
</div>
</div>
<?php endif ?>
I want to show the corresponding image to each $evento (post).
Hope yoy can help me!
You can use get_the_post_thumbnail(), like this
$output .= 'ID ).'" target="_blank"> '.get_the_post_thumbnail( $evento->ID).' '.$evento->post_title.'';
Related
please help me with this annoying issue as i'm junior in wordpress , i have a different slider in my project and i made a custom post type to make attachment posts on in to make every single post as a slider .. the problem is how to loop inside CPT to retrieve every single post when i want is to display on front-end i tried alot and searched on net about it but the only code that i found and i used it made every single post to override the first post .. this is my code
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
'fields' => 'ids'
));
$image_query = new WP_Query(array(
'post_type' => 'attachment',
'post_status' => 'puplish',
'post_mime_type' => 'image',
'posts_per_page' => 3,
'post_parent' => $query->id,
'order' => 'DESC'
));
?>
<?php if( have_posts( )) : ?>
<?php while ($image_query->have_posts()) : $image_query->the_post( ); ?>
<div class="item owl-slide"> <img src="<?php echo wp_get_attachment_url( get_the_ID() ); ?>" /> </div>
<?php endwhile; wp_reset_query( );?>
<? endif;?>
If you want to create a slider, set the featured image for every slider post and use that image in your WP_Query loop.
<?php
$query = new WP_Query( array(
'post_type' => 'owlgalleryslider',
'posts_per_page'=> 3,
));
?>
<?php if( have_posts( )) :
while ($query->have_posts()) : $query->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="item owl-slide"> <img src="<?php echo $image; ?>" />
</div>
<?php endwhile; wp_reset_query( );
endif;
?>
I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)
Here is the code:
<?php
$tags = get_tags('order=ASC&orderby=name);
foreach ( $tags as $tag ) { ?>
<?php $tag_name = $tag->slug;
echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';
echo '<div class="green_txt font20">' .$tag->name. '</div>';
//echo $tag_name;
$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );
query_posts( $args );
// The Loop
?>
<div class="row">
<?php
while ( have_posts() ) : the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
$thumb_url = $thumb_url_array[0];
$post_id = get_the_ID();
?>
<div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
<div class="partners_container">
<div class="row" style="height:125px;">
<div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%" alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>
</div>
<div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo '<div>' . the_title() . '</div>';?> </div>
<div class="padtop10 left"> <?php echo '<div>' . the_field('partner_page_description', $post_id ) . '</div>';?> </div>
</div>
</a> </div>
<?php
endwhile;
?>
</div>
</div>
<?php
wp_reset_postdata(); }?>
If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!
Thanks muchly!
This is a basic example of WP_Query which should get you started:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'slug',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'field' => 11
),
array(
'taxonomy' => 'tag',
'field' => 'term_id',
'field' => array( /* Your Tag IDs */ ),
'operator' => 'IN'
)
)
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) :
$post_query->the_post();
// Inner loop stuff code
endwhile;
endif;
wp_reset_postdata();?>
get_tags function accepts include argument. You could pass by it ids of tags you want to get.
$tags = get_tags('order=ASC&orderby=name');
change to
$tags = get_tags('order=ASC&orderby=name&include=12,45,67');
12, 45 and 67 are ids of tags
get_tags on codex
I've written some code which automatically creates some posts and adds a tag to them. I can see the tags in the 'All posts' admin panel and I can click on the posts 'Tag' link to get just those posts with the tags.
Here is my code:
<?php $tag_ID= single_tag_title();
$args = array(
'post_type' => 'post',
'tag_id' => $tag_ID,
'posts_per_page' => 10,
'order' =>'ASC'
);
$posts = get_posts( $args );
var_dump($args );
foreach ( $posts as $post ) {
?>
Can you help me to get all tag posts?
Thank you.
Create a new file (tag.php) in wp-content/themes/yourthemefolder/
and put below code in it.
<?php
get_header();
$tag = single_tag_title('', false);
echo '<h1>Tag: ' . $tag . '</h1>';
$args = array(
'post_type' => 'post',
'taxonomy' => $tag,
'terms' => $tag,
'posts_per_page' => 10,
'order' => 'ASC'
);
$postslist = get_posts($args);
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div id="post">
<h2>Post title:<?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php endforeach;
get_footer(); ?>
I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query
I`ve found sth like this:
Is it possible to create a shortcode that will query a post based on taxonomies
function posts_shortcode_handler($atts, $content) {
extract(shortcode_atts(array(
'posts_per_page' => '5',
'post_type' => 'gallery'
), $atts));
global $post;
$temp = $post;
$posts = new WP_Query($atts);
$retVal = '';
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
// these arguments will be available from inside $content
$parameters = array(
'PERMALINK' => get_permalink(),
'TITLE' => get_the_title(),
'CONTENT' => get_the_content(),
'CATEGORIES' => get_the_category_list(', '),
'THUMBNAIL' => get_the_post_thumbnail()
);
$finds = $replaces = array();
foreach ($parameters as $find => $replace) {
$finds[] = '{' . $find . '}';
$replaces[] = $replace;
}
$retVal .= str_replace($finds, $replaces, $content);
}
}
wp_reset_query();
$post = $temp;
return $retVal;
}
add_shortcode('galerie', 'posts_shortcode_handler');
My shortcode looks like this:
[galerie post_type="gallery" posts_per_page="5" taxonomy_name="movies"]
<h5>{TITLE}</h5>
<div>{THUMBNAIL}
{CONTENT}</div>
[/galerie]
My problem is about the taxonomy_name="movies" that`s not working for me.
In my custom taxonomy name 'Kategorie' I have two sub categories 'movies' and 'photos'.
Shortcode ignores choosen 'taxonomy_name' and display all custom posts from post_type="gallery". I would like to choose sub category in my custom_taxonomy to display custom post type from shortcode.
Please Help me, I`m stuck :(
Use this Code your wp page editor. this is function input:
add_shortcode( 'list-slider', 'slidermy' );
function slidermy( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'slider',
'color' => 'blue',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $query->have_posts() ) { ?>
<div class="home-slider">
<div class="container">
<div class="cycle-slideshow home-slideshow" data-cycle-slides="> div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
<?php
while ( $query->have_posts() ) : $query->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
<div class="caption">
<div class="con">
<h1><?php the_title(); ?></h1>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
add_shortcode( 'list-slider', 'slidermy' );
function slidermy( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'slider',
'color' => 'blue',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $query->have_posts() ) { ?>
<div class="home-slider">
<div class="container">
<div class="cycle-slideshow home-slideshow" data-cycle-slides="> div" data-cycle-pager=".home-pager" data-cycle-timeout="5000" data-cycle-prev="#HomePrev" data-cycle-next="#HomeNext">
<?php
while ( $query->have_posts() ) : $query->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="slide" style=" background-image:url(<?php echo $imgurl;?>)">
<div class="caption">
<div class="con">
<h1><?php the_title(); ?></h1>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
You have to replace "taxonomy_name" with the name of your taxonomy.
The first line of your shortcode should look like this:
[galerie post_type="gallery" posts_per_page="5" Kategorie="movies"]
That way, this is the $atts array passed to WP_Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => '5',
'kategorie' => 'movies'
);
More details here:
http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters