{"id":104,"date":"2025-03-04T03:08:15","date_gmt":"2025-03-04T03:08:15","guid":{"rendered":"https:\/\/notes.zj1963.com\/?p=104"},"modified":"2025-03-04T03:42:29","modified_gmt":"2025-03-04T03:42:29","slug":"query-loop-inline-items","status":"publish","type":"post","link":"https:\/\/notes.zj1963.com\/?p=104","title":{"rendered":"Query Loop &#8211; Inline items"},"content":{"rendered":"\n<p>In WordPress, when using the <strong>Query Loop block<\/strong> in the <strong>Block Editor<\/strong>, the default behavior is that each item (post) appears on a new line. However, if you want the items to appear <strong>inline<\/strong> (following the flow of the previous item instead of starting on a new line), you can achieve this by customizing the layout using <strong>CSS<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Solution: Use Custom CSS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Select the Query Loop block<\/strong> in the WordPress editor.<\/li>\n\n\n\n<li>Inside the Query Loop, <strong>choose the inner container<\/strong> (e.g., Post Template).<\/li>\n\n\n\n<li>Add a <strong>custom class<\/strong> to the &#8220;Additional CSS Class&#8221; field under the &#8220;Advanced&#8221; settings.\n<ul class=\"wp-block-list\">\n<li>Example: Add a class like <code>inline-query-loop<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Apply custom CSS<\/strong> to make the posts flow inline.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CSS to Add<\/strong><\/h4>\n\n\n\n<p>You can add this CSS in the <strong>Customizer \u2192 Additional CSS<\/strong> or in your theme&#8217;s CSS file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.inline-query-loop {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 5px; \/* Adjust the space between posts *\/\n    list-style: none; \/* Remove default list styling *\/\n    padding: 0;\n    margin: 0;\n}\n\n.inline-query-loop .wp-block-post {\n    display: inline-block;\n    white-space: nowrap; \/* Ensures text does not wrap inside *\/\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Alternative: Use a Grid Layout<\/strong><\/h3>\n\n\n\n<p>If you want a structured layout rather than pure inline elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.inline-query-loop .wp-block-post-template {\n    display: grid;\n    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); \/* Adjust width *\/\n    gap: 20px;\n}\n<\/code><\/pre>\n\n\n\n<p>This will make the posts appear in a grid format.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Customizing the Query Loop in PHP (If Using a Custom Theme)<\/strong><\/h3>\n\n\n\n<p>If you&#8217;re coding a <strong>custom Query Loop<\/strong> in a theme, you can modify the <strong>WordPress Loop<\/strong> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$args = array(\n    'post_type' =&gt; 'post',\n    'posts_per_page' =&gt; 6\n);\n$query = new WP_Query($args);\nif ($query-&gt;have_posts()) :\n    echo '&lt;div class=\"custom-query-loop\"&gt;';\n    while ($query-&gt;have_posts()) : $query-&gt;the_post();\n        echo '&lt;div class=\"post-item\"&gt;';\n        the_title('&lt;h2&gt;', '&lt;\/h2&gt;'); \n        the_excerpt();\n        echo '&lt;\/div&gt;';\n    endwhile;\n    echo '&lt;\/div&gt;';\n    wp_reset_postdata();\nendif;\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>Then, in your CSS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.custom-query-loop {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 20px;\n}\n\n.post-item {\n    display: inline-block;\n    width: auto;\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>For Block Editor<\/strong>: Use the &#8220;Additional CSS Class&#8221; and apply CSS with <code>display: flex<\/code> or <code>grid<\/code>.<\/li>\n\n\n\n<li><strong>For Custom Themes<\/strong>: Modify the WordPress Loop and style with CSS.<\/li>\n<\/ul>\n\n\n\n<p>Would you like me to help with a specific theme or plugin? \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In WordPress, when using the Query Loop block in the Block Editor, the default behavior is that each item (post) appears on a new line. However, if you want the items to appear inline (following the flow of the previous item instead of starting on a new line), you can achieve this by customizing the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":2,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}