How to display multiple authors on a WordPress post - php

I have a WordPress blog and some of the articles are written by multiple authors, therefore I would like to display them on the post. Part of the goal has been achieved by installing the Co-Authors Plus plugin. This plugin only allows you to assign more than one author to the post but it won't automatically show both authors on the published post. For that I had to tweak the post-author.php file of my theme Mission News and I helped myself with this guide. I successfully edited the file (as seen in the code below), and now both names, Rachele and Collaboratore are displayed at the end of this post.
The original post-author.php file
<?php if ( get_theme_mod( 'author_box_posts' ) == 'no' ) return; ?>
<div class="post-author">
<?php if ( get_theme_mod( 'author_avatar_posts' ) != 'no' ) : ?>
<div class="avatar-container">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 78, '', get_the_author() ); ?>
</div>
<?php endif; ?>
<div>
<div class="author"><?php the_author(); ?></div>
<p><?php the_author_meta('description'); ?></p>
</div>
</div>
The edited post-author.php file
<?php if ( get_theme_mod( 'author_box_posts' ) == 'no' ) return; ?>
<div class="post-author">
<?php if ( get_theme_mod( 'author_avatar_posts' ) != 'no' ) :
if ( function_exists( 'coauthors_posts_links' ) ) {
coauthors_posts_links();
} else {
the_author_posts_link();
}?>
<div class="avatar-container">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 78, '', get_the_author() ); ?>
</div>
<?php endif; ?>
<div>
<p><?php the_author_meta('description'); ?></p>
</div>
</div>
I suppose the real problem here is my almost non-existent knowledge of php because I failed to correctly edit the content.php file which I believe is the file I need to edit in order to add both author names also in the text right under the title and next to the published date.
The content.php file contains the following lines:
<?php
$author = get_theme_mod( 'post_author_posts' );
$date = get_theme_mod( 'post_date_posts' );
?>
<div <?php post_class(); ?>>
<?php do_action( 'ct_mission_news_post_before' ); ?>
<article>
<?php ct_mission_news_featured_image(); ?>
<div class='post-header'>
<h1 class='post-title'><?php the_title(); ?></h1>
<?php ct_mission_news_post_byline( $author, $date ); ?>
</div>
<div class="post-content">
<?php ct_mission_news_output_last_updated_date(); ?>
<?php the_content(); ?>
<?php wp_link_pages( array(
'before' => '<p class="singular-pagination">' . esc_html__( 'Pages:', 'mission-news' ),
'after' => '</p>',
) ); ?>
<?php do_action( 'ct_mission_news_post_after' ); ?>
</div>
<div class="post-meta">
<?php get_template_part( 'content/post-categories' ); ?>
<?php get_template_part( 'content/post-tags' ); ?>
<?php get_sidebar( 'after-post' ); ?>
<?php get_template_part( 'content/post-author' ); ?>
</div>
<?php get_template_part( 'content/more-from-category' ); ?>
</article>
<?php comments_template(); ?>
</div>
I think I should be editing the <?php ct_mission_news_post_byline( $author, $date ); ?> line, but I don't know how. I tried a few things but none worked.
I would like to be able to display both names of the authors under the title of the post and next to the published date.
Thank you in advance to all those willing to help me.
Have a good day!

This:
(function_exists ('coauthors_posts_links')) {coauthors_posts_links ();}
The other:
{the_author_posts_link (); }

Related

Post Archive page - If else statement advanced custom fields

I am using Advanced Custom Fields and Facet WP.
Currently there is a directory listing showing on a page that shows a business preview by displaying a business title, image and an excerpt - the same as you typically see from a post archive page.
I have added a new ACF checkbox field 'Are you ready to publish' and would like to amend the php so the Title, excerpt an image will only show for user profile listings that have checked 'yes' to publish.
I am fairly new to php and ACF and cannot get it to work.
I have tried to variations:
<?php
global $post;
?>
<div class="business-directory-results">
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<?php while ( have_posts() ): the_post(); ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php endwhile; ?>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
</div>
and
<?php
global $post;
?>
<div class="business-directory-results">
<?php while ( have_posts() ): the_post(); ?>
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
<?php endwhile; ?>
</div>
If you create posts first and then add an additional ACF field, they will not work. To make it work, you will need to update all your posts after adding an ACF field. In your case, you will need update each user profile and only then they will have the ACF field attached with them.
Don't place the condition outside of the loop. That means your second variation is correct.
<?php
global $post;
?>
<div class="business-directory-results">
<?php while ( have_posts() ): the_post(); ?>
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
<?php endwhile; ?>
</div>

Wordpress Post loop with query $args on search.php results?

