I am learning WordPress theme development and I have developed a theme. All work has been done, but the author name not displaying in a blog page. I used the following:
<?php the_author(); ?>
but it is not working. Is it necessary to do any function or code in functions.php?
Basically, this is the loop of a page in wordpress:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
some wordrpess code
<?php endif; ?>
You have to enter the get request INTO the loop, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $author = get_the_author(); ?>
or
<?php the_author(); ?>
<?php endif; ?>
If not within the loop you can try this
<?php the_author_meta( 'display_name', $userID ); ?>
From Codex
Related
So I want to return a Wordpress loop that returns just one category, so currently I have something like this
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This works great and does exactly what I want it to do
But I want the category number to be set by an ACF, so I have that set up and it's returning my value just as a string on the site, all good again, so now my code looks like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
What I want to do is something like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
I don't think I'm going about this the right way and can't seem to find an answer for this as I don't think I'm looking for the right thing .. if someone could point me in the right direction on this I'd be really grateful
Use get_the_field instead of the_field in query posts method something like this
query_posts('cat=get_the_field("category_number")');
note:- the_field() is echo everything which field contain.
get_the_field is only get the field not echo it.
Hope it will help you :)
Have you tried something along these line?
https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command
<?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Or Maybe something like this?
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $first = !isset( $first ) ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php endwhile; ?>
The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?
Try this
<?php $Inc=0; ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $Inc==0 ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php $Inc++; ?>
<?php endwhile; ?>
I'm currently working on integrating wordpress with my website, but I'm hitting a wall here.
I need the initial blog page(basically just the index.php of WP) to pick up the <!--more--> quicktags, but I can't seem to get it to work.
I've followed the WP Codex, but that didn't help me at all.
So here's part of the template file: content.php
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_excerpt();
?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<div class="bthumb2">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(220, 130));
}
?>
</div>
<?php
the_content("READ MORE");
?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'tinfoilhats' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php endif; ?>
But it just keeps displaying the <!--more--> tag in the posts once I load the page.
I've tried it with the global $more thing, but that didn't work for me.
<?php while ( have_posts() ) : the_post(); ?>
<?php
global $more;
$more = 0;
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
So what am I doing wrong?
Your global $more example looks good, and almost follows the recommendation on the documentation page applicable (the global $more line itself is supposed to be outside "the loop" - before while ( have_posts() ) - leaving the next line where it is.)
However, I couldn't get this to work on my own blog (using a a child theme of Coraline) until I moved both lines to before "the loop". Starting with your code, I'd suggest something like this:
<?php
global $more;
$more = 0;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
// your output html
<?php endwhile; ?>
You have nested the loops (missing endwhile).
You cannot just invent a variable "query_string2" and expect it to work ;)
Try this:
<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:
<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
This will work:
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?
This will help me aswering more correctly.
I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)
<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page
https://codex.wordpress.org/Function_Reference/rewind_posts
I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/