How-To

WordPress: One Way To Display The Perfect Title

While the work has been sometimes laborious to get this blog finished and ready to view, one of the more difficult things was to get the details right in the tag. Many of the snippets of code out there suggest using various separators and little tricks to determine if the user is at the home page. What I was after was this:</p> <p>If at the home page, display the blog’s name and description only. If on an article, display only the title of that article. The resulting code is a mix of php snippets found all over the web but one great way to make the links a search engine provides look better.<br /> <span id="more-1708"></span></p> <p>You’ll need to copy and paste this into your active theme’s header.php file. Click on the image below to get plain text of the code:<br /> <img fetchpriority="high" decoding="async" class="aligncenter wp-image-1711" title="TitleCode" src="https://cwl.cc/wp-content/uploads/2012/12/TitleCode.png" alt="" width="562" height="195" />What the above snippet uses on line #1 is the <a href="http://codex.wordpress.org/Function_Reference/is_home">is_home()</a> php function to check for the home page and, with an if… else statement, perform some work. On Blogging Calwell, you’ll find this implemented on all pages.</p> </div><!-- .entry-content --> <div class="entry-footer clearfix"> <div class="tagcloud"><a href="https://cwl.cc/tag/code-examples" rel="tag">Code Examples</a> <a href="https://cwl.cc/tag/programming" rel="tag">Programming</a> <a href="https://cwl.cc/tag/wordpress" rel="tag">Wordpress</a></div><span class="sl-wrapper"><a href="https://cwl.cc/wp-admin/admin-ajax.php?action=process_simple_like&post_id=1708&nonce=c248533b6a&is_comment=0&disabled=true" class="sl-button sl-button-1708" data-nonce="c248533b6a" data-post-id="1708" data-iscomment="0" title="Like"><span class="sl-icon"><svg role="img" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0" y="0" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve"><path id="heart" d="M64 127.5C17.1 79.9 3.9 62.3 1 44.4c-3.5-22 12.2-43.9 36.7-43.9 10.5 0 20 4.2 26.4 11.2 6.3-7 15.9-11.2 26.4-11.2 24.3 0 40.2 21.8 36.7 43.9C124.2 62 111.9 78.9 64 127.5zM37.6 13.4c-9.9 0-18.2 5.2-22.3 13.8C5 49.5 28.4 72 64 109.2c35.7-37.3 59-59.8 48.6-82 -4.1-8.7-12.4-13.8-22.3-13.8 -15.9 0-22.7 13-26.4 19.2C60.6 26.8 54.4 13.4 37.6 13.4z"/>♥</svg></span><span class="sl-count">212</a><span id="sl-loader"></span></span> </div> <div class="share-post"><a class="twit" target="_blank" href="https://twitter.com/intent/tweet?text=WordPress: One Way To Display The Perfect Title&url=https://cwl.cc/2012/12/wordpress-one-way-to-display-the-perfect-title.html" title="Twitter"><i class="fab fa-twitter"></i></a><a class="face" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://cwl.cc/2012/12/wordpress-one-way-to-display-the-perfect-title.html" title="Facebook"><i class="fab fa-facebook-f"></i></a><a class="linked" target="_blank" href="https://www.linkedin.com/shareArticle?mini=true&url=https://cwl.cc/2012/12/wordpress-one-way-to-display-the-perfect-title.html&title=WordPress: One Way To Display The Perfect Title&summary=https://cwl.cc&source=CWL" title="LinkedIn"><i class="fab fa-linkedin-in"></i></a> <a class="tumblr" target="_blank" href="http://www.tumblr.com/share/link?url=https://cwl.cc/2012/12/wordpress-one-way-to-display-the-perfect-title.html&name=WordPress: One Way To Display The Perfect Title&description=While the work has been sometimes laborious to get this blog finished and ready to view, one of the more difficult things was to get the details right in the tag. Many of the snippets of code out there suggest using various separators and little tricks to determine if the user is at the home page. What I […]" title="Tumblr"><i class="fab fa-tumblr"></i></a><a class="reddit" href="http://reddit.com/submit?url=https://cwl.cc/2012/12/wordpress-one-way-to-display-the-perfect-title.html&title=WordPress: One Way To Display The Perfect Title" target="_blank" title="Reddit"><i class="fab fa-reddit-alien" aria-hidden="true"></i></a></div> </div> </article> </main><!-- #main --> </div><!-- #primary --> </div> </div> </div> </div><!-- #content --> <footer id="site-footer" class="site-footer" itemscope="itemscope" itemtype="http://schema.org/WPFooter"> <div data-elementor-type="wp-post" data-elementor-id="11335" class="elementor elementor-11335"> <section class="elementor-section elementor-top-section elementor-element elementor-element-46e95555 ot-traditional elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="46e95555" data-element_type="section" data-e-type="section"> <div class="elementor-container elementor-column-gap-default"> <div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-474f119 ot-flex-column-vertical" data-id="474f119" data-element_type="column" data-e-type="column"> <div class="elementor-widget-wrap elementor-element-populated"> <div class="elementor-element elementor-element-78853107 elementor-widget elementor-widget-text-editor" data-id="78853107" data-element_type="widget" data-e-type="widget" data-widget_type="text-editor.default"> <div class="elementor-widget-container"> <p style="text-align: center;"><small> This site is proudly written by a <img draggable="false" role="img" class="emoji" alt="🇨🇦" src="https://s.w.org/images/core/emoji/17.0.2/svg/1f1e8-1f1e6.svg">. This is all-human content. Please do consider <a href="https://cwl.cc/subscribe" data-type="page" data-id="11324">subscribing </a>to be updated with new posts. If you like this stuff and want to support my work, please see my <a href="http://patreon.com/cwl">Patreon</a>.</small> <br></p> <form method="get" id="search" action="https://duckduckgo.com/" style="text-align: center;"> <input type="hidden" name="sites" value="cwl.cc"><br> <input type="hidden" name="k8" value="#444444"><br> <input type="hidden" name="k9" value="#D51920"><br> <input type="hidden" name="kt" value="h"><br> <input type="text" name="q" maxlength="255" placeholder="Search cwl.cc…"><br> <input type="submit" value="Go"><br> </form> <p></p> </div> </div> </div> </div> </div> </section> </div> </footer></div><!-- #page --> <script type="speculationrules"> {"prefetch":[{"source":"document","where":{"and":[{"href_matches":"/*"},{"not":{"href_matches":["/wp-*.php","/wp-admin/*","/wp-content/uploads/*","/wp-content/*","/wp-content/plugins/*","/wp-content/themes/engitech-child/*","/wp-content/themes/engitech/*","/*\\?(.+)"]}},{"not":{"selector_matches":"a[rel~=\"nofollow\"]"}},{"not":{"selector_matches":".no-prefetch, .no-prefetch a"}}]},"eagerness":"conservative"}]} </script> <script id='kirki-viewport-lists'>var kirkiViewports = {"md":{"value":1200,"scale":1,"minWidth":1200,"maxWidth":1200,"title":"Desktop","icon":"desktop","activeIcon":"desktop-hover","details":"Styles added here will be applied globally.","guides":null,"id":"md","type":"max"},"tablet":{"value":991,"scale":1,"minWidth":991,"maxWidth":991,"title":"Tablet","icon":"tablet-default","activeIcon":"tablet-hover","details":"Styles added here will apply at 991px and below.","guides":null,"type":"max","id":"tablet"},"mobileLandscape":{"value":767,"scale":1,"minWidth":767,"maxWidth":767,"title":"Landscape","icon":"phone-hr-default","activeIcon":"phone-hr-hover","details":"Styles added here will apply at 767px and below.","guides":null,"type":"max","id":"mobileLandscape"},"mobile":{"value":575,"scale":1,"minWidth":575,"maxWidth":575,"title":"Mobile","icon":"phone-vr-default","activeIcon":"phone-vr-hover","details":"Styles added here will apply at 575px and below.","guides":null,"type":"max","id":"mobile"}};</script><script id='kirki-variable-lists'>var kirkiCSSVariable = {"data":[{"title":"Colors","key":"color","modes":[{"title":"Default","key":"default"}],"variables":[]},{"title":"Numbers","key":"size","modes":[{"title":"Default","key":"default"}],"variables":[]},{"title":"Text Styles","key":"text-style","modes":[{"title":"Default","key":"default"}],"variables":[]},{"title":"Font Family","key":"font-family","modes":[{"title":"Default","key":"default"}],"variables":[]}]};</script><script id="kirki-api-and-nonce"> window.wp_kirki = { ajaxUrl: "https://cwl.cc/wp-admin/admin-ajax.php", restUrl: "https://cwl.cc/wp-json/", siteUrl: "https://cwl.cc", apiVersion: "v1", postId: "1708", nonce: "7e9234be6f", call_from: "", templateId: "", context: {"id":1708,"type":"post"} }; </script><a id="back-to-top" href="#" class="show"><i class="flaticon-up-arrow"></i></a> <script> const lazyloadRunObserver = () => { const lazyloadBackgrounds = document.querySelectorAll( `.e-con.e-parent:not(.e-lazyloaded)` ); const lazyloadBackgroundObserver = new IntersectionObserver( ( entries ) => { entries.forEach( ( entry ) => { if ( entry.isIntersecting ) { let lazyloadBackground = entry.target; if( lazyloadBackground ) { lazyloadBackground.classList.add( 'e-lazyloaded' ); } lazyloadBackgroundObserver.unobserve( entry.target ); } }); }, { rootMargin: '200px 0px 200px 0px' } ); lazyloadBackgrounds.forEach( ( lazyloadBackground ) => { lazyloadBackgroundObserver.observe( lazyloadBackground ); } ); }; const events = [ 'DOMContentLoaded', 'elementor/lazyload/observe', ]; events.forEach( ( event ) => { document.addEventListener( event, lazyloadRunObserver ); } ); </script> <link rel='stylesheet' id='elementor-frontend-css' href='https://cwl.cc/wp-content/plugins/elementor/assets/css/frontend.min.css?ver=4.0.8' type='text/css' media='all' /> <link rel='stylesheet' id='elementor-post-11335-css' href='https://cwl.cc/wp-content/uploads/elementor/css/post-11335.css?ver=1778804945' type='text/css' media='all' /> <link rel='stylesheet' id='elementor-icons-css' href='https://cwl.cc/wp-content/plugins/elementor/assets/lib/eicons/css/elementor-icons.min.css?ver=5.48.0' type='text/css' media='all' /> <link rel='stylesheet' id='elementor-post-10103-css' href='https://cwl.cc/wp-content/uploads/elementor/css/post-10103.css?ver=1778804945' type='text/css' media='all' /> <link rel='stylesheet' id='elementor-gf-local-roboto-css' href='https://cwl.cc/wp-content/uploads/elementor/google-fonts/css/roboto.css?ver=1742249596' type='text/css' media='all' /> <link rel='stylesheet' id='elementor-gf-local-robotoslab-css' href='https://cwl.cc/wp-content/uploads/elementor/google-fonts/css/robotoslab.css?ver=1742249767' type='text/css' media='all' /> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/seriously-simple-podcasting/assets/js/castos-player.min.js?ver=3.15.0" id="ssp-castos-player-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/gutenberg/build/scripts/hooks/index.min.js?ver=7496969728ca0f95732d" id="wp-hooks-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/gutenberg/build/scripts/i18n/index.min.js?ver=125448662852c5e18937" id="wp-i18n-js"></script> <script type="text/javascript" id="wp-i18n-js-after"> /* <![CDATA[ */ wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } ); //# sourceURL=wp-i18n-js-after /* ]]> */ </script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/contact-form-7/includes/swv/js/index.js?ver=6.1.5" id="swv-js"></script> <script type="text/javascript" id="contact-form-7-js-before"> /* <![CDATA[ */ var wpcf7 = { "api": { "root": "https:\/\/cwl.cc\/wp-json\/", "namespace": "contact-form-7\/v1" } }; //# sourceURL=contact-form-7-js-before /* ]]> */ </script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/contact-form-7/includes/js/index.js?ver=6.1.5" id="contact-form-7-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-includes/js/imagesloaded.min.js?ver=5.0.0" id="imagesloaded-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/jquery.magnific-popup.min.js?ver=20180910" id="magnific-popup-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/jquery.isotope.min.js?ver=20190829" id="isotope-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/slick.min.js?ver=20180910" id="slick-slider-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/easypiechart.min.js?ver=20190829" id="easypiechart-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/jquery.countdown.min.js?ver=20180910" id="countdown-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/elementor.js?ver=20180910" id="engitech-elementor-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/elementor-header.js?ver=20180910" id="engitech-elementor-header-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/scripts.js?ver=20180910" id="engitech-scripts-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/themes/engitech/js/header-mobile.js?ver=20180910" id="engitech-header-mobile-scripts-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/elementor/assets/js/webpack.runtime.min.js?ver=4.0.8" id="elementor-webpack-runtime-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/elementor/assets/js/frontend-modules.min.js?ver=4.0.8" id="elementor-frontend-modules-js"></script> <script type="text/javascript" src="https://cwl.cc/wp-includes/js/jquery/ui/core.min.js?ver=1.13.3" id="jquery-ui-core-js"></script> <script type="text/javascript" id="elementor-frontend-js-before"> /* <![CDATA[ */ var elementorFrontendConfig = {"environmentMode":{"edit":false,"wpPreview":false,"isScriptDebug":false},"i18n":{"shareOnFacebook":"Share on Facebook","shareOnTwitter":"Share on Twitter","pinIt":"Pin it","download":"Download","downloadImage":"Download image","fullscreen":"Fullscreen","zoom":"Zoom","share":"Share","playVideo":"Play Video","previous":"Previous","next":"Next","close":"Close","a11yCarouselPrevSlideMessage":"Previous slide","a11yCarouselNextSlideMessage":"Next slide","a11yCarouselFirstSlideMessage":"This is the first slide","a11yCarouselLastSlideMessage":"This is the last slide","a11yCarouselPaginationBulletMessage":"Go to slide"},"is_rtl":false,"breakpoints":{"xs":0,"sm":480,"md":768,"lg":1025,"xl":1440,"xxl":1600},"responsive":{"breakpoints":{"mobile":{"label":"Mobile Portrait","value":767,"default_value":767,"direction":"max","is_enabled":true},"mobile_extra":{"label":"Mobile Landscape","value":880,"default_value":880,"direction":"max","is_enabled":false},"tablet":{"label":"Tablet Portrait","value":1024,"default_value":1024,"direction":"max","is_enabled":true},"tablet_extra":{"label":"Tablet Landscape","value":1200,"default_value":1200,"direction":"max","is_enabled":false},"laptop":{"label":"Laptop","value":1366,"default_value":1366,"direction":"max","is_enabled":false},"widescreen":{"label":"Widescreen","value":2400,"default_value":2400,"direction":"min","is_enabled":false}},"hasCustomBreakpoints":false},"version":"4.0.8","is_static":false,"experimentalFeatures":{"additional_custom_breakpoints":true,"global_classes_should_enforce_capabilities":true,"e_variables":true,"e_opt_in_v4_page":true,"e_components":true,"e_interactions":true,"e_widget_creation":true,"import-export-customization":true},"urls":{"assets":"https:\/\/cwl.cc\/wp-content\/plugins\/elementor\/assets\/","ajaxurl":"https:\/\/cwl.cc\/wp-admin\/admin-ajax.php","uploadUrl":"https:\/\/cwl.cc\/wp-content\/uploads"},"nonces":{"floatingButtonsClickTracking":"8e46336043","atomicFormsSendForm":"9f4e30f720"},"swiperClass":"swiper","settings":{"page":[],"editorPreferences":[]},"kit":{"active_breakpoints":["viewport_mobile","viewport_tablet"],"global_image_lightbox":"yes","lightbox_enable_counter":"yes","lightbox_enable_fullscreen":"yes","lightbox_enable_zoom":"yes","lightbox_enable_share":"yes","lightbox_title_src":"title","lightbox_description_src":"description"},"post":{"id":1708,"title":"Wordpress%3A%20One%20Way%20To%20Display%20The%20Perfect%20Title%20%E2%80%93%20CWL","excerpt":"","featuredImage":false}}; //# sourceURL=elementor-frontend-js-before /* ]]> */ </script> <script type="text/javascript" src="https://cwl.cc/wp-content/plugins/elementor/assets/js/frontend.min.js?ver=4.0.8" id="elementor-frontend-js"></script> <script id="wp-emoji-settings" type="application/json"> {"baseUrl":"https://s.w.org/images/core/emoji/17.0.2/72x72/","ext":".png","svgUrl":"https://s.w.org/images/core/emoji/17.0.2/svg/","svgExt":".svg","source":{"concatemoji":"https://cwl.cc/wp-includes/js/wp-emoji-release.min.js?ver=6.9.4"}} </script> <script type="module"> /* <![CDATA[ */ /*! This file is auto-generated */ const a=JSON.parse(document.getElementById("wp-emoji-settings").textContent),o=(window._wpemojiSettings=a,"wpEmojiSettingsSupports"),s=["flag","emoji"];function i(e){try{var t={supportTests:e,timestamp:(new Date).valueOf()};sessionStorage.setItem(o,JSON.stringify(t))}catch(e){}}function c(e,t,n){e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(t,0,0);t=new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data);e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(n,0,0);const a=new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data);return t.every((e,t)=>e===a[t])}function p(e,t){e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(t,0,0);var n=e.getImageData(16,16,1,1);for(let e=0;e<n.data.length;e++)if(0!==n.data[e])return!1;return!0}function u(e,t,n,a){switch(t){case"flag":return n(e,"\ud83c\udff3\ufe0f\u200d\u26a7\ufe0f","\ud83c\udff3\ufe0f\u200b\u26a7\ufe0f")?!1:!n(e,"\ud83c\udde8\ud83c\uddf6","\ud83c\udde8\u200b\ud83c\uddf6")&&!n(e,"\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f","\ud83c\udff4\u200b\udb40\udc67\u200b\udb40\udc62\u200b\udb40\udc65\u200b\udb40\udc6e\u200b\udb40\udc67\u200b\udb40\udc7f");case"emoji":return!a(e,"\ud83e\u1fac8")}return!1}function f(e,t,n,a){let r;const o=(r="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?new OffscreenCanvas(300,150):document.createElement("canvas")).getContext("2d",{willReadFrequently:!0}),s=(o.textBaseline="top",o.font="600 32px Arial",{});return e.forEach(e=>{s[e]=t(o,e,n,a)}),s}function r(e){var t=document.createElement("script");t.src=e,t.defer=!0,document.head.appendChild(t)}a.supports={everything:!0,everythingExceptFlag:!0},new Promise(t=>{let n=function(){try{var e=JSON.parse(sessionStorage.getItem(o));if("object"==typeof e&&"number"==typeof e.timestamp&&(new Date).valueOf()<e.timestamp+604800&&"object"==typeof e.supportTests)return e.supportTests}catch(e){}return null}();if(!n){if("undefined"!=typeof Worker&&"undefined"!=typeof OffscreenCanvas&&"undefined"!=typeof URL&&URL.createObjectURL&&"undefined"!=typeof Blob)try{var e="postMessage("+f.toString()+"("+[JSON.stringify(s),u.toString(),c.toString(),p.toString()].join(",")+"));",a=new Blob([e],{type:"text/javascript"});const r=new Worker(URL.createObjectURL(a),{name:"wpTestEmojiSupports"});return void(r.onmessage=e=>{i(n=e.data),r.terminate(),t(n)})}catch(e){}i(n=f(s,u,c,p))}t(n)}).then(e=>{for(const n in e)a.supports[n]=e[n],a.supports.everything=a.supports.everything&&a.supports[n],"flag"!==n&&(a.supports.everythingExceptFlag=a.supports.everythingExceptFlag&&a.supports[n]);var t;a.supports.everythingExceptFlag=a.supports.everythingExceptFlag&&!a.supports.flag,a.supports.everything||((t=a.source||{}).concatemoji?r(t.concatemoji):t.wpemoji&&t.twemoji&&(r(t.twemoji),r(t.wpemoji)))}); //# sourceURL=https://cwl.cc/wp-includes/js/wp-emoji-loader.min.js /* ]]> */ </script> </body> </html>