Programmatically get html of top.links in magento - html

I am trying to get the html of top.links using the following ways:
$blockHtml = Mage::getModel('cms/block')->getBlockHtml('top.links')
$blockHtml = Mage::app()->getLayout()->getBlock('top.links').toHtml()
$blockHtml = Mage::getSingleton('core/layout')->getBlock('top.links')->toHtml()
None of above is working for me, how I can do this?
Thanks.
UPDATE
I used
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('page/html')->setTemplate('page/html/top.links.phtml')->toHtml();
With the help of this question Add Top Links on condition basis in magento but still no luck. During this try I found that the use of top.links.phtml is deprecated, any idea which template should I use for the links?
I think there is some dependency for top.links.phtml file, that's why it isn't working, when I tried to get footer.phtml it worked perfectly with above method.

Just like the op, I tried many ways without success. The following simple line finally does it:
<?php echo $this->getLayout()->getBlock('top.links')->toHtml(); ?>

Wow! I was able to find a correct answer finally :) Load block outside Magento, and apply current template
So by following the the above question's answer, I did this to get generated top.links
$layout = Mage::app()->getLayout();
$layout->getUpdate()
->addHandle('default')
->load();
$layout->generateXml()
->generateBlocks();
echo $layout->getBlock('top.links')->toHtml();

If you have created CMS block named 'block_identifier' from admin panel. Then following will be code to call them in .phtml
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();
?>

Please,used the belowCodes.This will be working
echo $this->getLayout()->getcreateBlock('page/template_links')->toHtml();

Related

PhpStorm - Make blade recognize (and autocomplete) variables

The problem
I'm working on a project, that has a lot of blade-files. But PhpStorm doesn't seem to play nice, with mixing <?php ... ?> and #php ... #endphp.
As you can see here, PhpStorm says that $position_link isn't defined, yet it is just a few lines above (this image serves the purpose of displaying the IDE-warnings/errors):
It's the same problem with $data, but as you can see, on line 98, it doesn't complain. Hmm!
The question
Ideally: How do I make PhpStorm understand this code better?
Next best thing: How do I hide these warnings?
Solution attempt 1: Blade Plugin
I can see that the plugin called 'Blade' is now bundled with PhpStorm.
Solution attempt 2: Googled around
I found a bunch of articles about this:
Type hinting works within php block but not in blade.
My comment: I guess this is the same issue. If PhpStorm thinks the variable doesn't exist, then I assume that it can't autocomplete it either. The solution on that article is to open a new issue. Both linked issues are incomplete.
Make variables autocomplete in the PhpStorm 9 for Blade templates
My comment: This was 6 years ago. I refuse to believe that nothing has happened on this area since. The marked solution is that it isn't supported. The most upvoted is to add this on the line before: #php /** #var $position_link #endphp. But it doesn't work. And even if it did - then it will bloat the code immensely.
Solution attempt 3: <?php instead of #php
I also tried changing this:
#php
$position_link = 'left';
if( App\isset_with_value( $data, 'position_link' ) ){
$position_link = $data->position_link;
}
#endphp
<div class="links-wrapper" style='text-align: {{ $position_link }};'>
with this:
<?php
$position_link = 'left';
if( App\isset_with_value( $data, 'position_link' ) ){
$position_link = $data->position_link;
}
?>
<div class="links-wrapper" style='text-align: {{ $position_link }};'>
... But is still shows the error.
Solution attempt 4: Remove Blade completely
If I just write the whole thing in good ol' PHP, then I get a stylelint error:
I know this is another problem, which has to be solved in my Stylelint-settings. I figured I'd just show that this isn't a solution (for me) either.
My system specs
MacBook Pro 16-inch, 2019
OS: Big Sur, 11.6
PhpStorm version: 2021.2.2 - PS-212.5284.49

formatting posts in wordpress

