how to add a class at particular child in wordpress loop - wordpress

This is my code and i want to add a class in second child in loop. please tell me how can i do it. i am new in wordpress.
<?php
$args = array(
'posts_per_page' => 3,
'post_type' => 'hosting_plan',
'order' => 'ASC'
);
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<div class="hostplbx /*here i want to add class on second child*/">
<h3><?php the_field('plan_name'); ?></h3>
<div class="hostprice">
<span class="hosprice"><b class="rs"><?php the_field('plan_price'); ?></b> per month</span>
<span class="plandetail"><?php the_field('tag_line'); ?></span>
</div>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>

You could use a simple iterator variable -- some sample code you can alter to apply to your template:
$counter = 1;
if ( have_posts() ) : while ( have_posts() ) : the_post();
if($counter == 2) echo "<div class='second_child_class'>content goes here</div>";
else echo "<div>content goes here</div>";
$counter++;
endwhile; endif;

Related

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

wordpress blog homepage customization

please don't give a negative feed like always and instead tell me the mistake.
I have a blog.
can i make the homepage display a summary of an article instead of the whole article?
if yes then where or how can i do it?
I have this pre-set in one of my WordPress sites. Here is what this looks like and it is in Appearance/Editor/Posts Page (home.php)
<div class="post-content">
<?php $content = get_the_content(); ?>
<?php echo wp_trim_words(strip_tags($content), 30); ?>
</div>
<a class="blog-post-read-more" href="<?php echo esc_url( get_the_permalink( get_the_ID() ) ); ?>"><?php echo esc_html( get_theme_mod( 'relia_blog_read_more', __( 'Read More', 'relia' ) ) ); ?></a>
</div>
So what this does is it will strip the words up to 30, wp_trim_words. And below that is the how to insert Read More.
Here are some links for you to check out:
https://codex.wordpress.org/Excerpt
https://codex.wordpress.org/Customizing_the_Read_More
Create one custom template that template you can assign your home page
/*
Template Name: Blog
*/
query_posts( array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) ) );
<?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 the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
wp_reset_postdata();

How to fix pagination not found in wordpress?

I am a new of wordpress. I have a problem with pagination when I click to the next post it shows "not found". I installed the plugin wp pagenavi, and I put code in my blog post . It shows the pagination, but I have a problem with the link to the next post. Example when I click to the next post it is show
Something went Wrong!
404
-->
You can see at: http://westecmedia.com/events/
And this is my code in event-page.php:
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; else: endif; ?>
<?php wp_pagenavi(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please ?
Maybe you missed the object here. You can try the code below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'category_name'=>get_the_title(),
'post_status'=> array('publish', 'future')
);
query_posts($args);
instead:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>

two paginations in a single page

I use the normal pagination method in wordpress
<?php $i = 1 ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => "2", 'paged' => $paged, 'cat' => 26 );
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="art_1">
<h5><?php echo $i ?>. <?php the_title(); ?></h5>
<?php $i++; ?>
</div>
<?php endwhile; ?>
<div class="er">
<?php next_posts_link(); ?>
</div>
<div class="err">
<?php previous_posts_link(); ?>
It works fine but the problem is this pagination I use for listing out articles in a particular category ,now I have another category of articles in same page which needs pagination too.pagination of 1st should no affect the other and vice versa. Can anyone suggest me what should I do?
Use this hook after pagination..
<?php wp_reset_query(); ?>

Thesis Framework and Custom Post Types

First time creating/implementing custom post types in Thesis, not first time using custom post types.
I used the Reed Write plugin to create the custom post types. The site is using Thesis 1.8.5.
On the following page (http://www.snyderleadership.com/press-releases/) I have the main content getting dropped in with the contents of the custom post type after it.
I used the custom_functions.php file to create a custom page template and call the db for the contents of the custom post type. Here is my code:
/* CUSTOM PRESS RELEASE TEMPLATE - ADDED by BRETT ATKIN */
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
<div id="press-image">
<?php echo wp_get_attachment_image(get_post_meta($post->ID, 'release_image', true)); ?>
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo get_post_meta($post->ID, 'release_date', true); ?></p>
<p class="press-link"><?php echo get_post_meta($post->ID, 'release_title', true); ?></p>
<p class="press-author"><?php echo get_post_meta($post->ID, 'release_author', true); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
add_action('thesis_hook_custom_template', 'press_releases_page');
It seems like everything is working correctly, just not pulling in the data for the custom post type.
Having done this on other sites (using custom themes), I'm not sure if I did something wrong here or if it is a Thesis issue.
Any help would be great.
Thanks
Brett
Here is the final working code. Thanks to the help from of a local WP Guru friend and maiorano84. We didn't figure out the cause, but we did fine a solution.
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $results = get_post_custom(); ?>
<div id="press-wrapper">
<div id="press-image"><?php echo wp_get_attachment_image($results['release_image'][0] ); ?></div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo $results['release_date'][0] ?></p>
<p class="press-link"><?php echo $results['release_title'][0] ?></p>
<p class="press-author"><?php echo $results['release_author'][0] ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_query(); wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'press_releases_page');
Try changing this:
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
To this:
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="press-wrapper">
UPDATE:
The only other thing I can see is that you're not resetting your post data, and that the $wp_query variable is actually a Wordpress Global. Try using the reset functions and changing your WP_Query instance name like this:
<?php
wp_reset_query();
wp_reset_postdata();
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
UPDATE 2:
Now that you have your wrappers being written to your document, you need to normalize your information. Try to avoid using Metadata instead of normal post attributes. I imagine your metadata 'release_image' is a link to an image somewhere on your server:
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div id="press-wrapper">
<div id="press-image">
<img src="<?php echo get_post_meta(get_the_ID(), 'release_image', true); ?>" />
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo the_date(); ?></p>
<p class="press-link"><?php the_title(); ?></p>
<p class="press-author"><?php the_author(); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>

Resources