Wordpress show latest category posts from custom query - php

in my category.php template i want to show the latest entries only if are of the same category, and when i execute the custom query shows every entries.
This is my code, how could i fix the error?
Code:
<?php
/**
* The template for displaying all single posts.
*
* #package Mundo Geek
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main-single" class="site-main" role="main">
<div id="row">
<?php
$ultimas = new WP_Query();
$ultimas -> query('showposts=3');
while($ultimas -> have_posts()) : $ultimas ->the_post();
?>
<div class="category_page_last col-xs-12 col-sm-4">
<a href="<?php the_permalink(); ?>">
<div class="thumb">
<?php
if(has_post_thumbnail()){
//echo '<img src="'.$url.'"/>';x
$backgroundImageUrl = "('".wp_get_attachment_url( get_post_thumbnail_id($post->ID) )."')";
echo '<div class="category_page_img-background" style="background-image: url'.$backgroundImageUrl.'"></div>';}
else{
$default_thumb = "('".get_bloginfo( 'stylesheet_directory' )."/images/default-thumbnail.jpg')";
echo '<div class="category_page_img-background" style="background-image: url'.$default_thumb.'"></div>';
}
?>
<div class="encimaimagen"></div>
</div>
<div class="meta"><h1><?php the_title(); ?></h1></div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="row">
<div class="col-sm-8">
<section id="category_page">
<?php
$ultimas = new WP_Query();
$ultimas -> query('showposts=40');
while($ultimas -> have_posts()) : $ultimas ->the_post();
?>
<article class="col-xs-12 archivo row">
<a href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
<div class="hidden-xs col-sm-4 col-md-3"><?php the_post_thumbnail( 'thumbnail' ); ?></div>
<div class="col-sm-8 col-md-9"><?php the_excerpt(); wp_reset_postdata();?></div>
</article>
<?php endwhile; ?>
</section>
</div>
<div class="col-sm-4 hidden-xs"><?php get_sidebar(); ?></div>
<div class="index_archivo col-xs-12">
<a href="<?php $url = home_url( $path = 'index.php/archivo', $scheme = relative ); echo $url;?>">
<h2>Ver todas las entradas</h2>
</a>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
Thank you guys!

To display latest posts from a certain category, your code needs to look something like this:
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?> </li>
<?php endforeach; ?>
</ul>
Source: https://wordpress.org/support/topic/recent-posts-from-specific-category

Related

How to change the WordPress search results page (using Betheme and Ajax search lite)

I'm currently working on a website for a client and I want to change the search results page. I tried a lot of things but when I search for something on the website I get search results, but without a image or description. My client want the website to show a image and a description.
Here is my search.php code:
/**
* The search template file.
*
* #package Betheme
* #author Muffin group
* #link https://muffingroup.com
*/
get_header();
$translate['search-title'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-title','Ooops...') : __('Ooops...','betheme');
$translate['search-subtitle'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-subtitle','No results found for:') : __('No results found for:','betheme');
$translate['published'] = mfn_opts_get('translate') ? mfn_opts_get('translate-published','Published by') : __('Published by','betheme');
$translate['at'] = mfn_opts_get('translate') ? mfn_opts_get('translate-at','at') : __('at','betheme');
$translate['readmore'] = mfn_opts_get('translate') ? mfn_opts_get('translate-readmore','Read more') : __('Read more','betheme');
?>
<div id="Content">
<div class="content_wrapper clearfix">
<div class="sections_group">
<div class="section">
<div class="section_wrapper clearfix">
<?php if( have_posts() && trim( $_GET['s'] ) ): ?>
<div class="column one column_blog">
<div class="blog_wrapper isotope_wrapper">
<div class="posts_group classic">
<?php
while ( have_posts() ):
the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( array('post-item', 'clearfix', 'no-img') ); ?>>
<div class="post-desc-wrapper">
<div class="post-desc">
<?php if( mfn_opts_get( 'blog-meta' ) ): ?>
<div class="post-meta clearfix">
<div class="author-date">
<span class="author"><span><?php echo esc_html($translate['published']); ?> </span><i class="icon-user"></i> <?php the_author_meta('display_name'); ?></span>
<span class="date"><span><?php echo esc_html($translate['at']); ?> </span><i class="icon-clock"></i> <?php echo esc_html(get_the_date()); ?></span>
</div>
</div>
<?php endif; ?>
<div class="post-title">
<h2><?php the_title(); ?></h2>
</div>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="post-footer">
<div class="post-links">
<i class="icon-doc-text"></i> <?php echo esc_html($translate['readmore']); ?>
</div>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
<?php
if(function_exists( 'mfn_pagination' )):
echo mfn_pagination();
else:
?>
<div class="nav-next"><?php next_posts_link(esc_html__('← Older Entries', 'betheme')) ?></div>
<div class="nav-previous"><?php previous_posts_link(esc_html__('Newer Entries →', 'betheme')) ?></div>
<?php
endif;
?>
</div>
</div>
<?php else: ?>
<div class="column one search-not-found">
<div class="snf-pic">
<i class="themecolor <?php echo esc_attr(mfn_opts_get('error404-icon', 'icon-traffic-cone')); ?>"></i>
</div>
<div class="snf-desc">
<h2><?php echo esc_html($translate['search-title']); ?></h2>
<h4><?php echo esc_html($translate['search-subtitle']) .' '. esc_html($_GET['s']); ?></h4>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php if( is_active_sidebar( 'mfn-search' ) ): ?>
<div class="sidebar four columns">
<div class="widget-area clearfix <?php echo esc_attr(mfn_opts_get('sidebar-lines')); ?>">
<?php dynamic_sidebar( 'mfn-search' ); ?>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php get_footer();
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="post-img">
<img src="<?php echo $featured_img_url; ?>"/>
</div>
use above code in while loop to show image by using post id.

Wordpress is removing some posts from the HTML tag where The Loop is placed. Why?

I'm developing a child theme of Onepress. I need all posts displayed inside a container div that is inside the main tag. I did it in my code, but, some posts are rendered outside the main tag in the browser. It breaks the layout. Any help will be deeply apprecciated!
Here's the code in my file:
<div id="content" class="site-content">
<?php onepress_breadcrumb(); ?>
<div id="content-inside" class="container">
<div id="primary" class="content-area">
<main class="site-main" role="main">
<div class="container">
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="container">
<?php get_template_part( 'template-parts/content', 'custom' ); ?>
</div>
<?php endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
</div><!--#content-inside -->
</div><!-- #content -->
The code in custom template file:
<article class="row entry-article col-sm-12 col-md-12 w-100" id="post-<?php the_ID(); ?>" <?php post_class( array('list-article', 'clearfix') ); ?>>
<div class="col-sm-12 col-md-3 thumb-custom-container">
<a href="<?php echo esc_url( get_permalink() ); ?>">
<?php
if ( has_post_thumbnail( ) ) {
the_post_thumbnail( 'onepress-blog-medium' );
} else {
echo '<img class="thumb-custom" alt="" src="'. get_template_directory_uri() . '/assets/images/placholder2.png' .'">';
}
?>
</a>
</div>
<div class="col-sm-12 col-md-6">
<div class="row">
<div class="col-sm-12">
<header class="entry-header col-sm-12">
<?php /* the_title( sprintf( '<p>', esc_url( get_permalink() ) ), '</p>' ); */ ?>
<?php
$post_id = get_the_ID();
echo '<h2 class="entry-price">R$ ' . get_post_meta($post_id, 'real_state_price', true) . '</h2>';
$post_id = get_the_ID();
echo '<small class="entry-features">'
. get_post_meta($post_id, 'real_state_size', true) . ' m², Square '
. get_post_meta($post_id, 'real_state_square', true) . ', Number '
. get_post_meta($post_id, 'real_state_number', true)
. '</small>';
?>
</header><!-- .entry-header -->
<div class="col-sm-12 entry-excerpt">
<?php
the_excerpt();
?>
</div>
</div>
<div class="col-sm-12 entry-link">
<button class="btn entry-btn-details" href="<?php echo esc_url( get_permalink($post_id) ); ?>">+</button>
<span><a class="entry-read-more" href="<?php echo esc_url( get_permalink($post_id) ); ?>">
Read More
</a></span>
</div>
</div>
</div>
<!-- PICTURE FROM CATEGORIE IMAGES PLUGIN -->
<div class="col-sm-12 col-md-3">
<div class="col-sm-12 entry-residencial">
<a href="<?php echo esc_url( get_permalink() ); ?>">
<?php
$residencial_terms = get_the_terms($post_id, 'residencials');
foreach ($residencial_terms as $term) : ?>
<img src="<?php echo z_taxonomy_image_url($term->term_id); ?>" />
<?php endforeach; ?>
</a>
</div>
<div class="col-sm-12">
<?php
// Teste para identificar a cidade e chamar o modal de contato correspondente
$cities_terms = get_the_terms($post_id, 'city');
if (!empty($cities_terms)) :
if (is_array($cities_terms)) :
foreach ($cities_terms as $term) :
$term_name = $term->name;
$city = $term_name == 'City01' ? 'firstModal' : 'secondModal';
endforeach;
else :
$city = $term;
endif;
else :
$city = 'generalModal';
endif;
?>
<button type="button" class="btn entry-btn" data-toggle="modal" data-target="#<?php echo $city; ?>">
Contact
</button>
</div>
</div>
</div><!-- entry content -->
And this is the code rendered in the browser:
<div id="content" class="site-content">
<?php onepress_breadcrumb(); ?>
<div id="content-inside" class="container">
<div id="primary" class="content-area">
<main class="site-main search-page" role="main">
<div class="container">
SOME POSTS HERE
</div>
</main><!-- #main -->
<!-- OTHER POSTS ARE INCORRECTLY HERE -->
<div class="container">
INCORRECTLY PLACED POSTS
</div><!-- incorrectly placed posts -->
</div><!-- #primary -->
</div><!--#content-inside -->
</div><!-- #content -->
So, anyone have a clue about how to fix this bug?
Thank you, guys!
it looks like you have some invalid HTML in your code.
So far I could find this inconsistency:
<div class="col-sm-12 entry-excerpt">
<?php
the_excerpt();
?>
</div>
</div>

wordpress loop – different html for every 2nd custom post

i am searching since a while for a solution to set my custom post loop like this:
first post> img left, content right // second post> content left, img right
this is my code so far:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
</div>
</article>
<?php else : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
</div>
</article>
<?php endif; endwhile; endif; ?>
</div>
I know, this question has been asked a couple of times, and i tried already this and this and i read this but nothing works for me. What am i doing wrong?
I imagine its only outputting one post, and that post is actually the post content attached to the page. The problem is how you are initialising your additional loop within the page. You are creating a new post object - but you are not assigning it to the if / while statements:
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
should be:
<?php if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); $i++; if(($i % 2) == 0) : ?>
Notice the addition of the $loop variable where you are setting your post object and arguments.
i got this code (not altering) working already:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div><!--.end col-->
</div><!--.end row-->
</article>
<?php endwhile; wp_reset_query(); ?>
</div><!--.end container-->

Wordpress: next page on pagination always return to index.php

I tried to add a simple pagination to my custom page by using WP_Query. However, after I click on the next page link, it will always go to index.php (since I still don't have 404.php in my theme)
This my code for my custom page that include with pagination
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=2&cat=2'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="col-md-3 col-sm-4 article-list" data-sr='enter bottom, move 50px, over 0.5s'>
<a href="<?php the_permalink(); ?>"><p class="article-link-img"><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="article-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
and this is the code in my index.php
<?php get_header(); ?>
<div class="container index-section">
<section>
<div class="row">
<div class="col-md-12">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile; else: ?>
<h1 class="index-error-message"><?php _e('Sorry, this post does not exist.'); ?></h1>
<h2 class="single-article-popular-title">Popular Articles</h2>
<?php
$topBlogs = new WP_Query('orderby=rand&cat=3&posts_per_page=4');
if ($topBlogs -> have_posts() ) :
while ($topBlogs -> have_posts() ) : $topBlogs -> the_post(); ?>
<div class="col-md-3 col-sm-3 col-xs-6 single-article-popular-list">
<a href="<?php the_permalink(); ?>"><p><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="single-article-popular-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; else: ?>
<h3>No content found</h3>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</section>
</div>
<?php get_footer(); ?>
I was trying to figured it out what is the problem, but still don't have the solution. I also have tried to use WP_PageNAVi plugin, but still have the same result.
This is the full code for my custom page
<?php get_header(); ?>
<div class="article-featured">
<div class="container">
<section>
<div class="row">
<?php
$featuredBlogs = new WP_Query('cat=4&posts_per_page=1');
if ($featuredBlogs -> have_posts() ) : while ($featuredBlogs -> have_posts() ) : $featuredBlogs -> the_post(); ?>
<div class="col-md-12 article-featured-descr">
<a href="<?php the_permalink(); ?>"><p class="article-featured-descr-text" data-sr='enter bottom, move 50px, over 0.5s'>Featured article</p>
<h1 data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'><?php the_title(); ?></h1>
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/no-article.png" alt="no article" data-sr='enter bottom, move 50px, wait 0.6s, over 0.5s'></a>
</div>
<?php endwhile; else: ?>
<div class="col-md-12 article-featured-descr">
<p class="article-featured-descr-text" data-sr='enter bottom, move 50px, over 0.5s'>No featured article found</p>
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/no-article.png" alt="no article" data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'>
</div>
<?php endif; ?>
</div>
</section>
</div>
</div>
<div class="container">
<section>
<div class="row">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=2&cat=2'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="col-md-3 col-sm-4 article-list" data-sr='enter bottom, move 50px, over 0.5s'>
<a href="<?php the_permalink(); ?>"><p class="article-link-img"><?php the_post_thumbnail('small-thumbnail'); ?></p>
<p class="article-link-title"><?php the_title(); ?></p></a>
</div>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
<div class="col-md-12 article-not-found">
<img src="/ecteciant/wp-content/themes/ecteciant/files/images/article/submit-article.png" alt="submit article" data-sr='enter bottom, move 50px, over 0.5s'>
<p data-sr='enter bottom, move 50px, wait 0.3s, over 0.5s'>What do you think about <b>"A"</b>? How about <b>"B"</b>?<br/>Send your thoughts to article#ecteciant.com, we accept from <b>A to Z.</b> Our team will review your submission.<br/>Stand a chance to be <b>FEATURED</b> in our page.</p>
</div>
</div>
<!--<iframe src="https://www.facebook.com/plugins/share_button.php?href=https%3A%2F%2Fbudilokawijaya.com&layout=button_count&mobile_iframe=true&width=86&height=20&appId" width="86" height="20" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
Tweet #ecteciantID-->
</section>
</div>
<?php get_footer(); ?>
I just found an answer from another forum that solved this problem.
By modifying the admin in Reading to Blog pages show at most = 1, it solved the issue. But not sure why this solved the issue and I also don't know if there is any drawback by doing this.
At least this solved the problem.
If anyone know a better solutions, please post your solutions.

Display posts from a Wordpress custom post type category

I have a custom post type set up called TESTIMONIALS and two CPT categories set up which are CLIENT TESTIMONALS & CLINIC TESTIMONIALS
I am trying to display only the posts from the CLIENT TESTIMONALS CPT category.
What would I need to add to the below to achieve this?
<div role="tabpanel" class="tab-pane fade" id="profile">
<?php query_posts('post_type=testimonials'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>
You can use something like this.
<?php
$type = 'testimonials';
$args=array(
'post_type' => $type,
'category'=>'CPT',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
}
?>

Categories