I'm developing a wordpress theme, but I'm stuck in formatting the single.php.
I have in my posts a slideshow plugin which is load with the_content() function, togheter with the text of the post, and with the_title() load the title.
that seen like this:
<h1>the_title()</h1>
<div id=post>the_content</div>
the problem is, I need customize how it's display.
I need display:
<div>theplugin</div>
<div id=post>
<span>the_title</span>
the text
</div>
I try to do that with add_filters but I wasn't lucky.
I hope you can understand my explanation, if you need more details, just tell me.
Thanks in advanced.
If users can add plugin components to posts in the editor, they are usually added via shortcodes.
If that's the case with your plugin, you can add it with apply_filters(). Here's an example of how to add a slideshow from the popular nextgen gallery plugin outside the post content:
<?php
$slideshow = apply_filters('the_content', '[slideshow id=1]' );
echo $slideshow;
?>
The above code can be added into single.php, any static page's page template file or directly into header.php to display on all pages.
If you specify the plugin you're using, I'll update the answer accordingly.
If it should indeed be called directly via a function, I second Ancide's answer.
You just need to change the place of where the functions are situated. Like this:
<div>theplugin</div>
<div id=post>
<span><?php the_title(); ?></span>
<?php the_content(); ?>
</div>
Edit: I misunderstood the description of the problem. Here's my answer now that I understand what the problem is:
The plugin uses a hook for the_content function. If you look inside all the php-files inside wp-content/plugins/your-plugin there will be a file with this code:
add_action('the_content','some-function-name');
This tells wordpress to run the function some-function-name everytime the function the_content is run. So you need to look for the some-function-name to figure out how to customize the output.

Wordpress search results order

I have a problem with pagination on results. I need to order the results by title and by alphabetic order (A->Z) , its possible? I tried some different approaches but none work as needed, the best i have so far is list the results by title and ASC but if i go to "next page" i get always the same results.
Any idea?
Thanks.
It looks like you are on the right track but you are getting the same results on each page because Wordpress is losing track of what page it is on internally. This is done with the $paged global.
If you are modifying the sorting with query_posts, make sure you are also passing in the global $paged var ('&paged='.$paged ). Also, make sure that global exists on the page as well before calling it in query_posts, otherwise it will always be 0 and return the same results on each page. If you are using WP_Query instead of query_posts, this is a good run down. http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
This is what I'm using, add it on index.php
<?php
$posts = query_posts($query_string . '&orderby=title&order=asc');
?>
In your case, you might also need to put is_home() condition
<?php
if(is_home()){
global $query_string;
query_posts($query_string . '&orderby=title&order=asc');
}
?>
Hope it helps :)

When moving a folder in the server, correct all links

Lets say I have this folder on my server called books.
Inside I have and index.php with this links:
books/book1.php
books/book2.php
I then decide to create a subdirectory called "scifi", for a better sorting of the books.
So the books are no in:
books/scifi/book1.php
books/scifi/book2.php
The links have changed and now the links on index.php dont work.
How can I (or redirect*) all the links without having to go one by one.
*please dont be thrown away by redirect I do not mean it in the programming way (necessarily) rather in an illustrative one.
$sampleLink = "books/book1.php";
$temp = explode("/",$sampleLink); //["books","book1.php"]
$temp[0] .= "/scifi"; //["books/scifi","book1.php"]
$sampleLink = implode("/",$temp); //"books/scifi/book1.php"
Just to clarify Steve´s answer:
<?php
$sampleLink = 'Book...';
$temp = explode("/",$sampleLink); //["books","book1.php"]
$temp[0] .= "/email/books/scifi"; //["books/scifi","book1.php"]
$sampleLink = implode("/",$temp); //"books/scifi/book1.php"
echo $sampleLink;
?>
It was really useful as a lesson but i didn´t have the links links defined as php variables so the work would be the same!
Thanks you very much!

Check condition and automatically redirect to a link, html

I'm creating a sample website using xhtml with javascript support. Also using php for server side programming. I need to redirect the webpage to some other page from an html page, after checking some condition.What is the best method to implement this. I've done it using
header("link"); but since i'm using it inside the tag, it shows up a error. is it possible to redirect to a particular link from within the tag.
The best way would be using PHP. Javascript solutions only work when… well, when javascript is enabled.
<?php
if($do_redirect) {
header("location: http://www.google.de");
}
?>
Note that this only works, when there was absolutely no output so far.
It depends on what the condition is.
If it is something you can test for in PHP, then do it in PHP (as one of the very first things you do, before you start thinking about generating output to the browser):
<?php
if (condition()) {
header("Location: http://example.com/foo/bar/baz");
exit();
}
?>
If it is something you can test for only in JavaScript then:
<script type="text/javascript">
if (condition()) {
location = "http://example.com/foo/bar/baz";
}
</script>
… keeping in mind the principles of progressive enhancement.
Sure. Do it through javascript:
window.location.href = 'http://www.google.com'
From within a tag you can use something like this
<tag onclick="if(condition == true) window.location.href = 'http://mylocation.com/'>Tag text</tag>
Most HTML tags support the onclick event, so replace tag with the type of tag you need for your situation.
The condition part can be an inline code to find some value, or some Javascript function you defined earlier.
try this
<?php
.
.
//your php code here
.
.
if($condition){
?>
script type='css/javascript'>
window.location.href = 'http://domain.com'
/script>
<?php
} else{
.
.
//again your php code here
.
.
}
?>