I have a CPT in the single-portfolio.php file where I would like to list blog posts according to their category.
That is, the items I have in my CPT called Portfolio are "India", "USA", "Mexico", "Spain" and "Italy". The blog categories are exactly the same.
How can I get the news in the India page to return to me with the India category? And on the USA page the news with category USA...etc
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'usa'
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>
If I understood your question, you are trying to show regular Posts on your CPT post where the name of the CPT Post is the same as the posts category name.
If that's the case you can do this (new lines are commented):
<?php
// Get the post object;
global $post;
// Get the post_name (e.g. slug) of the post
$slug = $post->post_name;
// As long as the slug of your CPT post is the same as the slug of your post category, this works.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
// Use the $slug variable here.
'category_name' => $slug,
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>
Related
Normally in wordpress permalink will open to a single page. but this time the goal is to open the custom post type permalink in modal window. I have lots of custom post type, and I only like to apply the modal in the doctor custom post type .
how could I achieve this? here is my code:
<div class="slider-wrapper">
<div class="center-doc">
<!--start connect to db-->
<?php $args = array(
'post_type' => 'doctor',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
);
$posts = get_posts( $args );
foreach ( $posts as $post ): setup_postdata( $post );
?>
<!--end connect to db-->
<div class="slider-box">
<a href="<?php the_permalink(); ?>"> <!-- PERMALINK TO OPEN MODAL-->
<div class="feature-img"> <?php if (has_post_thumbnail()) : ?> <?php the_post_thumbnail(); ?>
<?php else : ?> <img src="<?php echo get_template_directory_uri(); ?>/img/default-img.png" alt="<?php the_title(); ?>">
<?php endif; ?><!--/pro pic-->
</div>
<h6><?php the_title(); ?></h6>
<div class="content"><?php the_excerpt(); ?></div>
</a>
</div>
<!-- MODAL WINDOW TO OPEN WHEN PERMALINK CLICKED-->
<div id="modal" class="modal-window">
<div>
<i class="fa fa-times-circle" aria-hidden="true"></i>
<div class="detail-wraper">
<div class="detail-info">
<h3><?php the_title(); ?></h3>
<div class="details">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I am using Wordpress 5.9.3 version.
I have a custom post type called "case_studies" and it has 3 categories: "seo", "website-design" and "facebook-advertising".
I have several posts in the main page, and for each post I want to output what categories the post has.
For example, the latest post has a category of "seo" and "facebook-advertising", and I want it to be displayed in the page.
I have tried the get_the_terms(); function but I think I am using it incorrectly.
Here is the code of the page:
<div class="container">
<!--
<div class="category_container">
<p class="category_item" id="all">All</p>
<p class="category_item" id="website">Websites</p>
<p class="category_item" id="facebook">Facebook Ads</p>
<p class="category_item" id="seo">SEO</p>
</div>
-->
<div class="row d-flex">
<?php
$args1 = array( 'post_type' => array('case_studies'), 'order' => 'DESC', 'posts_per_page' => 30, 'orderby' => 'date' );
$loop = new WP_Query( $args1 );
while ( $loop->have_posts() ) {
$loop->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->ID));
?>
<div class="col-sm-4">
<div class="case-study-content">
<a href="<?php the_permalink() ?>" class="blog_blocks">
<div class="b_image">
<img src="<?php echo get_the_post_thumbnail_url(); ?>"/>
</div>
<div class="b_h_sec">
<h2><?php the_title(); ?></h2>
<p><?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?></p>
<span class="r_m">Read More</span>
</div>
</a>
</div>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
Here is how the output looks right now and where I want categories to be displayed.
I am trying to query Wordpress custom posts and categories they are related to.
Query acts strangely, displays category name and all customs posts, not even related to that category (keeps repeating till all categorie names are displayed.)
Example image : Wordpress custom query display
My query code:
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="lookbook-header">
<div class="wrap">
<p class="text-left">lookbook</p>
</div>
</div>
<?php
$taxonomy = 'lookbook_categories';
$terms = get_terms($taxonomy);
$args=array(
'taxonomy' => 'lookbook_categories'
'post_type' => 'lookbook',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) { ?>
<div class="lookbook-category">
<p class="text-center">
<?php echo $term->name; ?>
</p>
</div>
<?php
if( $my_query->have_posts() ) {
echo '';
$count=0;
while ($my_query->have_posts()) : $my_query->the_post();
if($count == 3) {?>
<div class="row">
<?php }
?>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 no-padding">
<div class="lookbook-item">
<div class="hvrbox">
<?php
$image = get_field('lookbook_image');
if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="news" class="img-responsive" />
<?php endif; ?>
<div class="hvrbox-layer_top">
<div class="hvrbox-text">
<div class="separator"></div>
<h3><?php the_title();?></h3>
<div class="separator"></div>
<p>
<?php the_field('excerpt');?>
</p>
</div>
</div>
</div>
</div>
</div>
<?php
$count++;
if($count == 3) echo '</div>';
endwhile;
}
}
endif;
wp_reset_query();
?>
</div>
</div>
I would like to display category name + posts related to category (not all posts over and over again)
You want to pass in an array to get_terms like:
$terms = get_terms( array(
'taxonomy' => 'lookbook_categories',
'hide_empty' => false,
) );
you can read more about it here: https://developer.wordpress.org/reference/functions/get_terms/
I created a Shortcode slider which gets ids of different pages and show display slider. Shordcode works fine but issue is that whenever i am copy past shortcode multiple times on same page/ template page it shows only first one.
This issue occurs only only when i past same type of shortcode but if i past any other shortcode on same page it works fine.
Here is my code
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is shortcode
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
here you can see live example
http://objextheme.wpengine.com/
This one working fine
ROOFTOP PATIO & LOUNGE
but this is not working
This Week At Vertigo Sky Lounge
Please guide me where i am doing mistake. Thanks
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on
same page. that is why only one time it runs perfect.
I have a Wordpress Site or its Blog section I did a WP_Query post.
The loop is within a custom template its showing only one record while I have few post entries.
I think I have done correctly...
Please do let me why it is not looping the records.
<?php get_header();
?>
<div id="body">
<div class="container">
<div class="row-fluid">
<div class="span9">
<?php
$arr = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$query = new WP_Query($arr);
if(count($query->posts) > 0):
$i = 0;
?>
<?php while($query->have_posts()): $i++;?>
<?php $query->the_post();
if(has_post_thumbnail(get_the_ID())){
$url_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
$url_thumbnail_news = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
} else {
$url_thumbnail = '<image src="'.get_template_directory_uri().'/images/no-thumbnail.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
$url_thumbnail_news = '<image src="'.get_template_directory_uri().'/images/no-thumbnail-news.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
}
?>
<?php if($i==1):?>
<div class="drop-shadow lifted"> <a class="nile-thumnail" href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>"><?php echo $url_thumbnail;?></a> </div>
<div class="the_post">
<h2 class="bj-title"><a href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>">
<?php the_title();?>
</a></h2>
<div class="bj-date">
<?php the_date();?>
<?php the_time();?>
</div>
<div class="bj-des">
<?php the_excerpt();?>
</div>
<a href="<?php the_permalink();?>" title="<?php the_title()?>" class="read-more">
<?php _e('Read more', APP_TD);?>
</a> </div>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
<?php // Reset Query
wp_reset_query(); ?>
<div class="navigation">
<div class="alignleft">
<?php previous_posts_link('« Previous') ?>
</div>
<div class="alignright">
<?php next_posts_link('More »') ?>
</div>
</div>
</div>
<div class="span3"> <?php echo get_default_sidebar();?> </div>
</div>
</div>
</div>
<?php get_footer();?>
Try removing the 'if($i==1)' and corresponding 'endif;' line