basically I have my site setup to display the title of each page/post in the header of my theme. I also have it setup to display the name of an author on my author pages(I did this using a conditional). The problem I'm having is that the name of the author is echoed for the number of posts the author has written and I only want it to echo once. Is there a way I can do this? I realize that its doing this because the_author has to be in a loop(I have a loop in my header to do this). Is there anyway to be able to display the_author outside of the loop?
My site is http://www.imagineitstudios.com . you can see what I'm talking about if you click on the "Posted by Abel" link on the first post of the home page
Thanks for your help.
Here's my code:
<div id="title">
<?php //Check to see if this is an author page ?>
<?php if(is_author()): ?>
<?php while (have_posts()) : the_post(); ?><?php //Creat a mini loop to display the author ?>
<h1><?php the_author();?></h1>
<?php endwhile;?><?php //End of Mini Loop ?>
<?php else :?>
<h1><?php the_title();?></h1>
<?php endif ;?>
</div>
You can use the following code:
<div id="title">
<?php //Check to see if this is an author page ?>
<?php if(is_author()): ?>
<?php
if(get_query_var('author_name')) :
$curauth = get_userdatabylogin(get_query_var('author_name')); ?>
<?php else :
$curauth = get_userdata(get_query_var('author')); ?>
<h1><?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?></h1>
<?php endif; ?>
<?php else :?>
<h1><?php the_title();?></h1>
<?php endif ;?>
</div>
Related
I have wordpress custom theme made from scratch including:
header.php, footer.php, single.php, index.php, functions.php
For Listing the blog list i used index.php with excerpt and read more link to the respective post.
index.php
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="single_excerpt">
<h1 class="red_text"><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
<button class="btn btn-default" type="">Read More</button>
</div>
<?php endwhile; ?>
functions.php
function new_excerpt_more( $more ) {
return '';
}
add_filter('excerpt_more', 'new_excerpt_more');
This works fine as i expected, but the problem is with single.php as when i want to add read more tag to the post content after the first paragraph,nothing is happened.
please help me to add read more tag from backend to a post content after a first paragraph or as desired and then on single post view only the content will display which is before the tag and after that a read more button and after clicking the button full content will display on the same single view of post.
single.php
<div class="col-lg-12 col-md-12 col-sm-12 ">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="single_post">
<h1 class="red_text"><?php the_title(); ?></h1>
<div class="post_info">
Posted by: <?php the_author_posts_link(); ?> | Posted on: <?php the_date(); ?> | Posted in:<?php the_category(', '); ?> | <?php comments_popup_link('No Comment', '1 Comment', '% Comments'); ?>
</div>
<?php the_content(); ?>
<?php comments_template( '', true ); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="post">
<h3><?php _e('404 Error: Not Found', 'brightpage'); ?></h3>
</div>
<?php endif; ?>
</div>
The more function doesn't work on single.php by default. You should add global $more; $more = 0; to your loop:
<?php
if(have_posts()) :
while(have_posts()) :
the_post();
global $more;
$more = 0;
the_content();
$more = 1;
the_content('',true,'');
?>
Now you have the content with text before the more tag first ($more = 0), followed by the text after the more tag ($more = 1). With javascript you can show-hide the content onclick.
You can user the_excerpt(); in the place of the_content();
But how you can add read more in single.php file. All read more link for post goes to single.php.
So I suggest to you don't user read more in single.php
i'm trying to build a navigation for a website, and i'm struggling with it. I'm trying to make a foldable navigation that shows only categories at first, when clicked, they link to the category, and show current category posts only.
I came this far:
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
The problem with this is: when i fill in the query so that it only takes the current cat, it displays current cat posts on both my categories.
this is what i want for the nav actually:
graphic design
Other Projects
when clicked on graphic design:
graphic design
Project 1
Project 2
Other Projects
when clicking on Other projects:
graphic design
Other Projects
Project 1
Project 2
so basically:
- when clicking from Index page to a category, only that category should expand
- when clicking the other category, the current category changes, so the previous category collapses and the other one expands.
and a bonus: is it possible, that when on a single post, there expands another level of info? for example, a few custom fields per post. like this:
graphic design
Other Projects
Project 1
custom field 1
custom field 2
…
Project 2
thank you very much
Try this code
<?php $article_categories = get_categories(array(
'child_of' => get_category_by_slug('graphic design')->term_id
));
$talentChildren = get_categories(array('child_of' => get_category_by_slug('Project 1')->term_id));
?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<div class="post-list">
<?php foreach($talentChildren as $talent): ?>
<?php
$talentSubChildren = new WP_Query();
$talentSubChildren->query(array('category_name' => $talent->slug));
?>
<h2><?php echo $talent->name; ?></h2>
<ul>
<?php while ($talentSubChildren->have_posts()) : $talentSubChildren->the_post(); ?>
<li>
<?php talent_thumbnail(); ?>
<h4>
<?php the_title(); ?>
</h4>
<p><?php the_excerpt(); ?></p>
read on »
</li>
<?php endwhile; ?>
</ul>
<?php endforeach; ?>
<?php if($wp_query->max_num_pages!=1):?>
<div class="pagination">
<?php previous_posts_link('« prev') ?>
<span class="current"><?php echo $wp_query->query['paged']; ?></span>
of <span class="total"><?php echo $wp_query->max_num_pages; ?></span>
<?php next_posts_link('next »') ?>
</div><!-- .pagination -->
<?php endif; ?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
Ive gotten a bit further, but i need some help now.
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo '<h2><a href="./?cat='.$cat->term_id.'">'.$cat->name.'</h2>';
// create a custom wordpress query
query_posts("cat=$cat_id"); ?>
<?php if (in_category($cat_id)) { ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?><br>
<?php endwhile; ?> <?php endif; // done our wordpress loop. Will start again for each category ?>
<?php } else { ?>
<?php } ?>
<?php } // done the foreach statement ?>
So this is working now: it lists the posts per category, if in the current category, else it doesnt show anything.
now what i still want: i want to add something extra within the loop IF i'm on a single page. I have this code:
<?php wp_reset_query(); ?>
<?php if (is_single('84')) { ?>
Yes
<?php } else { ?>
no
<?php } ?>
But that would mean i have to break a query in the middle of a loop. and the is_single thing does not work inside a loop / without the query reset.
I want it looking like this with above code:
Graphic Design
project 1 (for example id=84)
Yes
project 2 (for example id=101)
No
thanks
Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.
I am trying to pull latest posts from a specific category.
I am currently able to pull all latest posts and display them the way I want using the code below but I am unable to do the same thing from a specific category.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="content"><div id="circle"><div id="circle_text1"><p><?php the_time('M') ?></p></div>
<div id="circle_text2"><p><?php the_time('dS') ?></p></div></div>
<div id="text"><div id="title"><p><?php the_title(); ?></p></div>
<div id="name"><p>By <?php the_author(); ?></p></div>
<div id="blurb"><p><?php the_content('<br />Read More'); ?></p></div></div>
<div id="line_rule"><p> </p><hr /></div></div>
<?php endwhile; ?><?php else : ?><h2>Not Found</h2><?php endif; ?>
Thanks in advance
This is a basic WP query that resets itself and can be used multiple times in a template. You can add your html to it. showposts is the number of posts to show; -1 shows all posts.
<?php $my_query = new WP_Query('category_name=mycategoryname&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
if you are getting posts from database then use order by ID desc that will show latest post on the top. if ID is auto-incremented. like
select * from posts order by ID desc
I do this all the time. This will work:
Change the cat= number to whatever category ID you want to use.
Add html markup as you please and whatever other content you want to pull.
<?php query_posts('cat=4&order=ASC');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
In the example I am only grabbing the post content, but you could put in the title, meta-data, etc. The important parts are all here.
Try it out.
Add this line of code above your opening IF statement.
<?php query_posts( 'cat=1' ); ?>
And then change the 1 to match the ID of the category you are trying to display.
:)
You can do that by simply by adding this first line to your code:
<?php query_posts( 'category_name=slug_of_your_category&posts_per_page=10' ); ?>
Replace "slug_of_your_category" with the slug of your category, and "10" with the amount of posts you need.
I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?
this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
http://ivry.sweetyams.ca/?s=new
and a search not working here:
http://ivry.sweetyams.ca/?s=asjdfkl%3B
Code I am using:
(I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page
<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */ if (is_search() ) { ?>
<?php query_posts('category_name=0&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
<?php query_posts('category_name=escalade&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php query_posts('category_name=montagne&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`
I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.
The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:
This is getting every post below escalade, OR every post below the montagne category
My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.
first, showposts is deprecited since v2.1, used posts_per_page instead.
second, after each query_post, you need to reset the query like this:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
this is how I understand it. see more detail here:query_posts(), hope this helps.
I found a way to get it to work,
I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/
also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/
This code checks whether you are on a category or subcategory,
<?php
$my_posts = new WP_Query(); ?>
<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=6');?>
<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=14'); ?>
<?php } /* AND OTHER PAGE */ else { ?>
<?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>
<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
<p><?php echo substr($post->post_title,0,200); ?></p>
</a>
<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>