How do I structure this nested php loop for Wordpress? - php

I'm attempting to build a category.php page for a custom Wordpress theme. Ideally, the page would find all category posts, then grab the sticky posts from that category, and if there's at least two, it would display them at the top (it's fine if these same posts repeat below).
This is the page I have built so far. It can gather all of the results without error, but the nested loop which tries to find the most recent two stickies fails. I receive the error:
"Warning: Attempt to assign property of non-object in C:\dev\xampp\htdocs\wordpress\wp-content\themes\ampersand\category.php on line 22"
How do I fix this loop? (I've included a github gist in case that's easier to read, category page for Wordpress theme in development)
<?php // Category Page Content Start ?>
<div id="content">
<main id="main" class="category-page" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Periodical">
<h1><?php single_cat_title(); ?></h1>
<?php // Find all posts for the queried category ?>
<?php $catID = get_queried_object_id();
$args = array( 'numberposts' => -1, 'posts_per_page' => 20,
'cat' => $catID, 'post-type' => 'post' );
$query = new WP_query( $args ); $count = 0;
if ($query -> have_posts()) : ?>
<section class="featured">
<?php // Find the most recent sticky posts for this category; if at least 2, post them seperately ?>
<?php $sticky = get_option( 'sticky_posts' );
$findsticky = array( 'post__in' => $sticky, 'numberposts' => 2 );
$stickyposts = get_posts( $findsticky ); if ($stickyposts -> count = 2) :
foreach ($stickyposts as $post) : $post -> the_post(); ?>
<article class="featured-article" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endforeach; endif; ?>
</section>
<section class="all-results">
<?php while ($query -> have_posts()) : $query -> the_post(); $count++; ?>
<article class="<?php echo 'article-'.$count; ?>" itemtype="http://schema.org/Article">
<meta itemprop="wordCount" content="<?php echo word_count(); ?>">
<figure class="thumbnail">
<div class="image-wrapper">
<a href="<?php the_permalink() ?>">
<img src="<?php the_post_thumbnail_url( 'thumb-small' ); ?>">
</a>
</div>
</figure>
<section>
<h2 class="headline">
<?php the_title(); ?>
</h2>
<span class="byline">
<?php $check = get_field( 'contributor' );
if ($check):?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person">
<?php the_field( 'contributor' ); ?>
</span>
<?php else: ?>
<span class="contributor" itemprop="author" itemtype="http://schema.org/Person"><?php the_author_posts_link(); ?></span>
<?php endif; ?>
</span>
<?php $check = get_field( 'description' );
if ($check): ?>
<span class="description"><?php the_field( 'description' ); ?></span>
<?php else: ?>
<span class="excerpt"><?php the_excerpt(); ?></span>
<?php endif; ?>
</section>
</article>
<?php endwhile; ?>
</section>
<div class="pagination-bottom">
<?php if( get_next_posts_link() ) :
previous_posts_link( 'Older Posts' );
endif; ?>
<?php if( get_next_posts_link() ) :
next_posts_link( 'Newer Posts', 0 );
endif; ?>
</div>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; wp_reset_postdata(); ?>
</main>
</div>

Related

How to output text from single custom post type on archive page

I have a custom post type. There are single and archive pages. The archive page displays cards with the team. Each card has a city. How can I, when creating a single team member, specify his city and display it on the archive page. This information is not on the single page, only archived.
<?php
$args = array(
'post_type' => 'speakers',
);
$loop = new WP_Query( $args );
while ($loop->have_posts()) :
$loop->the_post(); ?>
<li>
<?php if ( is_object_in_term( $loop->post->ID, 'countries', 'germany' ) ) {
echo '<div class="speaker-flag"><img src="/wp-content/uploads/2022/08/flag-for-germany.svg"></div>';
} else if ( is_object_in_term( $loop->post->ID, 'countries', 'switzerland' ) ) {
echo '<div class="speaker-flag"><img src="/wp-content/uploads/2022/08/flag-for-switzerland.svg"></div>';
} ?>
<a href="<?php echo esc_attr(the_permalink())?>">
<div class="speaker-img">
<?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?>
<img src="<?php echo $img_url ?>">
</div>
<div class="speaker-name">
<p>
<?php the_title(); ?>
</p>
</div>
<div class="speaker-city">
<p>
Fribourg
</p>
</div>
</a>
</li>
<?php endwhile;
wp_reset_query(); ?>
//single
<div class="speaker_info">
<div class="speaker_text">
<h4>
<?php the_title(); ?>
</h4>
<?php the_content(); ?>
</div>
<div class="speaker_img">
<img src="<?php the_field('speaker_img'); ?>">
</div>
</div>

How to display each category most recent post and change the order of categories whenever there is a new post in each category?

I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();

Wrapping a div around the results of a word press loop but excluding the first 2

I have a loop for a custom post type. I'm bringing back a block of title, image and content for each post. I want to apply slick slider to the results to create a slick carousel, but I don;t want to include the first two results of the loop - so I'd need to create a parent div to the results but only start that div after the first two results.
I've trialed ways of querying the results on a loop count to apply a class to only the first two results, but this doesn't really achieve what I'm after.
<div class="wrapper_for_news_items">
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'news',
'order' => 'DESC'
));
if( $posts ): ?>
<?php $post = $posts[0]; $c=0; ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div class="treatment_block news_block <?php $c++; if($c == 1) { echo ' featured'; } elseif($c == 2) { echo ' featured'; } ?>">
<h2 class="block_title above"> <?php the_title( '' ); ?></h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below"> <?php the_title( '' ); ?></h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div> <!-- treatment news block wrapper -->
You could just create 2 loops.
Use the first for the featured output and the second for the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_two_posts );
if( $query_with_two_posts->have_posts ) :
while ( $query_with_two_posts->have_posts ) : $query_with_two_posts->the_posts; ?>
<div class="treatment_block news_block featured">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of 2 post initial news loop -->
</div>
<!-- treatment news block wrapper -->
<?php
// Start your second loop containing the slickslider content
?>
<div class="wrapper_for_news_carousel_items">
<?php
$args_with_all_posts = array(
'posts_per_page' => -1,
'offset' => 2 // Offset the 2 initial posts
'post_type' => 'news',
'order' => 'DESC'
);
$query_with_two_posts = new WP_Query( $args_with_all_posts );
if( $args_with_all_posts->have_posts ) :
while ( $args_with_all_posts->have_posts ) : $args_with_all_posts->the_posts; ?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news carousel items -->
Or you could count the posts in the loop and asign a wrapper before the third post and after the last post to create the carousel.
<div class="wrapper_for_news_items">
<?php
$args_with_two_posts = array(
'posts_per_page' => 2,
'post_type' => 'news',
'order' => 'DESC'
);
$query = new WP_Query( $args_with_two_posts );
$counter = 1; // Set the counter
if( $query->have_posts ) :
while ( $query->have_posts ) : $query->the_posts;
if ( $count == 3 ) { echo '<div class="slick-slider">'; };
?>
<div class="treatment_block news_block">
<h2 class="block_title above">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date top">
<?php echo get_the_date() ?>
</h3>
<div class="post_icon" style="background-image: url('<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail_url($post_id, 'thumbnail');
}
?>');">
<button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>
</div>
<h2 class="block_title below">
<?php the_title( '' ); ?>
</h2>
<h3 class="post_date bottom">
<?php echo get_the_date() ?>
</h3>
<p class="excerpt">
<?php the_excerpt( '' ); ?>
</p>
</div>
<?php
$counter++; // Add +1 every loop
if (($query->current_post +1) == ($query->post_count)) {
echo '</div>'; // This is the last post
}
endwhile;
?>
<?php wp_reset_postdata(); ?>
<?php else : ?> No News Found!
<?php endif; ?>
<!-- end of news loop -->
</div>
<!-- treatment news block wrapper -->

