Website Title Help - php

I'm working on adjusting my site title in Wordpress. I want my site title to admit the "Home >>" text from the title only on the homepage. Here is the code that i'm working with:
<title><?php if ( is_home() ) { ?> <?php } ?> <?php echo ucwords(wp_title('',false)); ?> » <?php echo ucwords(get_bloginfo('name')); ?> </title>
As i'm new to php, I'm trying decypher the coding. Would an if-else statement serve better?
<title><?php if ( is_home() )
echo "Blah blah blah";
else
echo "<?php echo ucwords(wp_title('',false)); ?>
» <?php echo
ucwords(get_bloginfo('name')); ?>";
?></title>
{ ?> <?php } ?>
<?php echo ucwords(wp_title('',false)); ?> » <?php echo ucwords(get_bloginfo('name')); ?>
</title>
I would greatly appreciate your opinion. My website is http://www.merrimentdesign.com

<title>
<?php if ( is_home() ) { ?> Home title, you don't need to echo something <?php } ?>
<?php echo ucwords(wp_title('',false)); ?> »
<?php echo ucwords(get_bloginfo('name')); ?>
</title>
Edit:
<title>
<?php
if ( is_home() ) { //I'm in the homepage
?>
Home title, you don't need to echo something
<?php
}else{ //every page but homepage
echo ucwords(wp_title('',false)) . '»' . ucwords(get_bloginfo('name'));
}
?>
</title>

An inline comparison would serve you well
<title>
<?php
echo (ishome()? "isHome evaluated to true": "isHome evaluated to false");
?>
</title>
Additionally nested PHP tags will not work and will simply throw errors.
IE
<?PHP
//everything in here is already php, if you add this:
echo "echo <?php doSomething(); ?>";
?>
Will not work because the ?> tag within your "echo" statement will be treated by PHP as the end of the PHP code block, not a string literal.

Instead of
echo "<?php echo ucwords(wp_title('',false));
just do
echo ucwords(wp_title('',false));
You can't echo PHP tags and have them be executed as PHP code.

That's what I do in my blog. But it won't work like it is - change it to:
<title><?php if ( is_home() )
echo "Blah blah blah";
else
echo ucwords(wp_title('',false)); ?>
»
<?php echo ucwords(get_bloginfo('name')); ?></title>

Related

How to use PHP Print function to print a html tag containing PHP code?

I want to do something in PHP like:
<?php Print '<h1> <?php echo "Hello"; ?> </h1>' ?>
How can I do that? (I want the output to be Hello in browser, NOT <h1> <?php echo "Hello"; ?> </h1> in browser)
You just do :
<h1><?php echo ‘Hello’;?></h1>
Or
<?php echo ‘<h1>Hello</h1>‘; ?>

PHP if function joomla no work

Hello everyone and thank you in advance that I will answer. I have this problem, I want to see the second page where I find a link that takes me home page blog if in other categories.
I developed this little code:
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php $link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53" ?>
<?php $homeblog = "Blog" ?>
<?php $doc = JFactory::getDocument(); ?>
<?php $page_title = $doc->getTitle(); ?>
<?php echo $page_title; ?>
<?php if ($page_title != $homeblog) ?>
<?php echo "<a href='".$link_address."'>Home Blog</a>"; ?>
<?php endif; ?>
<?php echo $this->escape($this->params->get('page_title')); ?>
Unfortunately although not error the result is the same, or as if what I wrote was invisible. In homepage obviously sees the home page link (and that is the rule I)
Your code is missing a colon at the end of if statement.
Change
if ($page_title != $homeblog)
To
if ($page_title != $homeblog):
Also dont use <?php and ?> every line. You can have the code like this
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php
$link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53";
$homeblog = "Blog";
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
echo $page_title;
if ($page_title != $homeblog):
echo "<a href='".$link_address."'>Home Blog</a>";
endif;
echo $this->escape($this->params->get('page_title'));
?>

CMS block only on first category page

I'm using Magento 1.9 and struggling on one thing.
I have a CMS block in my category page but I want it to show only on the first page! So if I scroll down and move to page 2 on the same category I don't want to see that CMS block again.
I tried to put this code in the CMS block... but it ignores me
(category-accordion.accordion is the main div of the CMS block)
<script>
if (window.location.href.indexOf("?p=") >-1)
{document.getElementsByClassName('category-accordion accordion')[0].display='none';}
// ]]></script>
Any idea?
EDIT:
tried the following code on the category page:
<?php if($this->isContentMode()): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php elseif($this->isMixedMode() && (strpos($_SERVER['REQUEST_URI'], '?=p') !== true)): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getProductListHtml() ?>
<?php elseif($this->isMixedMode() && (strpos($_SERVER['REQUEST_URI'], '?=p') !== false)): ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
<?php echo $this->getProductListHtml() ?>
<?php endif; ?>
you can set condition in Category View Template catalog\category\view.phtml.
<?php
$currentPage = (int) $this->getRequest()->getParam('p', 1);
if($currentPage <= 1) {
echo $this->getCmsBlockHtml()
} ?>
try this.
<script>
if (window.location.href.indexOf("?p=") >-1)
{
jQuery('.category-accordion.accordion').hide();
}
</script>

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

Adding dynamic title to Wordpress

I am trying to create a dynamic <title> with Wordpress, so far I have this:
<title><?php bloginfo('name'); ?> <?php if(wp_title('', false)) { echo '|'; } else { echo '| Dublin, Ireland';} ?> <?php wp_title(''); ?></title>
Then when I try to add the tagline, instead of hardcoding the location, it throws an error:
<title><?php bloginfo('name'); ?> <?php if(wp_title('', false)) { echo '|'; } else { echo <?php bloginfo('description'); ?>;} ?> <?php wp_title(''); ?></title>
What other ways can this be written?
You have some unnecessary openint and closing php tag.
Use this:
<title><?php bloginfo('name'); if(wp_title('', false)) { echo '|'; } else { echo bloginfo('description'); } wp_title(''); ?></title>
NOTE: I really don't like thats style of coding how the Wordpress guys do. A lot of unnecessary opening and closing tag in every line of their code.
I much more like this style:
<title><?php
bloginfo('name');
if (wp_title('', false)) {
echo '|';
} else {
echo bloginfo('description');
} wp_title('');
?></title>
Using the_title and add_filter
add_filter('the_title','some_callback');
function some_callback($data){
global $post;
// where $data would be string(#) "current title"
// Example:
// (you would want to change $post->ID to however you are getting the book order #,
// but you can see how it works this way with global $post;)
return 'Book Order #' . $post->ID;
}
try this.

Categories