How to show author name only on a post - Wordpress - php

This code should probably be a LOT smaller, but I can't seem to figure it out.
This is what I want:
[when the visitor is on a post]
Written by: the magnificent author of this piece
[When the visitor is on anything else then a post]
...... (nothing here)
Simply put:
This is the titel!!
Written by the magnificent author (I only want this to show on a post, not on > a page or any other page)
The text in the article is then shown here as normal. Regardless if
the author is shown or not.
This is the code I've got so far...
<?php
if (is_single() || is_page()) {
while (have_posts()):
the_post();
# page + post
wikiwp_get_thumbnail($post);
# post only
if (is_single()) {
?>
<?php
_e('<p>Written by:', 'wikiwp');
echo ' ';
the_author_posts_link();
?>
<?php
} else if (is_page()) {
Stuff that is shown when they are on a page
?>
<?php
}
endwhile;
} else {
The stuff that is shown when they are on anything else
?>
<?php
}
?>
The text of the article......
I'm pretty sure the code above doesn't make any sense.... But my limited PHP experience isn't really helping lol.

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

Content visibility based on multiple url values with strstr?

Using a Wordpress plugin to sort custom post types based on custom taxonomies. Results are returned on the post type's archive page. Sorting is working great but hit a snag trying to display header content for items with multiple taxonomies:
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating")) { ?>
<h3>These poses are invigorating:</h3>
<?php } ?>
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating/position/seated")) { ?>
<h3>These poses are invigorating and seated:</h3>
<p>For positions of this nature please take care of your sacrum.</p>
<?php } ?>
<?php if(strstr($_SERVER['REQUEST_URI'], "position/seated")) { ?>
<h3>These poses are seated:</h3>
<?php } ?>
"level/invigorating/position/seated" returns three h3's because all if's return true.
My PHP skills are awful and trying to understand the manual is like staring into the night sky.
How can this be written so only one value can be returned per page?
Perhaps this screenshot helps.
Using Beautiful Taxonomy Filters.
I believe what you are trying to do is this...
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating/position/seated")) { ?>
<h3>These poses are invigorating and seated:</h3>
<p>For positions of this nature please take care of your sacrum.</p>
<?php } elseif(strstr($_SERVER['REQUEST_URI'], "level/invigorating")) { ?>
<h3>These poses are invigorating:</h3>
<?php } elseif(strstr($_SERVER['REQUEST_URI'], "position/seated")) { ?>
<h3>These poses are seated:</h3>
<?php } ?>

Correct way to write an IF statement but without an ENDIF?

Am I doing this right? It works but wanted to ask if what I have done is bad practice?
Originally if the page is home then will get include a php file otherwise if it is a generic page will call in the featured image in WordPress.
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} ?>
<?php echo get_the_post_thumbnail($post->ID); ?>
Your code is wrong in my opinion based on what you requested in question. You only check if is home and if it is get template part 'hero'. After that no matter what page is you call get_the_post_thumbnail.
otherwise if it is a generic page will call in the featured image in WordPress
otherwise means else and your code should look like this:
<?php
if ( is_page('home')) {
get_template_part( 'hero' );
} else {
echo get_the_post_thumbnail($post->ID);
}
?>
Do not forget to be in loop to call $post->ID
You can aford endif by using <?php if(){ ... } ?> or <?php if(){ ?> ... <?php } ?>
no, it's okay. If the condition is not fulfilled, nothing will happen.

Pagination Wordpress in Custom Theme

Hi I hope someone can help with this.
I'm building a web site using wordpress, but I created my custom themes.
I just have basic knowledge of php and i managed to build the home page which show the latest 4 post.
This is the code that i use to show the last and the second-last post. Just using an offset and skipping one post every time to show the previous.
LATEST POST SCRIPT
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php echo catch_that_image() ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
SECOND LAST POST SCRIPT
</span></td>
<td colspan="8" rowspan="2" valign="top" class="blog_text">
<?php
query_posts('showposts=1'); ?>
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<?php the_content(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
I hope this part help to understand, because i'm working in local so i can't show the website for now.
Now my problem is how i can dynamic implement the pagination? so if the first page show the post 1,2,3,4
i need to create a dynamic pagination which would allow me on clicking "previous posts" or the number of the page
will show the post number 4,5,6,7.
I've already had a look on the tutorial for dynamic pagination but i can't understand how i can do it, as i'm working on wordpress
and my script are different! As i said i have basic knowledge of PHP so if someone could explain me how to do with my code would be perfect!
Probably i don't need the foreach because this was a code found to show from post 1 to post 4 for example, and i changed to show just one!
I basically can't use a normal loop because the post that i have to show are in diferrent postion so i need separate code!
I hope i could explained myself! Thank you to everyone for any help.

WordPress : Simple php IF statement not processing

can someone explain why the else statement is not working in WordPress for the sidebar.php?
<?php if(is_front_page() ) : ?>
**content shows on main page sidebar**
<?php elseif(!is_front_page() ) : ?>
<?php // else: ?> // tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php endif;?>
The is_front_page() conditional will always return false if it is used inside the loop or after the loop has ran (like in a sidebar). You can call wp_reset_query(); after your loop to reset the page query_vars.
See this answer on WPSE for more info.
Im not sure if is_front_page() exists, but just use {} around each condition result:
<?php if(is_front_page() ) { ?>
**content shows on main page sidebar**
<?php } else { ?>
// tried **else:** also
**some content**
**nothing is shown on any other page...**
<?php } ?>

Categories