I am trying to get the categories in a post like this example if the post is added in cate1, cate2, and cate3 then the post-show 3 categories like cate1, cate2, cate3 if the post is posted in one category then just show the cate1 without a comma. But my code $slug = $category->slug; only shows one category in the post. here is my full code.
<?php
$mi_args = array(
'post_status' => 'publish',
'posts_per_page' => '99999',
'post_type' => 'blog'
);
$mi_query = new WP_Query($mi_args);
if($mi_query->have_posts()):
while ($mi_query->have_posts()): $mi_query->the_post();
$portfolio_url = get_post_meta( get_the_ID(), 'portfolio_url_portfolio-url', true );
$hover_color = get_post_meta( get_the_ID(), 'hove_color_hover-color', true );
// Getting the category slug
$work_category = get_the_terms( get_the_ID(), 'blog_categories' );
foreach ($work_category as $category)
{
$slug = $category->slug;
?>
<div class="portfolio-item <?php echo $slug; ?>" data-category="<?php echo $slug; ?>">
<?php
}
?>
<?php endwhile; endif; wp_reset_postdata(); ?>
If I understand your question, you want to take an array of category slugs, and put them as a comma separated list?
If that's right, then you want to push your slugs to an array, and then implode it to a list.
<?php
$mi_args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'blog',
);
$mi_query = new WP_Query( $mi_args );
if ( $mi_query->have_posts() ) :
while ( $mi_query->have_posts() ) :
$mi_query->the_post();
$portfolio_url = get_post_meta( get_the_ID(), 'portfolio_url_portfolio-url', true );
$hover_color = get_post_meta( get_the_ID(), 'hove_color_hover-color', true );
// Getting the category slug.
$work_category = get_the_terms( get_the_ID(), 'blog_categories' );
$slug = array();
foreach ( $work_category as $category ) {
$slug[] = $category->slug;
}
$slug_list = implode( ',', $slug );
?>
<div class="portfolio-item <?php echo esc_attr( $slug_list ); ?>" data-category="<?php echoesc_attr( $slug_list ); ?>">
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Related
I have created a wordpress query to return all the "products" but it only returns 9. (weird number, I know). I have a total of 13 custom posts for "products". I have added post_per_page = -1 and post_per_page = 20. Both have not made a difference.
Here is my loop. Is there a way to debug it to see how it's breaking? Or does anyone know why it's breaking? Thank you
<?php
// WP_Query arguments
$args = array(
'p' => 'products',
'post_type' => array( 'products' ),
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'custom_product_position',
'post_per_page' => 20,
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$product_title = get_post_meta( get_the_ID(), 'custom_product_title', true );
$product_subtitle = get_post_meta( get_the_ID(), 'custom_product_subtitle', true );
$product_id = get_post_meta( get_the_ID(), 'custom_product_id', true );
?>
<div id="<?php if ( ! empty( $product_id ) ) {echo $product_id;}?>" class="section-title">
<?php
if ( ! empty( $product_title ) ) {
echo "<h2>" . $product_title . "</h2>";
}
?>
<?php
if ( ! empty( $product_subtitle ) ) {
echo "<h4>" . $product_subtitle . "</h4>";
}
?>
</div>
<?php
echo get_the_content();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
There's also the limit that you set within the WP settings for the maximum amount of posts displayed on post loops in general ("Settings > Read"). Try to increase that - should work...
i have created custom post type named "Person" and i need to get all attachments ID's separated by commas of current post in loop.
Here i have a code that loops the "person" post type posts and code that shows every posts attachments ID's, but thing is that that those ID returns like this "2713271227112710" i need it to return seperated by commas like this "2713,2712,2711,2710"
Here is my code:
<?php
$args = array(
'post_type' => 'person',
'post_status' => 'publish',
'posts_per_page' => 24,
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-4">
<div class="person">
<?php if ( $post->post_type == 'person' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) ); ?>
<?php
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
}
}
?>
<?php echo do_shortcode( '[vc_images_carousel images="'.$attachment_id.'" img_size="large" speed="3000" autoplay="yes" hide_pagination_control="yes" hide_prev_next_buttons="yes" wrap="yes"]' ); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
Use implode in combination with an array_map, to return an array of IDs, and then convert it to a string of IDs, separated by a comma.
So instead of this:
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
Try this:
$attachment_ids = implode(',', array_map(function($attachment){
return $attachment->ID;
}, $attachments));
echo $attachment_ids;
I have created a custom post type named "State Leagues" using types plugin. My post type has some categories and outputs both. When I display the posts' titles it displays correctly. My code for displaying post titles is this:
<?php
$args = array(
'post_type' => 'stateleague-pos-type',
'posts_per_page' => 4,
'order_by' => 'post_date',
'order' => 'DESC'
);
$loop = new WP_Query($args);
// $counter = 0;
// $big = 1;
while ($loop->have_posts()) : $loop->the_post();
?>
<a href="<?php the_permalink(); ?>">
<h1> <?php the_title(); ?> </h1>
</a>
?>
Now I want to display the categories' names as the title instead of the posts' titles. I have searched for it but didn't find anything that works. Is it possible to display category names as titles?
<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Or in fuctions.php Put this:
function get_the_category_custompost( $id = false, $tcat = 'category' ) {
$categories = get_the_terms( $id, $tcat );
if ( ! $categories )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return apply_filters( 'get_the_categories', $categories );
}
and call the function as:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>
Try this i hope it will help
I'm trying to get the attachments of a specific term (in its archive page).
But the results are showing the resulting images 5 times instead of one.
I have multiple loops in this page - one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can't show the attachments in the right way. :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'
I've done these and its working!
The result will show all attachments in a specific term inside term's archive page.
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>
https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination
I'm using this in my page template to get posts by their category:
<?php
if (is_page(19)){
?>
<ul>
<?php
global $post;
$args = array( 'category' => 'Testimonial' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
but it's retrieving all posts instead. Not just the ones labeled Testimonial. Any idea what I'm doing wrong?
'category_name'=>'this cat' also works but isn't printed in the WP docs
Check here : https://developer.wordpress.org/reference/functions/get_posts/
Note: The category parameter needs to be the ID of the category, and
not the category name.
You can use 'category_name' in parameters.
http://codex.wordpress.org/Template_Tags/get_posts
Note: The category_name parameter needs to be a string, in this case, the category name.
add_shortcode( 'seriesposts', 'series_posts' );
function series_posts( $atts )
{ ob_start();
$myseriesoption = get_option( '_myseries', null );
$type = $myseriesoption;
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endwhile;
echo '</ul>';
}
wp_reset_query();
return ob_get_clean(); }
//this will generate a shortcode function to be used on your site [seriesposts]
Create a taxonomy field category (field name = post_category) and import it in your template as shown below:
<?php
$categ = get_field('post_category');
$args = array( 'posts_per_page' => 6,
'category_name' => $categ->slug );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
//your code here
<?php endforeach;
wp_reset_postdata();?>