Dynamic Image Links Depending on Excerpt/'Single Post' View in Wordpress - image

I'm working on a new Wordpress theme; the default index view displays the excerpts of recent posts. Some posts will be regarding file downloads, and include an image, description, and link to the location where the described files are hosted. The images for these types of posts will be anchored with links(other types of posts may contain images that are not linked).
For these types of posts, I would like the images to link to their entry's full post views(single.php) when displayed in excerpts, but for the same images to link to an external download link when displayed as part of the full post view.
I'm not sure how exactly I would accomplish that.
Any help would be greatly appreciated!

Because I don't have post thumbnails defined, and since posts may or may not contain multiple images, I ended up doing it this way:
I turned off tags in excerpts(I had an excerpt plugin enabling tags in excerpt), then I added the following to the function.php file:
function catch_that_image() {
global $post, $posts;
$first_img = ''; ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img = "/images/default.jpg"; }
return $first_img;}
Then I added the following to the main index template file in between the posts title and the excerpt/content:
<p><img src="<?php echo catch_that_image() ?>"></p>
Random note: It's wrapped in a for styling reasons.
Thanx again for guiding me in the right direction.

if your theme uses 'the_excerpt()' for the front page, I think you can add a filter in functions.php, and with a regexp change the link href from the download link to the permalink.
something like,
function replace_link($content) {
if (is_home())
return preg_replace('regular_expression', get_permalink(), $content);
else
return $content;
}
add_filter('the_excerpt', 'replace_link');
I can't create an actual regular expression without knowing what your download link looks like

Related

Joomla display view not displaying site template

I am trying to load a view from my controller using the follow code but I only get a raw HTML view and does not show the site's template.
$view = $this->getView( 'download', 'html' );
$view->display();
Can some help me in what I am doing wrong to display the site's template.
I also tried a redirect but that did not work either
$this->redirect(JRoute::_('index.php?option=com_atdwcsv&view=download'), false);
Edit: I figured out what was wrong with the redirect. Code I needed was
$this->setRedirect('index.php?option=com_atdwcsv&view=download');
$this->redirect();
I could be wrong, but I don't think you need to use the display() method on the view, I think you need to use $this->display(); instead.

joomla Modules - Between Article or inside Article? What is correct?

I want to place articles and modules on my page. It should look like my scatch:
I am wondering if i can make 4 articles and one module (information box) and place this inbetween those articles, Or should i just create one article wich looks like the hole area (article 1-4 + INformation box)? So far i have tried to do {loadposition infobox-pos} into article 2. But than the box-width is not 100% but 50%.
Than aggain if i would make one big article with the contents from article 1-4 it would not fit great into my responsive layout.
I use T3-Framework.
Joomla doesn't support this natively, since module output is controlled by the template, and component output (in this case, com_content, view=category, layout=blog) is handled before modules are rendered.
You can proceed in many ways, I am listing them in order of decreasing ease:
Write a jquery script that moves the pieces in the browser. You could also use mootools and it's bundled with joomla, but odds are you already have jquery loaded anyways.
Write a view override for abovementioned view: so copy /components/com_content/view/category/tmpl to /templates/your_template/html/com_content/category (copy all files). Then edit blog.php and insert your module there, use something like /plugins/content/loadmodule/loadmodule.php :
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModules($position);
$params = array('style' => $style);
foreach ($modules as $module) {
echo $renderer->render($module, $params);
}
Write a content plugin
you can also use {loadmodule YourCustomModuleTitle} and place this in <div> with custom style attributes like
<div style="width:100%;float:right">{loadmodule MyModule}</div>
or
<div class="something">{loadmodule MyModule}</div>
Note: the module which loaded must be assigned to the same menu item or "all"

How to change load layout in Joomla view?

By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.
P.S.
Tried load loadTemplate and setLayout methods with no luck.
Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax
$this->setLayout('dafault:lol');
parent::display($tpl);
By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.
By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.
However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol
You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.
<?php
# ...
function display($tpl = null) {
# ...
# Edit : Set the default layout to 'lol'
$layout = JRequest::getWord('layout', '');
if (empty($layout)) $this->setLayout("lol");
// Display the view
parent::display($tpl);
}
# ...
I keep coming back to this and I've yet to find a satisfying solution.
What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php
If $tpl is empty or "" then tmpl/default.php is displayed by default.
If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php
I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php
None have worked for me.

Magento Widget - How to confirm access to block code?

