Adding dynamic title to Wordpress - php

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.

Related

expecting statement -php syntax | if-else

<?php if ( wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') !='' ) { ?>
<?php echo wp_get_attachment_image( $item['image']['id'], 'home-post-thumbnail' ); } ?>
<?php else {
?> <img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>">
<?php } ?>
I get critical error.phpstorms says that expecting statements.I couldnt find a solution
You cannot "interrupt" the PHP code after the closing { of the if and before the else keyword.
Cleaned up version:
<?php if (wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') != '') {
echo wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail');
} else {
?><img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>"><?php
} ?>
To avoid such errors, try learning and following a coding standard, e.g. PSR-12.
Since you are working with WordPress, here is WP's coding standard for PHP: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/

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'));
?>

Adding Anchor to every module in Joomla 3.1

I am trying to modify the module template code to add an anchor to the title of each module. I have extremely limited PHP knowledge, so any guidance would be very helpful.
I've found posts for doing this with articles, but the code appears to be very different for modules.
This is the code that is in my module template.
function modChrome_basic($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<?php echo $module->content; ?>
<?php endif;
}
function modChrome_standard($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="rt-block <?php if ($params->get('moduleclass_sfx')!='') : ?><?php echo $params->get('moduleclass_sfx'); ?><?php endif; ?>">
<div class="module-surround">
<?php if ($module->showtitle != 0) : ?>
<div class="module-title">
<?php
echo '<h2 class="title">';
if (preg_match("/icon[-]{1,}/i", $params->get('moduleclass_sfx'))) :
echo '<span class="title-icon ' .getIconClass($params->get('moduleclass_sfx')). '"></span>';
endif;
echo $module->title;
echo '</h2>';
?>
</div>
<?php endif; ?>
<div class="module-content">
<?php echo $module->content; ?>
</div>
</div>
</div>
<?php endif;
}
This is the code I tried
if( strlen($this->item->params->get('title')) > 0 ) {
echo '<a name="'.$this->item->params->get('title').'"></a>';
}
I also tried
if( strlen($module->item->params->get('title')) > 0 ) {
echo '<a name="'.$module->item->params->get('title').'"></a>';
}
Joomla has an easy way to customize the module appearance.
You have to use a Custom module chrome.
You have to create a modules.php file under /templates/TEMPLATE_NAME/html/modules.php.
Inside this file you have to put:
<?php
function modChrome_custom( $module, &$params, &$attribs ) {
echo '<div id="' .$params->get( 'moduleclass_sfx' ) .'" >';
if ($module->showtitle)
{
echo '<h' .$headerLevel .'>' .$module->title .'</h' .$headerLevel .'>';
}
echo '<div class="module_content">';
echo $module->content;
echo '</div>';
echo '</div>';
}
?>
In the code above the module id getting moduleclass_sfx value. You could change it and set another module variable.
In order to use this appearence you have to set the appropriate style to the template module calls like: <jdoc:include type="modules" name="user1" style="custom" />
If you want to use another style name you have to change the function from modChrome_custom to modChrome_NEWSTYLE.
Good Luck!

Wordpress php else if statement with advanced custom fields

I'm trying to make an if-else-statement which works by going if there is a link print it around the name.
I have the below code which is almost there. However, the link is being printed above the text ranther than being an actual link.
<?php if( the_sub_field('corporate_link') ){ ?>
<a href="<?php the_sub_field('corporate_link'); ?>"
target="_blank"
title="<?php the_field('corporate_name'); ?>"><?php the_field('corporate_name'); ?></a>
<?php } else { ?>
<?php the_sub_field('corporate_name'); ?>
<?php } ?>
Any thoughts on how to make it link instead of printing the link if it`s there?
So what im looking to acheive is if there is a link print this
Coprate Name
If there isn't a link it just shows the corporate name.
use get_sub_field('corporate_link') instead of the_sub_field('corporate_link')
<?php
$corporate_link = get_sub_field('corporate_link');
$corporate_name = get_sub_field('corporate_name');
if( $corporate_link != '' ){ ?>
<a href="<?php echo $corporate_link; ?>"
target="_blank"
title="<?php echo $corporate_name; ?>"><?php echo $corporate_name; ?></a>
<?php } else { ?>
<?php echo $corporate_name; ?>
<?php } ?>
use get_sub_field() and get_field() instead of the_sub_field() and the_field()

Website Title Help

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>

Categories