Technically your first example is also incorrect, but the problem is not apparent. You’re using template tags that themselves echo their content. For example, you can write simply:
the_permalink();
without echo, and the permalink will be output. This is because that function echoes its value. To output the permalink within an echo or variable assignment you need to use get_permalink:
echo get_permalink()
Just about all WordPress template tags have equivalent get_ versions, or an argument that toggles echo and return, like the_title:
the_title( '', '', false );
Here the 3rd parameter toggles echo (true) or return (false).
Related Posts:
- Can the new 4.8v text widget visual editor be removed?
- Passing user enetered value in widget: number of words, for example
- How to influence the information displayed on widget inside wp-admin
- How to add css class to cloud tag anchors?
- How can I get $id variable in widget’s form function?
- Using add_filter() in Widgets
- Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option
- remove the wrapping of text widget or
- remove from text-widget
- Programmatically Add Font-Awesome Icons to Category Widget
- Authors details such as social media links, emails etc → Is this Meta or something else?
- Dynamically Register Sidebars For Each Top Level Page
- Customising widget titles/headings in WP 5.8
- Widgets Section not displaying in theme customizer
- Displaying Widgets
- What is the most efficient method for loading widgets in functions.php
- Ajaxing function in widget class
- Increment price for Woocommerce Minicart [closed]
- Missing argument 2 for a custom function widgets_init
- Adding widget to dashboard with wp_dashboard_setup not working
- How to insert custom widget in custom sidebar in theme activation?
- Right way to update widget on dynamic new input field
- Passing values from a widget to a function within a plugin
- Override widget in function WordPress
- Best practices regarding the creation of custom widgets?
- How can I make a widget shortcode to control all the widgets?
- Replace a menu with widget or a custom template file programmatically
- Custom taxonomy widget in admin area
- Dashboard widget custom positioning?
- How do you Permanently Delete Sidebars
- WordPress Shortcode function display outside of widget
- Get rid of the widget Item Class=”widget-item”
- Custom Sidebar only on single post
- Widget back end radio button issue
- Why is it so hard to add a class to the tag in the sidebar widget?
- The correct way to add a JavaScript in the functions.php [closed]
- Create a Woocommerce product widget with category filter
- Add element to widgetpage
- “All posts” in the category widget
- Register_Sidebar overwriting itself and doesn’t exist in global $wp_registered_sidebars;
- widgets not working
- How to echo Widget Title in Custom Frontend-Template Box
- New checkbox in custom widget isn’t saving data
- the_widget() inside functions.php
- How to conditionally add a widget via a hook in functions.php
- Creating a custom wordpress widget and stopping js from running twice(once in active widget once in widget selector)?
- How Do I include/Import a Custom Widget from the Parent theme into My Child Theme?
- Add a custom function to widget code
- Include widget file in functions.php of child theme
- Enable accessibility mode in widget as default
- Widget is displayed in two places
- Trying to change widgets h-tags
- Extend WordPress Audio Widget to accept HLS (.m3u8) URLs
- my wordpress doesn’t save items to customized widget area
- Copying a widget class to functions.php results on a blank page
- How to add widget in main menu
- Adding A New Widget to WordPress Disables the Existing Widgets
- wp_nav_walker that interacts with widgets to setup mega menu
- Expanding a function to call a plugin or show a link
- How to programmatically GET (to know) which Sidebar (or) which Menu is being used on Different Pages?
- WordPress Javascript Widget jQuery Dependency Issue
- Register a widget area when a theme option has been saved?
- Give a function a unique ID
- Add the title of a widget as an ID – for anchor links
- Swap WordPress “Widget Area” based on Page Template
- Ajax Form Issues using Lightsail (AWS)
- page shows short code not output
- How to update an image attachment’s alt text from a custom field when saving a post?
- Can I use ‘Featured Image’ as a hero image and a thumbnail?
- How to Override Page Template if URL matches query?
- Probleme shortcode with list author
- how to check if an element in an html file exists in another html file? [closed]
- Save_post – Warning: Cannot modify header information
- Writing a function for WP Cron to run a SQL command daily
- How to get individual values from custom widget?
- What is the earliest Hook a Script can use?
- removing description tabs – single product page – breaks footer
- How do I trigger WP CLI DB export using a PHP function?
- Add Javascript code to functions.php childtheme
- Populate acf-field, depending on block name
- How to use WP conditional tag in a functions.php with OCEANWP Theme?
- If has action not working as expected
- Update wp_postmeta table based on 2 keys
- Best practice to call a function from a button-link?
- WordPress does not load jQuery
- Disable wordpress update notification
- How Can I Create a Friendly Slug for a Dynamic Page?
- Link product attribute value to a URL – woocommerce
- Google Analytics – add script to functions.php
- custom error message for empty username and password using authenticate filter not working
- Class parameter in get_avatar args doesn’t get added to output
- Deleting guests profile users after 1.5 hours
- Remove check boxes and its label from screen option for custom role
- get variables data from functions.php to template wordpress (without global variables)
- Removing comment lines from all enqueued js files using a filter function
- Modifying a WordPress Plugin
- ACF Flexible in TCPDF
- Function to function shortcode help [closed]
- Last updated posts shortcode in functions.php
- wp enqueue style on about us page