I have created my first Magento widget on a test site (same code and configuration as my live site). The widget works perfectly on the test site. However, having moved the code to the live site, I can configure the widget instance, but the output of the widget is not showing on the product page. So I know Magento is pulling information from the widget's config.xml and widget.xml files.
I have confirmed that the correct layout update is being inserted in the core_layout_update table. The information on the live system's table is exactly the same as that on the working test site.
I have confirmed that the file permissions and ownership are correct on the live site.
I have confirmed that I can put another (Magento supplied) widget in the exact spot on the configurable product page (Product Extra Info). So I know that my templating, etc. is not getting in the way of adding the widget in that spot.
I've tried deleting the widget instance and recreating a new instance of the same widget.
I have compared the code to the test site and copied the code straight from the test site. Still no success. I have enabled php_flag display_errors in .htaccess and no errors are shown. And no errors are showing in the exception.log when enabled.
The live and test systems are on different servers, different OS's, different versions of PHP (both 5.3.x). I've refreshed all the caches and confirmed that the test widget is showing so I don't think this is a caching issue.
How is the best way to confirm that Magento on the live site can actually access or find the Block code for my widget? As indicated I know Magento is accessing the widget.xml file in my local code directory. Not sure if installing Alan Storm's CommerceBug is the way to trace or dump what Magento is trying to load (modules, widgets, etc) for the page in question.
Verify that you have your xml in the /app/etc/modules folder.
You can turn on Block Hints by going to Admin->System->Configuration->Advanced->Developer, change the Current Configuration Scope to Main Website and click on Debug. Change Template Path Hints to Yes and save.
Verify your filenames. This has gotten me a number of times. I develop on a Mac (non case-sensitive), and occasionally when I upload to Linux (case-sensitive), it breaks. One of the first thing I check is the file name's case.
Ensure that the class name matches the filename.
The next step I'd take is to take a look at the final page layout xml generated for the page your'e adding the widget to and ensure a bit of Layout XML is being added to the page for your widget. If it isn't, there's something wrong with the Layout XML Updates that are added to the table via the widget UI. If it is, then start debugging why the particular chunk of generated layout XML isn't being added to the page.
This is the top-down approach to debugging the problem, but it's the only way I know to be sure.
I had the same problem. It's my decision:
app\code\local\Mage\Core\Model\Resource\Layout.php
When 'theme' => Mage::getSingleton('core/design_package')->getTheme('layout') result is not correct: 'theme' => 'multistore', should be 'theme' => 'your_theme'.
So, delete argument 'layout' in function getTheme() and get right result
*/
class Mage_Core_Model_Resource_Layout extends Mage_Core_Model_Resource_Db_Abstract
{
...
public function fetchUpdatesByHandle($handle, $params = array())
{
$bind = array(
'store_id' => Mage::app()->getStore()->getId(),
'area' => Mage::getSingleton('core/design_package')->getArea(),
'package' => Mage::getSingleton('core/design_package')->getPackageName(),
// 'theme' => Mage::getSingleton('core/design_package')->getTheme('layout')
'theme' => Mage::getSingleton('core/design_package')->getTheme()
);
foreach ($params as $key => $value) {
if (isset($bind[$key])) {
$bind[$key] = $value;
}
}
$bind['layout_update_handle'] = $handle;
$result = '';
$readAdapter = $this->_getReadAdapter();
if ($readAdapter) {
$select = $readAdapter->select()
->from(array('layout_update' => $this->getMainTable()), array('xml'))
->join(array('link'=>$this->getTable('core/layout_link')),
'link.layout_update_id=layout_update.layout_update_id',
'')
->where('link.store_id IN (0, :store_id)')
->where('link.area = :area')
->where('link.package = :package')
->where('link.theme = :theme')
->where('layout_update.handle = :layout_update_handle')
->order('layout_update.sort_order ' . Varien_Db_Select::SQL_ASC);
$result = join('', $readAdapter->fetchCol($select, $bind));
}
return $result;
}
}

Place certain article anywhere in joomla template

Is it possible in Joomla to place a certain article in a template, additionally to the normal content? I want the article to show up on every page.
You can simply take it from the databse and print its content. In the template, where you want to show the article, write this:
$id=/*Id of the article to show*/;
$db=&JFactory::getDBO();
$db->setQuery("SELECT * FROM #__content WHERE id=$id");
$item=$db->loadObject();
echo $item->introtext;
UPDATE: ENABLE PLUGINS
I can't find where i've used that code and i can't copy-paste it, so i try to write it again by looking at the view.html.php of the com_content:
JPluginHelper::importPlugin('content');
$dispatcher =& JDispatcher::getInstance();
$params = &$mainframe->getParams();
$dispatcher->trigger('onPrepareContent', array (&$item, &$params, 0));
//The last line triggers the onPrepareContent event, so if it does not work maybe you need other events, so try with onAfterDisplayTitle, onBeforeDisplayContent or onAfterDisplayContent
Have you seen this? http://extensions.joomla.org/extensions/news-display/content-embed/7528
It allows you to place any article as a module on your Joomla site. And with modules you can have them displayed site wide.

Resources