Redirecting to a dynamic page - php

I have a page displaying blog posts [latest_posts.php] and another page that display single blog posts [blog.php] . I intend to link the image title in latest_posts.php so that it redirects to blog.php where it would display the particular post that was clicked.
latest_posts.php ---->
<div class="main blog">
<!-- Header -->
<h2 class="underline">
<span>What's new</span>
<span></span>
</h2>
<!-- /Header -->
<!-- Posts list -->
<ul class="post-list post-list-1">
<?php
/* Fetches Date/Time, Post Content and title */
include 'dbconnect.php';
$sql = "SELECT * FROM wp_posts";
$res = mysql_query($sql);
while ( $row = mysql_fetch_array($res) ) {
?>
<!-- Post #1 -->
<li class="clear-fix">
<!-- Date -->
<div class="post-list-date">
<div class="post-date-box">
<?php
//Timestamp broken down to show accordingly
$timestamp = $row['post_date'];
$datetime = new DateTime($timestamp);
$date = $datetime->format("d");
$month = $datetime->format("M");
?>
<h3> <?php echo $date; ?> </h3>
<span> <?php echo $month; ?> </span>
</div>
</div>
<!-- /Date -->
<!-- Image + comments count -->
<div class="post-list-image">
<!-- Image -->
<div class="image image-overlay-url image-fancybox-url">
<a href="post.php" class="preloader-image">
<?php
echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />';
?>
</a>
</div>
<!-- /Image -->
</div>
<!-- /Image + comments count -->
<!-- Content -->
<div class="post-list-content">
<div>
<!-- Header -->
<h4> <?php echo $row['post_title']; ?> </h4>
<!-- /Header -->
<!-- Excerpt -->
<p>
<?php echo $row ['post_content']; }?>
</p>
<!-- /Excerpt -->
</div>
</div>
<!-- /Content -->
</li>
<!-- /Post #1 -->
</ul>
<!-- /Posts list -->
Browse All Posts
</div>
<?php require_once('include/twitter_user_timeline.php'); ?>
blog.php --->
<?php require_once('include/header.php'); ?>
<body class="blog">
<?php require_once('include/navigation_bar_blog.php'); ?>
<div class="blog">
<div class="main">
<!-- Header -->
<h2 class="underline">
<span>What's new</span>
<span></span>
</h2>
<!-- /Header -->
<!-- Layout 66x33 -->
<div class="layout-p-66x33 clear-fix">
<!-- Left column -->
<!-- <div class="column-left"> -->
<!-- Posts list -->
<ul class="post-list post-list-2">
<?php
/* Fetches Date/Time, Post Content and title with Pagination */
include 'dbconnect.php';
//sets to default page
if(empty($_GET['pn'])){
$page=1;
} else {
$page = $_GET['pn'];
}
// Index of the page
$index = ($page-1)*3;
$sql = "SELECT * FROM `wp_posts` ORDER BY `post_date` DESC LIMIT " . $index . " ,3";
$res = mysql_query($sql);
//Loops through the values
while ( $row = mysql_fetch_array($res) ) {
?>
<!-- Post #1 -->
<li class="clear-fix">
<!-- Date -->
<div class="post-list-date">
<div class="post-date-box">
<?php
//Timestamp broken down to show accordingly
$timestamp = $row['post_date'];
$datetime = new DateTime($timestamp);
$date = $datetime->format("d");
$month = $datetime->format("M");
?>
<h3> <?php echo $date; ?> </h3>
<span> <?php echo $month; ?> </span>
</div>
</div>
<!-- /Date -->
<!-- Image + comments count -->
<div class="post-list-image">
<!-- Image -->
<div class="image image-overlay-url image-fancybox-url">
<a href="post.php" class="preloader-image">
<?php echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />'; ?>
</a>
</div>
<!-- /Image -->
</div>
<!-- /Image + comments count -->
<!-- Content -->
<div class="post-list-content">
<div>
<!-- Header -->
<h4> <?php echo $row['post_title']; ?> </h4>
<!-- /Header -->
<!-- Excerpt -->
<p> <?php echo $row ['post_content']; ?> </p>
<!-- /Excerpt -->
</div>
</div>
<!-- /Content -->
</li>
<!-- /Post #1 -->
<?php } // close while loop ?>
</ul>
<!-- /Posts list -->
<div><!-- Pagination -->
<ul class="blog-pagination clear-fix">
<?php
//Count the number of rows
$numberofrows = mysql_query("SELECT COUNT(ID) FROM `wp_posts`");
//Do ciel() to round the result according to number of posts
$postsperpage = 4;
$numOfPages = ceil($numberofrows / $postsperpage);
for($i=1; $i < $numOfPages; $i++) {
//echos links for each page
$paginationDisplay = '<li>' . $i . '</li>';
echo $paginationDisplay;
}
?>
<!--
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
-->
</ul>
</div><!-- /Pagination -->
<!-- /div> -->
<!-- Left column -->
</div>
<!-- /Layout 66x33 -->
</div>
</div>
<?php require_once('include/twitter_user_timeline.php'); ?>
<?php require_once('include/footer_blog.php'); ?>
Thanks in Advance.

