I am having some trouble building the category template i want. Here is an example of one of my categories: http://transcorrect.bg/pi-de/category/versicherungen/
I want this field http://prntscr.com/k2d5xg to show posts only from the current category. I am attaching the code i am using right now, but it is only for specific category. What i want is to be on archive.php file as a code, that takes dynamically the category ID.
Minimal example of code
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?php
// get the top-category posts
$the_query = new WP_Query(array(
'category_name' => 'Versicherungen',
'posts_per_page' => 10,
));
if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div id="list-news-home" class="col-lg-12" style="padding-top:10px;padding-bottom:10px;">
<img src="http://transcorrect.bg/pi-de/wp-content/uploads/2018/07/arrow1.png" style="padding-right:20px;"><?php the_title(); ?>
[ mehr ]
</div>
<hr style="margin-top:50px;margin-bottom:10px">
<?php endwhile; ?>
<?php endif; ?>
</div>
I can't figure out how to get it so if someone has made something similar i will be thankful to know how :)
I have worked it out.
If anyone needs similar thing i am posting how i converted the code i posted earlier.
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?php $current_cat_id = get_query_var('cat'); $showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
" style="color:#00315C">
" style="float:right"> [ mehr ]
<?php endwhile; ?>
<?php endif; ?>
Related
I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>
I want to display all posts for a specific custom post type with a specific category. Right now, all posts from the specified post type are showing, but none show once I add a category id to the $args array. I am trying to get the Feature category to display, id shown in backend url is 3156. I currently have three posts in the Feature category (they are displaying correctly on the loop-category page).
<?php
$args = array(
'post_type' => 'technology_articles',
'cat' => 3156,
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
?>
<?php if( $my_query->have_posts() ):?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div style="background: url('<?php echo get_the_post_thumbnail_url(); ?>');background-size:cover;background-position: center;">
<a class="featured--link" href="<?php echo the_permalink(); ?>"></a>
<div class="slide-overlay">
<div class="category-tag">
Feature
</div>
<h2><?php echo the_title(); ?></h2>
<div class="posted">
Posted on <?php the_time('M j, Y'); ?> by <?php the_author_posts_link(); ?>
</div>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php
endif;
wp_reset_query();
?>
I needed to use tax_query. See below:
<?php
$args = array(
'post_type' => 'technology_articles',
'tax_query' => array(
array(
'taxonomy' => 'technology_category',
'terms' => 3156
)
),
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
?>
I try to build an option for the user to highlight "related posts" to be shown at the bottom of a single post, so I put this code at the bottom of "content-single.php":
<div class="related-post">
<?php
$args = array( 'post__in' => array(38328, 38359, 21321) );//it's going to be the user choice, for example, I put three id's
$related_query = new WP_Query($args);
if ($related_query):
echo '<div class="row">';
while ($related_query->have_posts()): $related_query->the_post();?>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()):
the_post_thumbnail();
endif;
?>
<?php the_title() . '<br/>'; ?>
</div>
<?php endwhile;
echo '</div>';
wp_reset_postdata();
?>
</div>
But in some posts, I get 8 diefferent posts, in some posts I don't get any posts (though I put only three id's in post__in).
I guess the problem is that I try to loop inside the main loop.
I tried with query_posts and with get_posts, doesn't work.
Any help will be appreciate!
I think that is related to the missing post_type argument. Yet you need some corrections in your loop structure:
<div class="related-post">
<?php
$args = array( 'post_type' => 'your_post_type', 'post__in' => array(38328, 38359, 21321) );
$related_query = new WP_Query($args);
if( $related_query->have_posts() ){ ?>
<div class="row"><?php
while( $related_query->have_posts() ){ $related_query->the_post();?>
<div class="col-sm-4"><?php
if( has_post_thumbnail() ) the_post_thumbnail();
the_title();?>
</div><?php
}
wp_reset_postdata();?>
</div><?php
}?>
</div>
UPDATE
Please try to add 'posts_per_page' => 3 to the args array.
I have a solution to this post. Hope so you are wrong with the query post.
<div class="related-post">
<?php
$args = array(
'post_type' => 'your_post_type',
'post__in' => array(38328, 38359, 21321),
'post_status'=>'publish'
);
$related_query = new WP_Query( $args );
?>
<?php if ( $related_query->have_posts() ) : ?>
<div class="row">
<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
<div class="col-sm-4">
<!-- do stuff ... -->
the_post_thumbnail();
the_title();
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
</div>
Hope so it helps you to fetch out your Output Required.
Make sure you have post in the ID that you have mentioned in the post_in section.
Well, I manage to solve this by adding this 'ignore_sticky_posts' => 1 to the arguments. thanks #Pieter Goosen
I had a categories with subcategories. After reorganize all categories, remove the subcategories and move post from some categories to other categories (with Bulk Move plugin), some categories in category.php show wrong content, mixing post from two categories.
For example, category "mistery" show post from "history, mistery". If I search one of these post that are showing incorrect in two categories, the assigned category is correct at wordpress backend
Other categories show posts correctly.
I think that this error it is happening with old subcategories.
Can somebody help me? Thank you.
This is my actual code loop in categories.php
<!-- loop de post normales-->
<?php
wp_reset_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 9,
'category__in' => array($category)
));
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="col-sm-6 col-lg-6 bottomMargin27">
<a class="noLine" href="<?php the_permalink();?>">
<?php the_post_thumbnail('fb') ?>
<div class="articleContainer">
<h4 class="articleTitle"><?php the_title(); ?></h4>
</div>
</a>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); wp_reset_query(); ?>
<?php else : ?>
<?php endif; ?>
<!-- / fin del loop de post normales -->
Try rewriting the query like this
query_posts(
array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 9,
'cat' => $category // This is new
));
My main portfolio page is setup correctly which is displaying ALL the work, the next step I'm having trouble with is displaying work ONLY from a specific category.
Under the custom content types manager I checked "Enable Categories" under Taxonomies which gave me control to add categories to the content type like the URL below.
For example: http://localhost/category/narrative would display all work with the category "Narrative" attached, right now it's displaying all the work since I copy & pasted the code from the work page.
How can I get this category.php template to detect and display the work associated with the category it's loading?
<?php
/**
* The template for displaying Category Archive pages
*
* #package WordPress
*/
$res = get_posts(array('post_type' => 'work', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1));
get_header(); ?>
<section role="main" class="container">
<div id="da-thumbs" class="row work-list da-thumbs">
<? foreach($res as $post) : setup_postdata($post) ?>
<?
$thumbnail = get_custom_field('thumbnail');
?>
<div class="col four">
<a href="<?php echo get_permalink(); ?>">
<img src="<?=$thumbnail?>" />
<div><span><?php the_title( '<h3>', '</h3>' ); ?></span></div>
</a>
</div>
<? endforeach; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('(more...)')); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</section>
<?php get_footer(); ?>
Your get_posts is not filtering by category.
Do something like:
$the_category = get_queried_object();
$cat_id = $the_category->term_id;
$res = get_posts(array('category' => $cat_id, 'post_type' => 'work', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1));