php link code - separate the code - php

hi i need help with a small bit of code for my wordpress theme.
<a href="<?php echo $link; ?>">
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.', </li>';
}
?>
</a>
the code
<?php echo $link; ?>
this is for a link of a website that is already on the page the link part works great the only issue is i want the links (tags that are being used as keywords/anchor text)to be seo friendly for outbound links
what changes are needed? feel free to spice the link up for seo :)

If you're wanting the same link for each of the tags, then this is a solution:
<ul>
<?php
global $post;
foreach( get_the_tags($post->ID) as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
?>
</ul>
Please note: I've added the unordered list start and end elements as these were missing, these could also be ordered list start and ends.

EDIT:
to reflect your code changes.
The code still looks the same...I would assume it should look something like this: (and I could be completely wrong) :)
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.'</li>';
}
?>

Related

How to set navbar tabs active when used?

this is my second question here, first one was resolved quite quickly and I appreciate any help I get. Here's the thing:
I have a folder views in which I have a folder named _global in which I have my beforeContent.php and afterContent.php those are my header and footer of actual Web pages. The main content in my pages is set in other view folders and I have no problem there.
I have this script in my beforeContent.php (this one shows my navbar):
<?php if (Session::exists('user_id')): ?>
<?php include 'app/views/_global/menu-session.php'; ?>
<?php else: ?>
<?php include 'app/views/_global/menu-no-session.php'; ?>
<?php endif; ?>
basically, if a user is logged in, it will show menu-session.php, if there's no user logged in, it will show menu-no-session.php.
my menu-session has this in it:
<ul>
<li><a <?php if ($FoundRoute['Controller'] == 'Main') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?>">Home</a>
</li>
<li><a <?php if ($FoundRoute['Controller'] == 'Overview') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?
>overview">Overview</a></li>
</ul>
it has more tabs, but you get the point.
I had to use this function that chooses the controller that will decide if the page is active or not. basically: If i'm on a page Overview, the script asks if the active Controller is named 'Overview' and if it is, it will select the tab as class="active".
However, this is wrong: my teacher said I can't have scripts in my view files (it's not wrong, it's just bad practice) and I need another method of doing this:
So, I created a method class Misc.php with a function Misc::url:
public static function url($link, $text){
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
this way my menu-session can just be:
<ul>
<li>Misc::url('', 'Home')</li>
<li>Misc::url('overview', 'Overview')</li>
</ul>
Now: I need a correct function in Misc.php where I have the >>> $FoundRoute['Controller'] <<< if statement implanted, something like:
Misc::urlWithActive($link, $text){
if ($foundRoute['Controller] = *thiscontroller*) {
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
I actually don't know how to write that.
I Really hope you guys understand what I need to do here and help me.

Wordpress Query on a Page using Allow PHP in Posts and Pages Plugin

It does what I want it to do, displays a certain category of posts in a column on a page. Right now its just the title, but I want to link that title and the permalink section isnt working, or rather the href.
[php]
// The Query
$the_query = new WP_Query( 'cat=3' );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul style="list-style:none;">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . '<a href="the_permalink();">' . get_the_title() . '[/a]'. '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
[/php]
It links to subdomain.domain.com/site/the_permalink(); instead pulling the permalink for that post and linking to it.
the_permalink(); return to echo your link.
You need to return only string. For this you can use get_the_permalink($post->ID);
Because your permalink function is inside of echo function.
When you enter the a-tag you start with HTML, if you close it you change to BBCode.
Fatih is right, you need to use get_permalink function. Since you're inside the loop, you don't need to specify the parameter ($post->ID).
Also, you need to switch back to PHP (that is your main problem) as you did with get_the_title function. There are multiple syntax issues with your code (brackets!).
The line should go like this:
echo '<li>' . get_the_title() . '</li>';
Learn the difference between echo and return in (not only) PHP functions!

Wordpress: Condition Tags PHP

This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.

Problem with PHP Syntax

I am trying to get a link together using 2 variables but the output is the link and title but no html / clickable link appearing.
I'm getting something link this:
http://www.mydomain.com/post1/post_title_here
Here is the code:
echo ''.the_title().'';
Can anyone help please?
Thanks
UPDATE:
Here's the whole block of code:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo ''.the_title().'';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
That's because the wordpress functions the_permalink() and the_title() display the respective outcomes already they need not be echoed. If you want functions that return the values, you have to use get_permalink() and get_the_title() instead.
So either do:
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li>';
echo ''.get_the_title().'';
echo '</li>';
endwhile;
wp_reset_postdata();
?>
</div>
or
<div id="MyBlock1">
<?php
$query = new WP_Query('posts_per_page=5');
while( $query ->have_posts() ) : $query ->the_post();
echo '<li><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;
wp_reset_postdata();
?>
</div>
Both will work.
Here's a checklist for debugging:
1.) Is the_title() returning an empty string? (You can check by looking at the html source)
2.) Are you echoing this inside of the body tag?
3.) Is this being echoed in a hidden html element?
echo ''.the_title().'';
In this sort of situation, you'll want to use get_permalink instead of the_permalink and get_the_title instead of the_title.
echo ''.get_the_title().'';
WordPress the_* functions do a direct echo call, whereas the get_* functions return a value that you can use for further processing, like the concatenation you're doing.
(also note the inconsistent naming conventions - this can be a pain)
You could use the corresponding get_* versions:
echo '' . get_the_title() . '';
See the codex reference for more
You need to absolutely make sure that .the_title(). is definately bieng set a value. If it isn't, then there will be no displayed HTML as there is no text for the anchor tag. Just a thought (i've done it many times, try print_f(); ing the the_title(). Hope it helped.

access and display wordpress posts from out of wordpress

I have a commercial site (php), and have a Wordpress blog in a subdirectory. I need to display latest posts at homepage which is out of Wordpress :/
site:
http://www.blabla.com
blog:
http://www.blabla.com/blog/
So I need to display posts at www.blabla.com/index.php. How can I access Wordpress functionality?
Thanks a lot! appreciate!
The easiest way is to consume your Wordpress RSS feed.
Download it using file_get_contents() or cURL for more control.
Parse it with simpleXML and output it.
You'll probably want to cache it somewhere... you could use APC user functions or PEAR::Cache_Lite.
Edit: the code would look something like this (you'd want more error checking and stuff - this is just to get you started):
$xmlText = file_get_contents('http://www.blabla.com/blog/feed/');
$xml = simplexml_load_string($xmlText);
foreach ($xml->item as $item)
{
echo 'Blog Post: <a href="' . htmlentities((string)$item->link) . '">'
. htmlentities((string)$item->title) . '</a>';
echo '<p>' . (string)$item->description . '</p>';
}
Using WordPress best practices, you shouldn't be loading wp-blog-header.php, but rather wp-load.php, as it was specifically created for this purpose.
After this, use either the WP_Query object or get_posts(). An example of how to use WP_Query is available on The Loop page on the WordPress codex. Although using either of these doesn't matter if you use them from outside WordPress, there's less chance of something interfering, such as GET parameters.
For example, using WP_Query:
<?php
$my_query = new WP_Query('showposts=3');
while ($my_query->have_posts()): $my_query->the_post();
?>
<h1><?php the_title() ?></h1>
<?php endwhile; ?>
Or, using get_posts():
<?php
global $post;
$posts = get_posts('showposts=3');
foreach($posts as $post) :
?>
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
Hope this helps! :)
hey just found a solution online;
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
works great!
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I guess the easiest solution is to take posts directly from database.

Categories