I'm creating a search results page and I'm trying to exclude specific post formats. I found this query but I don't know how make the post loop works, I don't even know the code. And, I don't know how to put the 404 error not found on the search.php.
This is the code
<?php
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-quote', 'post-format-video',
'operator' => 'NOT IN'
)
)
);
query_posts( $args );
?>
I first tried something and worked fine, but if i set "4 post per-page" it counts all the formats post, and if the standard posts on the search results should have been 3 and 1 quote-format post, it shows 3 standard post and an empty space.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_post_format() == 'quote' ) : ?>
<?php elseif( get_post_format() == 'link' ) : ?>
<?php elseif( get_post_format() == 'video' ) : ?>
<?php elseif( get_post_format() == 'image' ) : ?>
<?php else : ?>
post
<?php endif; ?>
<?php endwhile; else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
This is the whole code of my search.php. This was the only way to not display other formats post on my search results. I just want the loop to not count "other formats post" on my results. I want to show 4 results per-page and has to be 4 standard posts format.
<?php
get_header(); ?>
<div id="content" class="site-content">
<div id="news-page">
<div class="right">
<?php get_sidebar(); ?>
</div>
<div class="left">
<div class="newstitle">RISULTATI</div>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_post_format() == 'quote' ) : ?>
<?php elseif( get_post_format() == 'link' ) : ?>
<?php elseif( get_post_format() == 'video' ) : ?>
<?php elseif( get_post_format() == 'image' ) : ?>
<?php else : ?>
<a href="<?php the_permalink(); ?>" title="Leggi">
<div class="post">
<div class="data">
<span class="giorno"><?php the_time('j M Y') ?></span> <span class="commenti"><?php comments_number('0 commenti', '1 commento', '% commenti'); ?></span>
</div>
<div class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'news-page' ); } ?></div>
<div class="thumb-full"><?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'news-page-full' ); } ?></div>
<div class="title"><?php echo get_the_title( $post_id ); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<div class="diss"></div>
<div style="position: absolute; z-index:999; bottom: 0; left: 0; font: normal 10px arial; padding: 3px;"> In <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></div>
</div>
</a>
<?php endif; ?>
<?php endwhile; else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
</div>
</div>
</div><!-- #primary -->
<?php get_footer(); ?>

Infinite scrolling of WP categories doesn't load all elements

I set up a site for a photographer & she wanted to use her blog categories as her portfolio, which works fine, except for the fact that the infinite scrolling feature I'm using won't load all of the content for the excerpts.
The two items that it omits are the post thumbnail (image) and the horizontal row serving as a separator between the posts.
Any insight on this is appreciated - I'm using Paul Irish's infinite scroll plugin & the category excerpts are being called like so:
<?php get_header(); ?>
<section id="content" role="main">
<header class="header">
<h1 class="entry-title"><!-- <?php _e( 'Category Archives: ', 'themename' ); ?> --> <?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php get_template_part( 'entry' ); ?>
<hr />
<?php endwhile; endif; ?>
<?php get_template_part( 'nav', 'below' ); ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is the entry template:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<?php if ( is_singular() ) { echo '<h1 class="entry-title">'; } else { echo '<h2 class="entry-title">'; } ?><?php the_title(); ?><?php if ( is_singular() ) { echo '</h1>'; } else { echo '</h2>'; } ?><?php edit_post_link(); ?>
<?php if ( !is_search() ) get_template_part( 'entry', 'meta' ); ?>
</header>
<?php get_template_part( 'entry', ( is_archive() || is_search() ? 'summary' : 'content' ) ); ?>
<?php if ( !is_search() ) get_template_part( 'entry-footer' ); ?>
</article>
For reference, this is how I'm calling the excerpts for the blog, which have no issue:
<section class="entry-content">
<div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div>
<?php the_excerpt(); ?>
<em><p>
<?php comments_number( '0 comments', '1 comment', '% comments' ); ?>.
</p></em>
<div class="entry-links"><?php wp_link_pages(); ?></div>
</section>
<hr />
Link to example
Move <div class="cat-thumbs align-right"><?php the_post_thumbnail(); ?></div> and <hr> into the entry template.

Next button not showing other posts in Wordpress

In my wordpress I have many posts, every page I am showing 5 posts.
Bottom of my page I have next and prev button. When I click one the next button it will go to /page/2/ link but this page title is showing Page not found. And it's not showing other posts in page 2.
My next and prev code :
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
My index.php code :
<div class="center-content">
<ul>
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else {
?>
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
You do have a few issues here
NEVER EVER use query_posts. It is slow, reruns queries, breaks and fails silently with pagination and worse of all, it breaks the main query object. If you break the main query object, you break page functions. So, please never use query_posts. Mke if it never existed
You have replaced the main query loop with a custom query, which you must not do. If you need to show a different amount of posts on a particular page ( not on page templates and a static front page though as this will not work there ), then use pre_get_posts. You need to go and read how helpful that action is and how to use it
Remove this line
<?php query_posts('posts_per_page=5'); ?>
Then add the following in your functions file
add_action( 'pre_get_posts', function ( $query )
{
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
});
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
Just add below code: I added 'paged' variable and set in next_posts_link() and previous_posts_link()..please see below code:
<div class="center-content">
<ul>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
global $wp_query;
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else { echo 'test';
?>
<div class="prev-next-btn">
<?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<div class="prev-next-btn">
<?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>

Link Wordpress post images to post

I'm developing a wordpress theme for my blog and I'm having some difficulty.
When I make a new post, the first image of that post is displayed on the homepage. When the image is clicked, it links to the image file and shows the full size. I would like for it to automatically link to the post from which it came. I understand that you can choose what the image links to upon upload or in the editor. I would like for the images to link to their original post automatically, since I have other people writing on my blog, and I don't want them to have to do this each time.
As I understand it the content.php file controls the post format in this case. Here is the file from my theme. Or is it possible to use a function?
<?php
/**
* The default template for displaying content single/search/archive
*
* #package test
* #since test 0.1.0
*/
?>
<!-- START: content.php -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( is_single() ) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<?php the_post_thumbnail(); ?>
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<?php endif; // is_single() ?>
</header><!-- .entry-header -->
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php posted_on(); ?>
<?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
<span class="label radius secondary"><?php _ex( 'Featured', 'Post format title', 'test' ); ?></span>
<?php endif; ?>
</div><!-- .entry-meta -->
<?php endif; ?>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'test' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'test' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-meta">
<?php if ( 'post' == get_post_type() ) : ?>
<?php get_template_part('entry-meta', get_post_format() ); ?>
<?php endif; ?>
</footer><!-- #entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->
<!-- END: content.php -->
https://codex.wordpress.org/Function_Reference/the_post_thumbnail
example 1. To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme's template files:
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>

Categories