get custompost with category - php

I have issue with getting all posts with a category("case"), if I remove the 'cat' => $cat_id line i get all posts in posttype "Feed"
How do I get out only posts with category 'case'?
$cat_id = get_cat_ID('case');
var_dump($cat_id); //responce is 40
$args = array(
'post_type' => 'Feed',
'posts_per_page' => -1,
'cat' => $cat_id,
);
while ( $loop->have_posts() ) : $loop->the_post();
var_dump($loop);
endwhile;
wp_reset_query();

<ul>
<?php
global $post;
$args = array( 'category' => 'your category name id''offset'=> 1, );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
for more information check https://codex.wordpress.org/Template_Tags/get_posts

Please try this code :
change your category id or post id
<?php if (is_category('3') ) { ?>
<?php query_posts('p=17'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} elseif (is_category('13') ) { ?>
<?php query_posts('p=11'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} else { ?>
//whatever goes in here
<?php } ?>

Related

Pagination for Custom Loop (loop with foreach)

I'm trying to add pagination to a custom loop, but I can't figure out how to do it. When I manage to add "previews" and "next" buttons it always shows the same 10 posts. I found some solutions for a while loop but not for a foreach loop (which I actually never used before).
This is the loop (is get_posts a problem?) :
<?php
$news = get_posts(array('posts_per_page' => 10));
$news['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>
<?php foreach ($news as $article): ?>
<div class="col-md-4">
<h3><?php echo $article->post_title ?></h3>
<hr>
<p class="desc"><?php echo $article->post_excerpt ?></p>
<?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?>
<p class="btn_text"> Ler mais</p>
</div>
<?php endforeach; ?>
<?php previous_posts_link( '<<' );
next_posts_link( '>>', $custom_query->max_num_pages ); ?>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source : http://web-profile.net/wordpress/themes/wordpress-custom-loop/

Can someone help me to add a category to this Post?

I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name

Can't reset query to show ramdom post after firs loop Wordpress

I have a piece of code to show a random post under a post. But this code only show me the post's write from the author of that post. Whats wrong?
<!-- post -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php the_date(); ?>
<?php the_content();?>
<?php $key="video"; echo get_post_meta($post->ID, $key, true); ?>
<?php $key="imagen"; echo get_post_meta($post->ID, $key, true); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<!-- ramdom post -->
<?php $posts = $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<?php the_post_thumbnail('photo-thumbnail') ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php } ?>
Try adding <?php wp_reset_query(); ?> after the first query.
Replace the random post code with the code below. In your example you were querying 3 posts. If that's not your intention and you only want a single post replace 3 with 1 in posts_per_page.
$secondary_posts = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 3, 'no_found_rows' => 1, ) );
if ( $secondary_posts->have_posts() ) : while ( secondary_posts->have_posts() ) : $secondary_posts->the_post();
the_post_thumbnail( 'photo-thumbnail' );
the_author();
the_category();
endwhile; endif;
wp_reset_postdata();

How to display custom post wordpress while showing default posts?

I am trying to display new custom post and the default post as well.
How to display both posts(default,my_custom_post)
<div id="primary" class="site-content">
<div id="content" role="main">
<?php wp_list_pages('title_li=');
wp_nav_menu();?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' );
?><?php else : ?>
<?php wp_reset_query();
query_posts('post_type=my_custom_post'); // my custom post
?>
but only single post is displaying yet.
This is how you display custom posts in wordpress.
<?php
$type = 'my_custom_post';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>

wordpress related posts by category duplicating

I am having an issue getting the $do_not_duplicate working properly I have several title duplicating on my blog and i need it to stop. Here is what i have so far:
<?php if (is_single()): ?>
<section>
<h3>Related Posts</h3>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args );
if (count($related_posts)): ?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?><?php
endwhile;
wp_reset_query(); ?>
</a></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
</section>
<?php endif; ?>
you have a while (have_posts() ) within a foreach and this produces the duplication. You may change your loop to something like this:
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args2 = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args2 );
if (count($related_posts)):
?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>" ><?php $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_query(); ?>
<?php endif;endif; ?>
At the end of the loop, the var $do_not_duplicate save the id of the post and all the id of the relative posts.

Categories