Without seeing code for your problem I estimate the logic to be something like this..
On your latest_posts.php you would do something like this (assuming you have a variable like $post that contains the ID and title, or a schema like that):
<?php echo $post["title"]; ?>
Then on blog.php you'd need to grab the ID from the URL and look it up somehow..
<?php
$id = $_GET["id"];
$post = lookup_post_somehow($id);
if($post) {
// render post
} else {
// 404, blog post not found..
}
?>

You need to do some googling for tutorials. :) there is some great stuff around.
Your blogs are hopefully in a database. Those database records should each have a unique id.
On you latest_blogs page add ?id= (just an example) to pass the idover when the link is clicked.
Now, on your blog page the php variable $_GET['id'] will contain the blog id.
Hope that helps a bit.

Related

Trouble closing if statement and while loop in PHP

I'm trying to display some html inside a php "if statement" and during a "While" Loop.
I keep getting unexpected end of file error and I know there are two braces missing but I can't see where to close the while loop properly.
I'm still new to coding so please be gentle... :-)
tried php syntax checker and other similar posts here
<?php include 'includes/header.php';?>
<!-- Navigation -->
<?php include 'includes/navbar.php';?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
}
else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn)) ;
if (mysqli_num_rows($run_query) > 0 ) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p><h2><?php echo $post_title; ?></h2></p>
<p><h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php';?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Try below:
<?php include 'includes/header.php'; ?>
<!-- Navigation -->
<?php include 'includes/navbar.php'; ?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
} else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn));
if (mysqli_num_rows($run_query) > 0) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p>
<h2><?php echo $post_title; ?></h2></p>
<p>
<h3>by <?php echo $post_author; ?></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext == "mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>"
alt="900 * 300">
<?php endif; ?>
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php'; ?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php
} // End while
} // End if
Note: your code was missing <?php endif; ?> and two braces at the end.
PHP is syntatically more similar to C than to Python or VisualBasic. Indentation means nothing here. Code blocks need to be explicitly open and closed.
There are 2 types to quote the if-else blocks but you did neither of those.
You can either:
use brackets if { ... } else { ... }
Example:
<?php if ($img_ext=="mp4") { ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php } else { ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php } ?>
use colon and keyworkds if, endif
Example,
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else: ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php endif ?>
Just chose which one to go for.
Similarly, you have to close your while-loop with either bracket } or endwhile, depends of your syntax of choice.
you opened the if statement and forgot to close it also while is not closed closed it after the container gets closed

PHP code to change image source on each page with URL or page Title