Getting Taxonomy Error

I'm trying to get taxonomy from post and link. But, link has to be from other custom post "directors" and taxonomy is has to be from other custom post type... I can get taxonomy normally using another code but, i cant get links...
When i trying to use this code i get nothing on page...
What is the problem of this code?
<h6 class="box-title"><?php _e( 'Directed by:', 'listingpress' ); ?></h6>
<div class="director-box-inner">
<?php
$taxonomy ='mydirectors';
$term_lists = get_the_terms($post->ID, $taxonomy);
foreach ($term_lists as $termlist):
$name_term= $termlist->slug;
$argsdirectors = array(
'post_type' => 'directors',
'name' => $name_term
);
$loop = new WP_Query($argsdirectors);
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post();
?>
<div class="agent-card">
<a href="<?php the_permalink() ?>" class="agent-avatar-container">
<?php if(has_post_thumbnail()){
the_post_thumbnail();
}else{?>
<img src="<?php echo get_stylesheet_directory_uri().'/images/profil.png';?>"/>
<?php }
?>
</a>
<div class="agent-card-info">
<div class="agent-card-info-inner">
<h6 class="agent-card-name">
<?php the_title(); ?>
</h6>
</div>
</div>
</div>
<?php _e( 'View Profile', 'listingpress' ); ?>
<div class="clearfix"></div>
<?php
endwhile;
endif;
endforeach;
wp_reset_query();
?>
</div>

