I am using the wp_get_archives function to display a list of posts on my sidebar. Currently this does work as intended.
I want to build upon this sidebar by displaying get_the_title() and get_the_excerpt(). these also work.
However they are only displaying the current title and excerpt and not the corresponding title and excerpt to the current post.
Example:
Post 1 displays post 1's title and excerpt.
Post 2 displays post 1's title and excerpt.
Here is my full sidebar:
<aside id= "homeSideBar">
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post(); ?>
<?php }}
wp_get_archives( array( 'type' => 'postbypost', 'limit' => 10, 'after' => "<img class='showExcerpt' src=\"" . get_template_directory_uri() ."/images/plus-circle.png\"><div class='postExcerpt'><h1 class='fitHeadliner'>" . get_the_title() . "</h1><p>" . get_the_excerpt() . "</p><div class='hideExcerpt'>X</div></div>" ) ); ?>
...
</aside>
How can I make my posts display their corresponding titles and excerpts?
So as my sidebar gets more involved, I thought it best to refactor and create a sidebar.php template.
Within my sidebar.php template I use get_posts() within get_posts() you can use setup_postdata( $post ) to globalize post information like the_excerpt(), the_title(), etc.
Related
I am ALMOST there with this, but need help with the final piece. I am trying to show only a link to an author's post after they log in. It works, but the snippet below displays the post title as plain text, then generates an empty <a> tag after the post title instead of wrapping the post title with the <a>. The link in the <a> is correct, just not wrapping the post title. What am I missing?
<?php
$user_id = get_current_user_id();
$args=array(
'post_type' => 'team_fundraiser',
'post_status' => 'published',
'posts_per_page' => 1,
'author' => $user_id
);
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$team_link .= ''.the_title().'';
endwhile;
echo $team_link;
?>
The output looks like this....
<div>
"My Awesome Post"
</div>
How do I get the <a> to wrap the post title?
You need to replace the_title with get_the_title. the_title echoes out the title by default, hence why the title is appearing before the link. get_the_title will return you the title as a value.
So change the line inside the while loop to this
$team_link .= ''. get_the_title() . '';
I'd also recommend wrapping the get_the_title function in the esc_html function (WP Code Reference) and the hyperlink in the esc_url function (WP Code Reference) to ensure you don't get any HTML/JS outputted along with the title and link.
Would end up like this:
$team_link .= ''. esc_html( get_the_title() ) . '';
I have a theme I'm building and I'm trying to work on the homepage from home.php. I already have my listing of latest articles generating, but I want the page navigation to show up at the bottom for the next and/or previous set of articles. Ideally, some sort of lazy load too, but at the moment, I just want to understand how to do the page navigation part.
I'm having trouble finding articles on how to do this without "add THIS plugin" or "use THIS theme".
Here's what I already have to generate my articles section:
<section id="articles" class="container">
<?php
$grid_posts = get_posts( array(
'posts_per_page' => 12
) );
if ( $grid_posts ) {
foreach ( $grid_posts as $post ) :
setup_postdata( $post ); ?>
<?php
if ( has_post_thumbnail() ) : get_template_part('template-parts/posts-grid', 'image');
else : get_template_part('template-parts/posts-grid', 'default');
endif;
?>
<?php
endforeach;
wp_reset_postdata();
}
?>
</section>
I just want to find a resource that shows me how to either "load more articles" on that page or show the "next/previous page" navigation.
Thanks
You're looking for wp_link_pages
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'test' ),
'after' => '</div>',
) );
For those kind of questions and to get a basic understanding, it's also always worth at least looking into a "template theme" like _s
In your case, the file to look at would be this one
I have such a problem. On single.php page template, I display posts by categories. At the sidebar, I have the widget with related services and a bunch of tags that where I can filter displayed tags(using GET requests)
It looks like this:
<aside class='main__aside'>
<?php the_widget('some_wdgt',
array('title' => esc_html__('title', 'domain'),
'nav_menu' => 772));
?>
<?php
... some code with get params ...
?>
</aside>
Everything works fine, but when I apply filter widget with menu filter disappears. The only title of the widget is displayed.
Insdide widget code I display meny via:
...
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
echo $after_widget;
Why this is happening and how can I fix it? Thanks for your help.
May be it happens because of $nav_menu is WP_Term object, not number.
Please, look at this: widget_nav_menu_args. Hope it helps
I want to display only one author's post say "abc_author" .
i have use the validation using if clause but THE PROBLEM is that i get pagination below the post where page 1 is blank (as an other author post is skipped) , page 2 (as an other author post is skipped) is blank and soon i get my desired post on page 6 . commenting the verbosa_pagination() hide the pagination navigation but the starting pages are blank and
I am a new to WordPress , i have also tries different plugin but they facilitate me to filter it on pages not post (though i can change the home page to a particular page but i want it on default blog post for few reasons )
<?php if ( have_posts() ) : ?>
<div id="content-masonry">
<?php /* Start the Loop */
while ( have_posts() ) : the_post();
if(get_the_author()=='abc_author')
{
get_template_part( 'content/content', get_post_format() );
}
endwhile; ?>
</div> <!-- content-masonry -->
<?php verbosa_pagination();
else :
get_template_part( 'content/content', 'notfound' );
endif;
Can you please try this code before the while loop?
<?php query_posts( array( 'author' => 'your author ID' ) ); ?>
Ref: https://developer.wordpress.org/reference/functions/query_posts/#usage
I am looking to automatically create a Wordpress Menu for the 'current' Custom Post Type. I found a useful snippet that outputs the current custom post type here:-
$post_type = get_post_type( $post->ID );
echo $post_type;
But am struggling to translate this (or alternate method) into a dynamically created menu for the current custom post type - list all posts in the custom post type. I can't do this on an individual custom post type basis as I'm using a master template to display a series of custom post types.
Thanks
Glennyboy
Here's a super simple way to do it. just create a loop that lists all the page titles wrapped in a link.
<?php
$obj = get_post_type_object(get_post_type($post->ID));
echo '<h2>' . $obj->labels->name . '</h2>';
?>
<ul>
<?php
$query = new WP_Query(array('post_type' => get_post_type($post->ID), 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date',));
while ( $query->have_posts() ) : $query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
This will loop through all the current post's post type and create an unordered list with all the posts titles, wrapped in links to the posts. You could obviously change the HTML to suit your needs.