I am working on running website which is loading main.php file on Each page and it has thousands of pages. Each page url is different e.g,
mywebsite.com/United-Kingdom.php
and
mywebsite.com/United-Stats-of-america.php.
each page having unique page title which is being load by PHP from Database,
(<h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>Information</h4>).
i want to change header jpg image automatically with url or with page title e.g if user go united-kingdom url than image should be loaded /images/country/united-kingdom.jpg on header and when user open United Stats page it should load image /images/country/united-stats.jpg on header.
How can i achieve this ?
= now() ORDER BY fare");
$arr1=mysql_fetch_array($sel1);
?>
<div id="content-top-txt" style="height:auto">
<div id="content-top-inner1">
<div id="content-top-txt-inner1" style="height:auto">
<div class="content">
<h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>specials</h4>
<p style='text-align:justify; font-size:13px; color:#3c3c3c;'>
<strong style="text-transform:uppercase;color:#148ADB;"><?php echo $cityname['country']; ?></strong> with choice of range prices think of comfort for lesser cost<strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> starts from <?php
$tempcode=$arr1['des_code'];
$stemp=mysql_query("SELECT * FROM cities_var WHERE code='$tempcode'");
$temp=mysql_fetch_array($stemp);
if($temp['code']=='')
{
?>
<strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong>
<?php
}
else
{
?>
<strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong>
<?php
}
?>
# <strong>£<?php echo $arr1['fare']; ?></strong> with & table below<?php echo $arr1['des_name'];?> & other cities in <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> About<strong>UK</strong>.General information About <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong>call # 123456789 or use Form or email:- <strong>Email#address.com</strong></p>
<p><?php echo $cvar['overview2']; ?></p>
<p>Note:-Prices given are excluding taxes</p>
</div>
<div id="content-top-inner2">
<div id="content-top-txt-inner2" style="height:auto">
<!-- slider start here -->
<div class="slider-main">
</div> <!-- slider end here-->
</div><!-- Content-top-txt-inner2 end-->
</div><!-- content-top-inner2 end here-->
</div><!--contaent-top-txt end here -->
</div><!--content-top end here -->
<div id="content-middle">
<div id="content-middle-txt">
<?php
include("../../files/lb1.php");
?>
<div class="content-middle-txt-right">
<?php
include("../../files/d-search-compare.php");
?>
</div>
<div style="clear:both"></div>
<div class="more"><p><span>Note : </span><?php //echo $sql_rec['note']; ?></p><p>paragraph text.</p>
</div> <!--content-middle-txt end -->
</div> <!--content-middle end -->
<div id="content-bottom">
<div id="content-bottom-txt">
<div id="bottom-all-box">
<?php
include("../../files/lb2.php");
?>
<div id="content-bottom-right">
<?php
include("../../files/rb1.php");
?>
</div>
</div>
<?php
/*
include("../../files/country-left-box3.php");
include("../../files/country-right-box1.php");
*/
?>
I would change all the image names to the exact page name of the PHP file. So for example you have /united-kingdom.php your image should be named united-kingdom.jpg.
With that the same you can get the current page URL which you requested using $_SERVER['REQUEST_URI'].
Now you can define the image using the current page name.
$url= $_SERVER['REQUEST_URI'];
$imageName = substr($url, 0, strpos($imageName, "."));
The variable $imageName will contain united-kingdom.
So you can define your image like:
<img src="<?php echo $imageName; ?>.jpg" alt="" />
This code is not completely tested but is just to give you an idea on how you can handle it.

Getting images from DB in MYSQL, PDO and putting into slider foreach

This is my code, actually, it actually retrieves data, but, its looking bad.
I use Jquery, (nivoSlider), and Bootstrap loaded in that order.
I have 2 results on DB, it successfully fetched title, description and other information, but it looks bad formatted, or I cannot make it to display the information correctly.
QUESTION: How can I achieve From this-> http://prntscr.com/5imr6e to this-> http://prntscr.com/5imy35 - With its proper thumbnails ?
Any help will be greatly appreciated. Thank you
$sql = "SELECT * FROM homeslider ORDER by id DESC LIMIT 6";
$query = $handler->prepare($sql);
$query->execute();
$row = $query->fetchAll();
return $row;
}
?>
<?php
$data = getContent();
foreach ($data as $row) {
echo '<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
';
$id = $row['id'];
$titulo = $row['titulo'];
$descripcion = $row['descripcion'];
$link = $row['link'];
$imgurl = $row['imgurl'];
$ultimo_update = $row['ultimo_update'];
$captions = '';
}
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = '<div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->
<?php echo $captions; ?>
</div> <!-- Close slider -->
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
Well the first thing to do is switch this to use a more readable output syntax - breaking in and out of php instead of echoing all the html as complete strings. Additionally i think you want all the images to be slides but im pretty sure you are actually creating a slider for each image...
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php foreach ($data as $row): ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php endforeach; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
We could further optimize this by building a big string of the captions in the loop so that we do not need loop over the array twice:
<div id="wrapper">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">
<?php
// initialize captions as an empty string
$captions = '';
?>
<?php foreach($data as $row): ?>
<?php // lets use printf since the majority of this is static with just a couple vars ?>
<?php printf(
'<img src="images/slider/%s" data-thumb="images/slider/%s" data-transition="fold" title="#htmlcaption_%s" />',
$row['imgurl'],
$row['imgurl'],
$row['id']
); ?>
<?php ob_start(); // start a buffer ?>
<div id="htmlcaption_<?php echo $row['id'] ?>" class="nivo-html-caption">
<?php echo $row['titulo']; ?>
<br />
<?php echo $row['descripcion'] ?>
<span class="nivoButtonSpan">
Leer más <i class="glyphicon glyphicon-share-alt"></i></span>
</span>
</div>
<?php
// get the buffer contents, append them to $captions
// and then clear the buffer and stop buffering
$captions .= ob_get_clean();
?>
<?php endforeach; ?>
</div> <!-- Close slider -->
<?php // output the captions ?>
<?php echo $captions; ?>
</div> <!-- Close slider-wrapper -->
</div> <!-- Close wrapper -->
It seems that you are not closing the nivoslider div:
echo'
<img src="images/slider/'.$imgurl.'" data-thumb="images/slider/'.$imgurl.'" data-transition="fold" title="#htmlcaption_'.$id.'" />'; ?>
<?php $captions = 'CLOSE HERE: </div><div id="htmlcaption_'.$id.'" class="nivo-html-caption">
'.$titulo.'<br/>'.$descripcion.'<span class="nivoButtonSpan">Leer más <i class="glyphicon glyphicon-share-alt"></i></span>';?>
</div> <!-- Close htmlcaption_# -->