the_content(); not showing in images.php

I am having problem getting the_content(); on image.php or attachment.php, the_content(); it's showing in single.php but no in imaage.php, I tried to edit the code but I got nothing,
<div class="content section-inner">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="posts">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="content-inner">
<div class="featured-media">
<?php $imageArray = wp_get_attachment_image_src($post->ID, 'full', false); ?>
<a href="<?php echo esc_url( $imageArray[0] ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment">
<?php echo wp_get_attachment_image( $post->ID, 'post-image' ); ?></a>
</div> <!-- /featured-media -->
<div class="post-header">
<h2 class="post-title"><?php echo basename(get_attached_file( $post->ID )); ?></h2>
<div class="post-meta">
<span><?php _e('Uploaded', 'lingonberry'); echo ' '; the_time(get_option('date_format')); ?></span>
<span class="date-sep">/</span>
<span><?php _e('Width:', 'lingonberry'); echo ' ' . $imageArray[1] . ' px'; // 1 is the width ?></span>
<span class="date-sep">/</span>
<span><?php _e('Height:', 'lingonberry'); echo ' ' . $imageArray[2] . ' px'; // 2 is the height ?></span>
</div>
</div> <!-- /post-header -->
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<div class="post-content">
<?php the_excerpt(); ?>
</div> <!-- /post-content -->
<?php endif; ?>
</div> <!-- /content-inner -->
<div class="post-nav">
<?php
/**
* Grab the IDs of all the image attachments in a gallery so we can get the URL of the next adjacent image in a gallery,
* or the first image (if we're looking at the last image in a gallery), or, in a gallery of one, just the link to that image file
*/
$attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
foreach ( $attachments as $k => $attachment ) :
if ( $attachment->ID == $post->ID )
break;
endforeach;
$l = $k - 1;
$k++;
if ( isset( $attachments[ $k ] ) ) :
// get the URL of the next image attachment
$next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
$prev_attachment_url = get_attachment_link( $attachments[ $l ]->ID );
else :
// or get the URL of the first image attachment
$next_attachment_url = get_attachment_link( $attachments[ 0 ]->ID );
endif;
?>
<?php _e('« Previous<span> attachment</span>', 'lingonberry'); ?>
<?php _e('Next<span> attachment</span> »', 'lingonberry'); ?>
<div class="clear"></div>
</div> <!-- /post-nav -->
<?php comments_template( '', true ); ?>
<?php endwhile; else: ?>
<p><?php _e("We couldn't find any posts that matched your query. Please try again.", "lingonberry"); ?></p>
<?php endif; ?>
</div> <!-- /post -->
</div> <!-- /posts -->
I need help on this
Your code is bit of a mystery but if it looks like this
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<div class="post-content">
<?php the_excerpt(); ?>
<?php the_content(); ?>
</div> <!-- /post-content -->
<?php endif; ?>
the commands the_excerpt() and the_content() will only execute when there is an excerpt present. The if-statement says "if excerpt is not empty then execute the following code.
Try this:
<div class="post-content">
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php the_content(); ?>
</div> <!-- /post-content -->
There is a shorter way of writing this but this way it shows the change better.

Categories