php code to display rows in two different display

I create two design to display news .
first DIV Display BIGGER images and Separate css class and display only two rows.
Second DIV Display Smaller images and Separate css class and display five rows..
I select rows in asc order from query.
my query is = select * from news order by priority asc.
My Only Need is TO Display two rows in First div and then display another rows in Second DIV.
IS This is possible using if else or any other condition...how to do this.........
but keep all div tag as it is bcoz of page design purpose......
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
</div>
<!-- end post -->
Yes, it's possible. Assuming your $top2 array has standard numerical/sequential keys, then:
$split = 2;
for($i = 0; $i < $split; $i++) {
... print $top2[0] and $top2[1]
}
for ($i = $split; $i < count($top2); $i++) {
... print the rest of the rows
}
Can use array_slice()
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 0, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More </p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
Add a count that you increment in the foreach loop, then break when its over two:
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php
$rowCount = 0;
foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php
$rowCount++;
if($rowCount > 2){
break;
}
} ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->

AddThis in Joomla Category Blog Override. Linking to corresponding articles

I've overwritten the "blog.php", for Category Blog to put the AddThis social media sharing plugin at the bottom of each article. Working on joomla 3.0
The Category blog layout displays many articles per page. By default AddThis uses your current page to share/like/tweet/etc.
My add this code looks like this:
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=undefined"></script>
<!-- AddThis Button END -->
</div>
What I of course want is to change the URL being used to the corresponding article. This is possible (http://support.addthis.com/customer/portal/articles/381242-url-title#.Ucd1HevmT2x).
The code should look like this (only with dynamic urls):
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style"
addthis:url="http://example.com/blog/my-article-about-horses"
addthis:title="The title of my article"
addthis:description="The short article description">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=undefined"></script>
<!-- AddThis Button END -->
</div>
Would be great if someone could help me understand a bit better how I go about doing this.
addthis:url="http://example.com/blog/my-article-about-horses"
addthis:title="The title of my article"
addthis:description="The short article description"
Here is the full blog.php code.
<?php
/**
* #package Joomla.Site
* #subpackage com_content
*
* #copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* #license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers');
JHtml::_('behavior.caption');
?>
<div class="blog<?php echo $this->pageclass_sfx;?>">
<?php if ($this->params->get('show_page_heading', 1)) : ?>
<div class="page-header">
<h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1>
</div>
<?php endif; ?>
<?php if ($this->params->get('show_category_title', 1) or $this->params->get('page_subheading')) : ?>
<h2> <?php echo $this->escape($this->params->get('page_subheading')); ?>
<?php if ($this->params->get('show_category_title')) : ?>
<span class="subheading-category"><?php echo $this->category->title;?></span>
<?php endif; ?>
</h2>
<?php endif; ?>
<?php if ($this->params->get('show_description', 1) || $this->params->def('show_description_image', 1)) : ?>
<div class="category-desc">
<?php if ($this->params->get('show_description_image') && $this->category->getParams()->get('image')) : ?>
<img src="<?php echo $this->category->getParams()->get('image'); ?>"/>
<?php endif; ?>
<?php if ($this->params->get('show_description') && $this->category->description) : ?>
<?php echo JHtml::_('content.prepare', $this->category->description, '', 'com_content.category'); ?>
<?php endif; ?>
<div class="clr"></div>
</div>
<?php endif; ?>
<?php $leadingcount = 0; ?>
<?php if (!empty($this->lead_items)) : ?>
<div class="items-leading">
<?php foreach ($this->lead_items as &$item) : ?>
<div class="leading-article leading-<?php echo $leadingcount; ?><?php echo $item->state == 0 ? ' system-unpublished' : null; ?>">
<?php
$this->item = &$item;
echo $this->loadTemplate('item');
?>
</div>
<div class="clearfix"></div>
<?php
$leadingcount++;
?>
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=undefined"></script>
<!-- AddThis Button END -->
</div>
<?php endforeach; ?>
</div><!-- end items-leading -->
<div class="clearfix"></div>
<?php endif; ?>
<?php
$introcount = (count($this->intro_items));
$counter = 0;
?>
<?php if (!empty($this->intro_items)) : ?>
<?php foreach ($this->intro_items as $key => &$item) : ?>
<?php
$key = ($key - $leadingcount) + 1;
$rowcount = (((int) $key - 1) % (int) $this->columns) + 1;
$row = $counter / $this->columns;
if ($rowcount == 1) : ?>
<div class="items-row cols-<?php echo (int) $this->columns;?> <?php echo 'row-'.$row; ?> row-fluid">
<?php endif; ?>
<div class="span<?php echo round((12 / $this->columns));?>">
<div class="item column-<?php echo $rowcount;?><?php echo $item->state == 0 ? ' system-unpublished' : null; ?>">
<?php
$this->item = &$item;
echo $this->loadTemplate('item');
?>
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=undefined"></script>
<!-- AddThis Button END -->
</div>
</div><!-- end item -->
<?php $counter++; ?>
</div><!-- end spann -->
<?php if (($rowcount == $this->columns) or ($counter == $introcount)): ?>
</div><!-- end row -->
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php if (!empty($this->link_items)) : ?>
<div class="items-more older-articles">
<?php echo $this->loadTemplate('links'); ?>
</div>
<?php endif; ?>
<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel != 0) : ?>
<div class="cat-children">
<h3> <?php echo JTEXT::_('JGLOBAL_SUBCATEGORIES'); ?> </h3>
<?php echo $this->loadTemplate('children'); ?> </div>
<?php endif; ?>
<?php if (($this->params->def('show_pagination', 1) == 1 || ($this->params->get('show_pagination') == 2)) && ($this->pagination->get('pages.total') > 1)) : ?>
<div class="pagination">
<?php if ($this->params->def('show_pagination_results', 1)) : ?>
<!-- <p class="counter pull-right hidden-phone"> <?php echo $this->pagination->getPagesCounter(); ?> </p> -->
<?php endif; ?>
<?php echo $this->pagination->getPagesLinks(); ?> </div>
<?php endif; ?>
</div>
Update
Thank you Ahmad for your answer. It helped out a lot.
This is my current AddThis code within the blog_item.php file. It works well for Twitter, email etc. But I have some issues when it comes to Facebook. The "addthis:title" and description are being not being used. Instead the OG tags I have in the index.php file are being used. It still links to the correct article when the shared link is clicked on Facebook. This can be a bit confusing for the user - when he/she get's the Facebook share pop-up there is nothing about the article that he/she wants to share - only info about the page in general.
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style"
addthis:url="http://www.tolt-inspiration.com<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>"
addthis:title="<?php echo $this->escape($this->item->title); ?>"
addthis:description="<?php echo $this->escape($this->item->metadesc); ?>">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"
addthis:title="<?php echo $this->escape($this->item->title); ?>"
addthis:description="<?php echo $this->escape($this->item->metadesc); ?>"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<!-- AddThis Button END -->
</div>
How to fix that?
Note on editing core files
First of all let's clarify that editing Joomla's core file is a bad practice. You should always use overrides. This way you make sure that your changes wont get overwritten when Joomla is updated.
update: didn't notice the question title saying that OP is working on override already.
How Joomla category view is rendered
Let's dissect the category view in Joomla (in order of execution). It starts with default.php (mother of all views) Then either loads:
blog.php (for blog view; obviously) This part output the category description, image, title, columns ... etc and will call the following parts:
blog_item.php -- display the article <- This is the file you should be working on.
blog_links.php
default_items.php (for list view)
For Joomla 1.5 before <span class="article_separator"> </span> add the code
For Joomla 2.5 before <div class="item-separator"></div> add the code
The code
<div class="article-sharing">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style"
addthis:url="<?php echo $this->item->readmore_link; ?>"
addthis:title="<?php echo $this->escape($this->item->title); ?>"
addthis:description="<?php echo $this->escape($this->item->metadesc); ?>">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
Adding the <script>
Note that now we didn't add addthis <script> yet. You don't want to be adding it to blog_item.php or it will be added multiple times. We will add it to blog.php. To add it using Joomla's API:
Right after:
defined('_JEXEC') or die('Restricted access');
Add:
$doc =& JFactory::getDocument();
$doc->addScript("//s7.addthis.com/js/300/addthis_widget.js#pubid=undefined");
Note
Adding the script using the addScript method is not required but it is preferred than just adding it simply in the template. Some plugins for example move every script tag right before closing the body tag; It provides a way to grasp that object.

Categories